So here's a break down of my issue in steps:
下面是我的问题的分解步骤:
- The user logs in with Google.
- 用户使用谷歌登录。
- On the login callback, information about the user is gathered
- 在登录回调中,将收集关于用户的信息
- Roles are assigned based on the user
- 角色是根据用户分配的
- A FormsAuthenticationTicket is created which passes the user/roles to the Application_PostAuthenticateRequest in Global.asax
- 创建一个FormsAuthenticationTicket,它将用户/角色传递给Global.asax中的Application_PostAuthenticateRequest
- In that request a GenericPrinciple is created from the authentication ticket and roles
- 在该请求中,从身份验证票据和角色创建一个通用原则
- HttpContext.Current.User is set to the above variable
- HttpContext.Current。User设置为上述变量
Now my question is, now that I set who the current user is that is using the website, how can I reference them? After the post authenticate complete, I check the current user and it's null. Should I be setting the principle to a different variable other than HttpContext.Current.User?
现在我的问题是,既然我设置了使用网站的当前用户是谁,我如何引用他们?完成post验证后,我检查当前用户,它为null。我应该将原则设置为HttpContext.Current.User之外的其他变量吗?
Callback
回调
public async Task<ActionResult> ExternalLoginCallback(string returnUrl)
{
...
// Get roles for current user
string roles = "bob,adminbob,cthulu";
FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(
1,
loginInfo.Email,
DateTime.Now,
DateTime.Now.AddMinutes(30), // value of time out property
false,
roles,
FormsAuthentication.FormsCookiePath);
HttpCookie authCookie = new HttpCookie(FormsAuthentication.FormsCookieName, FormsAuthentication.Encrypt(ticket));
Response.Cookies.Add(authCookie);
...
}
Post Authenticate in Global.asax
后验证Global.asax
protected void Application_PostAuthenticateRequest(Object sender, EventArgs e) {
HttpCookie authCookie = Context.Request.Cookies[FormsAuthentication.FormsCookieName];
if (authCookie != null) {
FormsAuthenticationTicket authTicket = FormsAuthentication.Decrypt(authCookie.Value);
string[] userRoles = authTicket.UserData.Split(new Char[] { ',' });
GenericPrincipal userPrincipal = new GenericPrincipal(new GenericIdentity(authTicket.Name), userRoles);
HttpContext.Current.User = userPrincipal; //How do I reference this in the program?
}
}
1 个解决方案
#1
1
Nevermind I figured out why it was returning blank. When I was calling the method to check the Name, the Identity wasn't authenticated yet. If you try to get the name without it being authenticated, it returns blank.
没关系,我弄明白了为什么它是空白的。当我调用这个方法来检查名称时,身份还没有被验证。如果您试图在未经身份验证的情况下获取该名称,它将返回空。
So I just simply checked with:
所以我只是检查一下
HttpContext.Current.User.Identity.IsAuthenticated
#1
1
Nevermind I figured out why it was returning blank. When I was calling the method to check the Name, the Identity wasn't authenticated yet. If you try to get the name without it being authenticated, it returns blank.
没关系,我弄明白了为什么它是空白的。当我调用这个方法来检查名称时,身份还没有被验证。如果您试图在未经身份验证的情况下获取该名称,它将返回空。
So I just simply checked with:
所以我只是检查一下
HttpContext.Current.User.Identity.IsAuthenticated