Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
123 views
in Technique[技术] by (71.8m points)

c# - I need help. My ASP.Net MVC Logout not working

When I log in, I am unable to log out. The session doesn't clear and I don't know why? When I click on Logout, it doesn't do anything. I want to click Logout and then it switches the navbar to show Register/Login. But it doesn't clear the session. In the Logout Method in UserController.cs, I've tried Session.Clear() Session.Abandon() FormsAuthentication.Signout() Below is my Controller UserController.cs

  // Login POST
    [HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult Login(UserLogin login, string ReturnUrl = "")
    {
        string message = "";
        using (SocialEntities dc = new SocialEntities())
        {
            var v = dc.tblUsers.Where(a => a.Email == 
                          login.Email).FirstOrDefault();
            if (v != null)
            {
                if (string.Compare(HashPassword.Hash(login.Password), 
                                                    v.Password) == 0)
                {
                    int timeout = login.RememberMe ? 525600 : 20; // 525600 
                                                               min = 1 year
                    var ticket = new FormsAuthenticationTicket(login.Email, 
                                                login.RememberMe, timeout);
                    string encrypted = FormsAuthentication.Encrypt(ticket);
                    var cookie = new 
                  HttpCookie(FormsAuthentication.FormsCookieName, encrypted);
                    cookie.Expires = DateTime.Now.AddMinutes(timeout);
                    cookie.HttpOnly = true;
                    Response.Cookies.Add(cookie);

                    if (Url.IsLocalUrl(ReturnUrl))
                    {
                        return Redirect(ReturnUrl);
                    }
                    else
                    {
                        Session["Email"] = login.Email;
                        return RedirectToAction("Index", "Home");
                    }
                }
            }
            else
            {
                message = "Error";
            }


        }
        ViewBag.Message = message;
        return View();
    }

    // Logout
    [Authorize]

    [HttpPost]
    public ActionResult Logout()
    {
        Session.Clear();
        return RedirectToAction("Index", "Home");
    }

And here is my _Layout.schtml

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>@ViewBag.Title - Social Network</title>
    @Styles.Render("~/Content/css")
    @Scripts.Render("~/bundles/modernizr")
</head>
<body>
    <div class="navbar navbar-inverse navbar-fixed-top">
        <div class="container">
            <div class="navbar-header">
                <button type="button" class="navbar-toggle" data-toggle="collapse" data- 
                 target=".navbar-collapse">
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
            </button>
            @Html.ActionLink("Social Network", "Index", "Home", new { area = "" }, new { @class = 
 "navbar-brand" })
            </div>
            <div class="navbar-collapse collapse">
                <ul class="nav navbar-nav">
                    <li>@Html.ActionLink("Home", "Index", "Home")</li>
                    <li>@Html.ActionLink("About", "About", "Home")</li>
                    <li>@Html.ActionLink("Contact", "Contact", "Home")</li>
                </ul>
                @*@Html.Partial("_loginPartial")*@          
            <ul class="nav navbar-nav" style="float:right !important">
                @if (Session["Email"] != null)
                {
                    <li> <p style="color:red; margin-top:15px;">Hello, @Session["Email"] </p></li>
                    <li>@Html.ActionLink("Logout", "Index", "Home")</li>
                }
                else
                {
                    <li>@Html.ActionLink("Register", "Registration", "User")</li>
                    <li>@Html.ActionLink("Login", "Login", "User")</li>
                }
            </ul>
            </div>
        </div>
    </div>
    <div class="container body-content">
        @RenderBody()
        <hr />
        <footer>
            <p>&copy; @DateTime.Now.Year - Keane, Inc.</p>
        </footer>
    </div>

    @Scripts.Render("~/bundles/jquery")
    @Scripts.Render("~/bundles/bootstrap")
    @RenderSection("scripts", required: false)
</body>
</html>
question from:https://stackoverflow.com/questions/65647142/i-need-help-my-asp-net-mvc-logout-not-working

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...