所以,这是我的问题分解步骤:
现在我的问题是,现在我设置了当前用户使用网站的用户,我该如何引用它们?在验证完成后,我检查当前用户并且它为空。我应该将原则设置为除HttpContext.Current.User以外的其他变量吗?
回调
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);
...
}
在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?
}
}
答案 0 :(得分:1)
没关系我弄明白为什么它会变回空白。当我调用方法来检查名称时,身份尚未经过身份验证。如果您尝试获取未经过身份验证的名称,则返回空白。
所以我只是简单地检查:
HttpContext.Current.User.Identity.IsAuthenticated