【问题标题】:FormsAuthentication.SetAuthCookie(user.UserName, true); cookie nameFormsAuthentication.SetAuthCookie(user.UserName, true);曲奇名称
【发布时间】:2016-01-12 22:37:19
【问题描述】:

这个 cookie 被设置时叫什么?我可以在浏览器的 cookie 文件夹中看到它吗?

我认为它没有设置,所以我的登录验证失败。

【问题讨论】:

    标签: c# asp.net authentication forms-authentication


    【解决方案1】:

    这个 cookie 被设置时叫什么?

    cookie 的默认名称是.ASPXAUTHconfigurable 通过 web.config 可以有任何名称。

    <authentication mode="Forms">
      <forms name=".SomeName" loginUrl="Login.aspx" />
    </authentication>
    

    所以检查那里是否有任何不标准的地方。

    我可以在浏览器的 cookie 文件夹中看到它吗?

    您应该能够使用任何浏览器工具看到它。

    【讨论】:

    • 仅当您使用持久 cookie 时,作者似乎是因为它们传递的是 true。
    【解决方案2】:

    通常,如果您自己创建身份验证Cookie,则需要创建 Principal 对象并将其保存在 AuthenticateRequest 事件中的 Current Thread 中每个请求。

    否则,当你检查User.Identity.IsAuthenticated时,它会返回false。

    Global.asax.cs

    public class Global : HttpApplication
    {
        private void Application_AuthenticateRequest(object sender, EventArgs e)
        {
            HttpCookie decryptedCookie =
                Context.Request.Cookies[FormsAuthentication.FormsCookieName];
    
            FormsAuthenticationTicket ticket =
                FormsAuthentication.Decrypt(decryptedCookie.Value);
    
            var identity = new GenericIdentity(ticket.Name);
            var principal = new GenericPrincipal(identity, null);
    
            HttpContext.Current.User = principal;
            Thread.CurrentPrincipal = HttpContext.Current.User;
        }
    }
    

    web.config

    确保您在 web.config 中有身份验证标签。例如,

    <authentication mode="Forms">
       <forms loginUrl="~/Account/Login" timeout="2880"/>
    </authentication>
    

    用法

    public ActionResult Index()
    {
        var username = User.Identity.Name;
        return View();
    }
    

    浏览器插件

    我使用EditThisCookie Chrome 插件。

    【讨论】:

    • 它仍在网络表单中(来自客户端的旧代码),但现在正在研究更多内容
    猜你喜欢
    • 2023-01-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多