我需要能够在应用程序控制器中随时向用户添加/删除声明 . 这怎么可能,步骤是什么?
我正在使用ASP.NET CORE RC1并使用 Microsoft.AspNet.Authorization .
例如:
public IActionResult ChangeUserInfo(int id, User user)
{
var mUser = db.Users.Where(d => d.UserId == id).First();
mUser.Email = user.Email;
// change claims, How?
db.SaveChanges();
return View();
}
编辑:
这是我为获取其他声明所做的一种方法,将它们添加到claimsidentity并将用户注销,然后再次登录用户,但它不起作用 . (如果用户手动注销,则有效)
public async Task SingInAsync(HttpContext httpContext, string rememberme, bool forceSingOut = false, List additionalClaims = null)
{
var userIdentity = new ClaimsIdentity("SecureLogin");
userIdentity.AddClaims(_claims);
if(additionalClaims != null)
{
userIdentity.AddClaims(additionalClaims);
}
var userPrincipal = new ClaimsPrincipal(userIdentity);
if(forceSingOut)
{
await httpContext.Authentication.SignOutAsync("Cookie");
}
await httpContext.Authentication.SignInAsync("Cookie", userPrincipal,
new AuthenticationProperties
{
ExpiresUtc = (rememberme != null) ? DateTime.UtcNow.AddDays(int.Parse(_config["AppOptions:RememberMeDuration"])) : DateTime.UtcNow.AddMinutes(20),
IsPersistent = false,
AllowRefresh = false,
});
return null;
}
然后它被称为:
await customUserManager.SingInAsync(HttpContext, null, true, additClaims);