简体   繁体   English

HttpContext.Current.User.Identity.Name始终为null

[英]HttpContext.Current.User.Identity.Name is always null

I know this question has been asked before but each question has a different scenario to what I have or I am missing the obvious. 我知道之前曾有人问过这个问题,但每个问题都有与我所遇到的情况不同的情况,或者我错过了明显的情况。

So I have implemented SSO architecture, so the user click on log in and redirected to identity server and log in there and get redirected back to the application. 因此,我已经实现了SSO体系结构,因此用户单击登录并重定向到身份服务器,然后在此处登录,然后重定向回到应用程序。

in one of my application I want to use the HttpContext.Current.User.Identity.Name in my membershipprovider , but it is always empty. 在我的一个应用程序中,我想在我的membershipprovider使用HttpContext.Current.User.Identity.Name ,但是它始终为空。 so I found out that I need create a cookie and set it up properly to get the HttpContext.Current.User.Identity.Name form wherever I want. 所以我发现我需要创建一个cookie并正确地设置它,以获取我想要的HttpContext.Current.User.Identity.Name表单。

My code look like this: 我的代码如下所示:

After the user is redirected from the Identity server to a login control: 将用户从身份服务器重定向到登录控件后:

if (!System.Web.HttpContext.Current.User.Identity.IsAuthenticated)
{
    FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(1,
                HttpContext.Current.Session["username"].ToString(),
                DateTime.Now, DateTime.Now.AddMinutes(30), true, String.Empty, 
                FormsAuthentication.FormsCookiePath);
    string encryptedCookie = FormsAuthentication.Encrypt(ticket);
    HttpCookie cookie = new HttpCookie(FormsAuthentication.FormsCookieName,
                        encryptedCookie);
    cookie.Expires = DateTime.Now.AddMinutes(30);
    Response.Cookies.Add(cookie);
    FormsAuthentication.RedirectFromLoginPage("/my-profile", true);
}

My web config file looks like this: 我的网络配置文件如下所示:

 <authentication mode="Forms">
     <forms domain="http://localhost:8081" name="yourAuthCookie" loginUrl="login.aspx" 
      protection="All" path="/" />
 </authentication>
 <authorization>
      <allow users="?" />
 </authorization>

after the user is redirected to the my profile page the System.Web.HttpContext.Current.User.Identity.IsAuthenticated is true and the AuthinticationType = Cookies. 将用户重定向到我的个人资料页面后, System.Web.HttpContext.Current.User.Identity.IsAuthenticated为true,并且AuthinticationType = Cookies。 but the Name is empty. 但名称为空。

I am using asp.net webforms. 我正在使用asp.net网络表单。

UPDATE: I have tried to set the HttpContext.Current.User it is set properly but when the page load or redirected it goes back to null empty. 更新:我试图设置HttpContext.Current.User它设置正确,但是当页面加载或重定向时,它会返回null为空。

  if (ticket != null && !ticket.Expired)
{
      var roles = ticket.UserData.Split(',');
     System.Web.HttpContext.Current.User = new   System.Security.Principal.GenericPrincipal(new FormsIdentity(ticket), roles);
  }

any help will be much appreciated. 任何帮助都感激不尽。

Your domain is wrong - in your app.config; 您的domain是错误的-在您的app.config中; <forms domain="http://localhost:8081" ... /> in this context domain has a meaning of domain into which the cookie is set. 在此上下文中, <forms domain="http://localhost:8081" ... />具有将cookie设置为的域的含义。 You can read more about domain of cookies here . 您可以在此处阅读有关cookie域的更多信息。

Domain is something like www.google.com or .google.com etc - it doesn't contain protocol prefix, it doesn't contain port and it makes no sense to use it for local hosts - so drop that attribute from your web.config for good. 域类似于www.google.com或.google.com等-它不包含协议前缀,不包含端口,将其用于本地主机没有任何意义-因此请从您的网络中删除该属性。配置好。

Also (and this has nothing to do with your issue, it's just a friendly suggestion) - you don't have to construct forms auth ticket when all you care about is username. 另外(这与您的问题无关,这只是一个友好的建议)-当您只关心用户名时,您不必构造表单身份验证票。 You can use just FormsAuthentication.SetAuthCookie instead. 您可以只使用FormsAuthentication.SetAuthCookie

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 模拟HttpContext.Current.User.Identity.Name - Mock HttpContext.Current.User.Identity.Name 更改HttpContext.Current.User.Identity.Name - Changing HttpContext.Current.User.Identity.Name 用户登录后更改HttpContext.Current.User.Identity.Name - Changing HttpContext.Current.User.Identity.Name after User is logged in 在Web Api 2中获取HttpContext.Current.User.Identity.Name null通过angular 4登录 - Getting HttpContext.Current.User.Identity.Name null in Web Api 2 log in through angular 4 在我的owin授权提供程序中的GrantResourceOwnerCredentials中设置自定义主体后,HttpContext.Current.User.Identity.Name为null - HttpContext.Current.User.Identity.Name is null after setting custom principal in GrantResourceOwnerCredentials in my owin authorization provider HttpContext.Current.User.Identity.Name返回DefaultAppPool,为什么? - HttpContext.Current.User.Identity.Name returns DefaultAppPool, why? 访问DbContext中的HttpContext.Current.User.Identity.Name - Accessing HttpContext.Current.User.Identity.Name in the DbContext HttpContext.Current.User.Identity.Name在.ashx处理程序中不起作用 - HttpContext.Current.User.Identity.Name not working in .ashx hander HttpContext.Current.User.Identity.Name无法转换 - HttpContext.Current.User.Identity.Name can't be converted HttpContext.Current.User.Identity.Name在Active Directory中的什么位置? - HttpContext.Current.User.Identity.Name where is it looking in Active directory?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM