[英]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.