【问题标题】:Why ASPXAUTH cookie not found in the Request.Cookies[FormsAuthentication.FormsCookieName]?为什么在 Request.Cookies[FormsAuthentication.FormsCookieName] 中找不到 ASPXAUTH cookie?
【发布时间】:2013-09-16 21:29:08
【问题描述】:

我花了几个小时来解决这个问题,但我不能。

当我在网站项目中工作时,在登录页面中验证用户时,我使用 FormsAuthentication.RedirectFromLoginPage 将用户重定向到主页,它起作用了,我在 Request.Cookies 中找到了 ASPXAUTH cookie。但是在 Web 应用程序项目中,我做了同样的事情,但没有工作。看起来很奇怪。在FormsAuthentication.RedirectFromLoginPage 之后,页面被重定向,但在Request.Cookies[FormsAuthentication.FormsCookieName] 中找不到ASPXAUTH cookie。

我错过了什么吗..?

为什么在 Request.Cookies 中找不到ASPXAUTH

请在这个问题上帮助我。

提前致谢。

【问题讨论】:

  • 您应该检查网络流量以查看您的 cookie 是否作为重定向的一部分传输到客户端。如果是,请检查此 cookie 是否一切正常(域、路径、到期日期)
  • @jbl 感谢您的回复域、路径和到期日期,一切都很好。你能告诉我如何检查网络流量吗?这对我会有帮助:)
  • @jbl 我正在使用 Fiddler。在该标头中,我在 Set-Cookie 标头响应中找到了 ASPXAUTH cookie,但在请求中我没有找到它。错过了。
  • 您尝试过几种浏览器吗?你的 cookie 有端口号吗?你的 web.config 中的 httpCookies 或 forms 元素中是否有 requireSSL="true" ?
  • 我也有同样的问题。只要 cookie 与 FormsAuthentication.FormsCookieName 的名称匹配,该 cookie 似乎就会自动从 Request.Cookies 集合中删除。

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


【解决方案1】:

我们在项目中使用了 mojoportal cms,因此当我设置 FormsAuthentication cookie 时,cms 还会检查其数据库中的用户名。在数据库中添加用户名后,问题就解决了。

如果您使用的是 https,请检查 Forms Authentication has been used in web.config 中的 RequireSSL=true。

<forms name=".mojochangeme"  protection="All" timeout="20160" path="/" cookieless="UseCookies" requireSSL="true" />

我的项目使用了两个数据库,如果我将用户名添加到另一个数据库,即 mojoportal 数据库,那么这对我来说不是一种有效的方法。

所以我使用了 CustomPrincipal 类来代替 FormsAuthentication 但不会设置 cookie。

CustomPrincipal 类:

public class CustomPrincipal:IPrincipal
    {
        public IIdentity Identity { get; set; }
        public bool IsInRole(string role) { return false; }
        public CustomPrincipal(string username)
        {
            this.Identity = new GenericIdentity(username);
        }
    }

用于在HttpContext中设置当前用户:

CustomPrincipal newUser = new CustomPrincipal(username);
                    HttpContext.Current.User = newUser;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2010-09-30
    • 1970-01-01
    • 2015-12-12
    • 1970-01-01
    • 2016-10-06
    • 1970-01-01
    • 2012-04-29
    • 1970-01-01
    相关资源
    最近更新 更多