Skip to main content
 首页 » 编程设计

c#之Page.User.Identity.Name 在子域的页面上为空

2024年02月24日31zhenyulu

我有多个子域试图使用一个子域进行身份验证,使用所有在 Windows Server 2008 r2 上运行的表单例份验证。

所有表单例份验证页面都设置为使用相同的名称,并且在身份验证页面上使用以下代码段添加 cookie:

 FormsAuthentication.SetAuthCookie(txtUserName.Text, false); 
 System.Web.HttpCookie MyCookie = System.Web.Security.FormsAuthentication.GetAuthCookie(User.Identity.Name.ToString(), false); 
 MyCookie.Domain = ConfigurationManager.AppSettings["domainName"]; 
 Response.AppendCookie(MyCookie); 

当我登录到 signon.mysite.com 时,page.user.identity.isauthenticated 和 page.user.identity.name 属性都可以正常工作。当我导航到 subdomain.mysite.com 时,page.user.identity.isauthenticated 返回 true,但名称为空。

我尝试使用以下方法从 cookie 中检索它,但它也是空白的。

 HttpCookie cookie = Request.Cookies[".ASPXAUTH"]; 
 FormsAuthenticationTicket fat = FormsAuthentication.Decrypt(cookie.Value); 
 user2_lbl.Text = fat.Name; 

在谷歌搜索这个问题时,我发现有些人说必须向 global.asax 添加一些东西,而其他人则说没有必要。

目标是能够登录身份验证子域,并使用户身份可从根站点和其他子域访问。机器 key 在所有 web.config 中匹配,并且 AppSettings["domainName"] 当前设置为 "mysite.com"。

有谁知道是什么阻止我访问用户信息?

请您参考如下方法:

将“mysite.com”更改为“.mysite.com”(注意前导“.”)。

这将告诉浏览器该 cookie 对子域也有效。