【问题标题】:Forms Authentication Cookie is not setting?表单身份验证 Cookie 未设置?
【发布时间】:2013-09-16 14:31:17
【问题描述】:

我正在尝试使用FormsAuthentication.RedirectFromLoginPage(username,true,cookiepath); 在使用FormsAuthentication.RedirectFromLoginPage 时,它会重定向到web.config 中提供的DefaultUrl

web.config 中的身份验证部分:

<authentication mode="Forms">
<forms name=".ASPXADMINAUTH" 
               loginUrl="/Default.aspx" 
               defaultUrl="homepage.aspx"
               protection="All"
              timeout="30" path="/admin" slidingExpiration="true" enableCrossAppRedirects="false" cookieless="UseCookies" domain="localhost" ticketCompatibilityMode="Framework20" ></forms>
</authentication>

在 httpModules 部分:

<add name="FormsAuthentication" type="System.Web.Security.FormsAuthenticationModule" />

页面正在从登录页面重定向到“homepage.aspx”,但它没有设置身份验证 cookie。

在我的Response header中,Set-Cookie包含Authentication cookie,但在homepage.aspx页面中没有设置。

所以 LoginStaus 和 LoginName 控件不起作用。

【问题讨论】:

  • 听起来您正在通过与设置 cookie 不同的域名访问您的网站。
  • @KlausByskovPedersen 感谢您的回复。在我的代码中,我将域用作“localhost:51370”,并且我还通过删除域属性进行了检查。但是没有用。
  • 您的所有页面都位于/admin 下吗?因为如果不是,您应该将path 属性更改为/
  • 域名有什么用..? /admin 和 /.. 有区别吗?
  • 如果您为domain=localhostpath=/admin 设置cookie,您的浏览器只会在访问该域上的资源并使用正确路径(例如http://localhost/admin/somepage.aspx。如果您访问http://localhost/Default.aspx,它不会将cookie发送到服务器,因为路径不匹配。域也是如此。如果您为localhost 设置了cookie,但后来通过mydevserver 等别名访问服务器,浏览器也不会发送cookie。

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


【解决方案1】:

你代码中的问题是path="/admin" domain="localhost"

根据你的代码

用户登录后,会在 /admin 下设置一个 cookie。结果,/admin 文件夹下的每个页面都知道用户已通过身份验证,例如 ~/admin/default.aspx。

但是 ~/homepage.aspx 不知道用户,因为 ~/homepage.aspx 无法读取 /admin 下编写的 cookie。

var path = FormsAuthentication.FormsCookiePath;
FormsAuthentication.RedirectFromLoginPage("win", false, path);

如何解决?

你想慢慢开始使用简单的。然后根据您的需要进行调整。

<forms loginUrl="~/Default.aspx" timeout="2880" defaultUrl="~/homepage.aspx" />

仅供参考:请不要添加默认属性,例如slidingExpiration="true"enableCrossAppRedirects="false" 等等。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-07-12
    • 1970-01-01
    • 1970-01-01
    • 2018-03-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多