2
我有一個使用Asp.NET-Identity 2.0(代碼優先)進行身份驗證的Web API 2.1。如何使用聲明
我的問題是更新/刪除用戶聲明,AuthenticationType是「承載」。我有一個稱爲「實例」的聲明,我想更新它。我有一個從OAuthAuthorizationServerProvider派生的authprovider和我重寫GrantResourceOwnerCredentials所以它看起來像這樣:
var user = await userManager.FindAsync(context.UserName, context.Password);
var identity = new ClaimsIdentity(new[]
{
new Claim(ClaimTypes.Name, user.UserName)
},
context.Options.AuthenticationType,
ClaimTypes.Name,
ClaimTypes.Role);
identity.AddClaim(new Claim(ClaimTypes.NameIdentifier, user.Id));
identity.AddClaim(new Claim(ClaimTypes.Instances, "test"));
var ticket = new AuthenticationTicket(identity, new AuthenticationProperties());
context.Validated(ticket);
然後在我UserController的我有一個函數來更新要求「實例」。
var user = (ClaimsIdentity)User.Identity;
var insClaim = user.Claims.Single(x => x.Type == ClaimTypes.Instances);
user.RemoveClaim(insClaim);
user.AddClaim(new Claim(ClaimTypes.Instances, "TEST 123"));
var ctx = Request.GetOwinContext();
var authCtx = await ctx.Authentication.AuthenticateAsync(user.AuthenticationType);
if (authCtx != null)
{
ctx.Authentication.SignOut(user.AuthenticationType);
ctx.Authentication.AuthenticationResponseGrant = new AuthenticationResponseGrant(user, authCtx.Properties);
ctx.Authentication.SignIn(user);
}
return Ok(new { });
但下一次我試圖從用戶那裏得到「實例」它仍然有值「測試」。我錯過了什麼?