c# - Cookie 未在 mvc(c#) 中删除

标签 c# asp.net-mvc cookies

我想在 mvc4 中创建登录和注销功能。在登录函数中,如果登录 cookie 存在且不为空,则用户处于登录模式,否则重定向到登录页面。 在 logOut func 中,所有 cookie 和 session 都被清除并重定向到 login func,但在 login func 中存在登录 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 的值,但您并没有将它再次添加到响应中!

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();

关于c# - Cookie 未在 mvc(c#) 中删除,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17592530/

相关文章:

c# - "There was an error sending this message to your bot: HTTP status code NotFound"

c# - 打开图像并使用相同的参数重新保存,c#

c# - 发布到 mvc actionresult 时,Json 数组对象始终为空

javascript - hibext_instdsigdipv2 cookie 来自哪里?

ios - 保留 "session only"cookies, iOS

c# - 如何知道使用 Entity Framework 4.1 的 asp.net MVC3 应用程序当前打开了多少个连接

c# - 如何获取自定义类中对象的索引?

jquery - MVC 4 ApiController 操作上的空参数

asp.net-mvc - 在不杀死 IntelliSense 的情况下将 MVC Futures 集成到 Spark View 引擎中

asp.net-mvc - 为什么在 asp.net 身份中注销早于 ExpireTimeSpan?