【问题标题】:Cookie is not delete in mvc(c#)Cookie 未在 mvc(c#) 中删除
【发布时间】:2013-07-13 08:02:39
【问题描述】:

我想在 mvc4 中创建登录和注销功能。在登录函数中,如果登录 cookie 存在且不为空,则用户处于登录模式,否则重定向到登录页面。 在 logOut func 中,所有 cookie 和 session 都清除并重定向到 login func,但在 login func 中存在 login cookie!

登录:

public ActionResult Login()
        {
            if (Request.Cookies["login"] != null)
            {
                string login = Request.Cookies["login"].Value.ToString();                

                if (login != string.Empty)
                {
                    //Get from service
                    Service srv = new Service();
                    UserItem userItem = srv.getUserItem(login);                    
                    srv.Close();

                    Session.Timeout = 30;
                    Session["login "] = login;
                    Session["userId"] = userItem.No;
                    Session["firstName"] = userItem.FirstName;
                    Session["lastName"] = userItem.LastName;
                    string loginName = userItem.LoginName;                    

                    FormsAuthentication.SetAuthCookie(loginName, false);

                    return Redirect(“Index”);
                }
                else 
                {
                    Return redirect("http://mySite/SignIn.aspx");
                }
            }
            else
            {
                Return redirect("http://mySite/SignIn.aspx");                    
            }
        }

注销:

public ActionResult LogOut()
        {
            string login = Session["login"].ToString();

            Request.Cookies["login"].Value = "";
            Response.Cookies["login"].Value = "";

            FormsAuthentication.SignOut();
            HttpCookie c = Request.Cookies[FormsAuthentication.FormsCookieName];
            c.Expires = DateTime.Now.AddDays(-1);

            Session.Clear();
            Request.Cookies.Clear();
            Response.Cookies.Clear();

            //FormsAuthentication.Initialize();
            //string strRole = String.Empty;
            //FormsAuthenticationTicket fat = new FormsAuthenticationTicket(1, "", DateTime.Now, DateTime.Now.AddMinutes(-30), false, strRole, FormsAuthentication.FormsCookiePath);
            //Response.Cookies.Add(new HttpCookie(FormsAuthentication.FormsCookieName, FormsAuthentication.Encrypt(fat)));

            //Session.Abandon();

            //// clear authentication cookie
            //HttpCookie cookie1 = new HttpCookie(FormsAuthentication.FormsCookieName, "");
            //cookie1.Expires = DateTime.Now.AddYears(-1);
            //Response.Cookies.Add(cookie1);

            //// clear session cookie (not necessary for your current problem but i would recommend you do it anyway)
            //HttpCookie cookie2 = new HttpCookie("ASP.NET_SessionId", "");
            //cookie2.Expires = DateTime.Now.AddYears(-1);
            //Response.Cookies.Add(cookie2);

            //FormsAuthentication.RedirectToLoginPage();               

            return RedirectToAction("Login", "Usr");
        }

Web.config:

<authentication mode="Forms">
      <forms loginUrl="~/Usr/Login" timeout="30" />
    </authentication>

我正在尝试评论代码,甚至评论这一行:

FormsAuthentication.SignOut();

即使我将 cookie 值设置为“”,但在登录页面中,此 cookie 具有旧值! 并尝试了几种方法来清除cookie,比如设置过期一天后。但是……

谢谢

【问题讨论】:

  • 忽略有更好的方法可以做到这一点,为了从浏览器中删除 cookie,您 1) 必须修改它以使其过期 2) 将其返回响应中的浏览器。您正在修改它,但浏览器不会知道,因为您没有返回它。
  • 我知道我跑题了,但仍然建议你看看 ASP.Net Membership 提供者和 MVC 的 Authorize 属性。它消除了复杂的实现。

标签: c# asp.net-mvc cookies


【解决方案1】:

您正在更改 cookie 的值,但不会再次将其添加到响应中!

FormsAuthentication.SignOut();
HttpCookie c = Request.Cookies[FormsAuthentication.FormsCookieName];
c.Expires = DateTime.Now.AddDays(-1);

// Update the amended cookie!
Response.Cookies.Set(c)

Session.Clear();
/* Get rid of this, it will break the above by clearing
 * the cookie collection that you've just updated. */
// Request.Cookies.Clear();
// Response.Cookies.Clear();

【讨论】:

  • 那你没有正确实现它。如果您更新 cookie 然后设置它,它将在响应中返回并更新(过期)。如果这没有发生,那么您在发送响应之前再次清除它。
  • 非常感谢,Ant P,AndreyMaybe。它的工作,但我不能回答我的问题,直到 8 小时
【解决方案2】:

根据这篇文章,有一种更简单的方法可以确定用户是否经过身份验证 How to check if user is authorized inside Action

调用 FormsAuthentication.SetAuthCookie() 后,您可以调用 User.Identity.IsAuthenticated。无需设置您自己的 cookie。

如果你这样做,FormsAuthentication.SignOut() 将销毁正确的 cookie

【讨论】:

  • 我需要这个cookie,因为这个cookie是在服务中设置的(我的登录页面)
【解决方案3】:

谢谢AndreyMaybe,Ant P

此代码有效:

Response.Cookies.Clear();

FormsAuthentication.SignOut();     

HttpCookie c = new HttpCookie("login");
c.Expires = DateTime.Now.AddDays(-1);
Response.Cookies.Add(c);

Session.Clear();

【讨论】:

    猜你喜欢
    • 2010-12-18
    • 1970-01-01
    • 1970-01-01
    • 2011-08-06
    • 2011-07-27
    • 1970-01-01
    • 2012-04-23
    • 2016-06-03
    • 2013-12-11
    相关资源
    最近更新 更多