2

認証に Asp.NET-Identity 2.0 (コードファースト) を使用している Web API 2.1 があります。

私の問題は、ユーザー クレームを更新/削除することです。AuthenticationType は「Bearer」です。「インスタンス」というクレームがあり、それを更新したいと考えています。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 { });

しかし、次にユーザーから「インスタンス」を取得しようとすると、値は「テスト」のままです。私は何が欠けていますか?

4

1 に答える 1