C# Web API 更新声明

dul*_*eme 5 c# asp.net claims-based-identity asp.net-web-api asp.net-web-api2

我制作 asp.net web api 项目。
客户端程序是winform使用HttpClient。
当用户登录时,我将用户语言分配给声明。

public override async Task GrantResourceOwnerCredentials(OAuthGrantResourceOwnerCredentialsContext context)
{
    .......
    var identity = new ClaimsIdentity(context.Options.AuthenticationType);
                identity.AddClaim(new Claim("UserId", context.UserName));
                identity.AddClaim(new Claim("role", "user"));
                identity.AddClaim(new Claim("Language", forms["language"]));
    context.Validated(identity);
   ......
}
Run Code Online (Sandbox Code Playgroud)

我需要更改索赔运行时的语言。所以我做了更改语言API。

    [HttpGet]
    [APIAuthorize]
    public HttpResponseMessage ChangeLanguage(Language language)
    {
        User.AddUpdateClaim("Language", language.ToString());

        return Request.CreateResponse<IResult>(HttpStatusCode.OK, "OK");
    }
Run Code Online (Sandbox Code Playgroud)

并进行 ClaimsIdentity 扩展。

public static class ClaimsIdentityExtensions
{
    public static void AddUpdateClaim(this IPrincipal currentPrincipal, string key, string value)
    {
        var identity = new ClaimsIdentity(currentPrincipal.Identity);            
        // check for existing claim and remove it
        var existingClaim = identity.FindFirst(key);
        if (existingClaim != null)
            identity.RemoveClaim(existingClaim);
        // add new claim
        identity.AddClaim(new Claim(key, value));

        var authenticationManager = HttpContext.Current.GetOwinContext().Authentication;
        authenticationManager.AuthenticationResponseGrant = new AuthenticationResponseGrant(new ClaimsPrincipal(identity), new AuthenticationProperties() { IsPersistent = true });                                 

        HttpContext.Current.GetOwinContext().Authentication.SignOut(DefaultAuthenticationTypes.ExternalBearer);
        HttpContext.Current.GetOwinContext().Authentication.SignIn(new AuthenticationProperties() { IsPersistent = true }, identity);

        ClaimsPrincipal principal = Thread.CurrentPrincipal as ClaimsPrincipal;
        if (principal != null)
        {
            identity = principal.Identities.ElementAt(0);
            var old = identity.FindFirst(key);
            if (old != null)
                identity.RemoveClaim(old);

            identity.AddClaim(new Claim(key, value));
        }
    }

    public static string GetClaimValue(this IPrincipal currentPrincipal, string key)
    {
        var identity = currentPrincipal.Identity as ClaimsIdentity;
        if (identity == null)
            return null;

        var claim = identity.Claims.First(c => c.Type == key);
        return claim.Value;
    }
}
Run Code Online (Sandbox Code Playgroud)

然后客户端调用“ChangeLanguage”api

    [HttpGet]
    [APIAuthorize]
    public HttpResponseMessage ChangeLanguage(Language language)
    {
        User.AddUpdateClaim("Language", language.ToString());

        return Request.CreateResponse<IResult>(HttpStatusCode.OK, "OK");
    }
Run Code Online (Sandbox Code Playgroud)

但下次调用的控制器构造函数
声称未更新。

    public MyController()
    {
        var user = this.User.Identity as ClaimsIdentity;
        if (user.IsAuthenticated)
        {
            // language not changed
            var language = User.GetClaimValue("Language");
        }
    }
Run Code Online (Sandbox Code Playgroud)

我如何更改索赔?抱歉我的英语很糟糕。

谢谢