什么时候可以理赔
When claims are available
我在 GenerateUserIdentityAsync 方法中添加声明:
public class ApplicationUser : IdentityUser
{
public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<ApplicationUser> manager)
{
var userIdentity = await manager.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie);
userIdentity.AddClaim(new Claim(ClaimsStaticStrings.Inactivity, company.Inactivity.ToString()));
return userIdentity;
}
}
然后我尝试用Account/Login方法获取:
[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public async Task<ActionResult> Login(LoginViewModel model, string returnUrl)
{
var result = await SignInManager.PasswordSignInAsync(model.Username, model.Password, model.RememberMe, shouldLockout: false);
switch (result)
{
case SignInStatus.Success:
int inactivity = Utils.GetInactivityFromIdentity(User.Identity);
Response.Cookies.Add(new HttpCookie("inactivity", inactivity.ToString()));
return RedirectToAction("Index", "Home");
}
}
public static int GetInactivityFromIdentity(IIdentity identity)
{
System.Security.Claims.ClaimsIdentity claims = (System.Security.Claims.ClaimsIdentity)identity;
var claim = claims.FindFirst(Models.ClaimsStaticStrings.Inactivity);
if (claim != null)
{
return int.Parse(claim.Value);
}
else
throw new Exception("Inactivity is not set");
}
它抛出异常 "Inactivity is not set"。变量 'claims' 只有一个声明 - name
但是当我从任何其他页面(重定向后)调用 GetInactivityFromIdentity 方法时 - 它工作正常(并且声明中充满了所有设置的声明)。为什么会这样?
声明被序列化到 auth-cookie 中。在您通过身份验证重新加载页面之前,不会设置 Cookie。在您尝试从 cookie 访问声明时,HTTP 请求中没有 cookie - SignInManager
将仅在请求完成时设置 cookie,但不会在请求完成后立即设置。您确实需要 redirect/page 重新加载周期才能获得可用的 cookie 和声明。
您必须以某种方式获得 inactivity
价值,而不是通过声明,而是在您登录用户时从您的数据存储中获得。
我在 GenerateUserIdentityAsync 方法中添加声明:
public class ApplicationUser : IdentityUser
{
public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<ApplicationUser> manager)
{
var userIdentity = await manager.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie);
userIdentity.AddClaim(new Claim(ClaimsStaticStrings.Inactivity, company.Inactivity.ToString()));
return userIdentity;
}
}
然后我尝试用Account/Login方法获取:
[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public async Task<ActionResult> Login(LoginViewModel model, string returnUrl)
{
var result = await SignInManager.PasswordSignInAsync(model.Username, model.Password, model.RememberMe, shouldLockout: false);
switch (result)
{
case SignInStatus.Success:
int inactivity = Utils.GetInactivityFromIdentity(User.Identity);
Response.Cookies.Add(new HttpCookie("inactivity", inactivity.ToString()));
return RedirectToAction("Index", "Home");
}
}
public static int GetInactivityFromIdentity(IIdentity identity)
{
System.Security.Claims.ClaimsIdentity claims = (System.Security.Claims.ClaimsIdentity)identity;
var claim = claims.FindFirst(Models.ClaimsStaticStrings.Inactivity);
if (claim != null)
{
return int.Parse(claim.Value);
}
else
throw new Exception("Inactivity is not set");
}
它抛出异常 "Inactivity is not set"。变量 'claims' 只有一个声明 - name
但是当我从任何其他页面(重定向后)调用 GetInactivityFromIdentity 方法时 - 它工作正常(并且声明中充满了所有设置的声明)。为什么会这样?
声明被序列化到 auth-cookie 中。在您通过身份验证重新加载页面之前,不会设置 Cookie。在您尝试从 cookie 访问声明时,HTTP 请求中没有 cookie - SignInManager
将仅在请求完成时设置 cookie,但不会在请求完成后立即设置。您确实需要 redirect/page 重新加载周期才能获得可用的 cookie 和声明。
您必须以某种方式获得 inactivity
价值,而不是通过声明,而是在您登录用户时从您的数据存储中获得。