注销时无法删除Cookie

天蝎座

//接口-注入控制器

  public interface IContext
{
    HttpRequestBase Request { get; }
    HttpResponseBase Response { get; }
}

//执行

  public class Context : IContext
{
    public HttpRequestBase Request { get { return new HttpRequestWrapper(HttpContext.Current.Request); } }
    public HttpResponseBase Response { get { return new HttpResponseWrapper(HttpContext.Current.Response); } }
}

//在登录时设置Cookie。SetCookie(_context,登录);

public void SetCookie(IContext context, Login login)
{
        var loginDetails = new JavaScriptSerializer().Serialize(login);
        var cokie = new HttpCookie("login", login);
        context.Response.Cookies.Add(cokie);
}

//尝试注销。

    public ActionResult Logout()
    {
        foreach (var key in _context.Request.Cookies.AllKeys)
        {
            try
            {
                _context.Response.Cookies.Remove(key); //this didn't work

                //tried this even this is not working
                var login = new Login {Identity = "", LogIn = false};
                _login.SetCookie(_context, login);
            }
            catch (Exception e)
            {
            }
        }
        _context.Response.Cookies.Clear(); //this didn't work either

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

在登录索引上,当我检查当前登录cookie值时,它始终具有已登录用户的值,只是没有将其设置为null或为空。我怀疑IContext的实现中存在问题。应该有塞特犬吗?没有把握..

也尝试过:

        var cokie = context.Request.Cookies["login"];
        cokie.Expires = DateTime.Now.AddDays(-2);
        cokie.Value = null;
        context.Response.Cookies.Add(cokie);
文件夹横幅

如果您使用表单身份验证,则可以使用以下代码。它将清除所有必需的cookie

FormsAuthentication.SignOut();
Session.Abandon();

或者,您可以使用

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

或者

YourCookies.Expires = DateTime.Now.AddDays(-1d);

欲了解更多信息,请访问

MSDN Cookie帮助

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章