首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用索赔

如何使用索赔
EN

Stack Overflow用户
提问于 2014-05-21 09:46:56
回答 1查看 1.2K关注 0票数 2

我有一个WebAPI2.1,它使用Asp.NET-Identity 2.0 (代码优先)进行身份验证。

我的问题是升级/删除一个用户声明,AuthenticationType是"Bearer“。我有一个名为“实例”的声明,我想更新它。我有一个从GrantResourceOwnerCredentials派生的authprovider,我重写了这个OAuthAuthorizationServerProvider,所以它看起来如下所示:

代码语言:javascript
复制
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中,我有一个函数来更新声明“实例”。

代码语言:javascript
复制
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 { });

但是下一次我试图从用户那里获得“实例”时,它仍然具有"test“的值。我遗漏了什么?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-03-18 21:27:20

您不能修改与令牌“相关”的声明。令牌基于声明和服务器machine.config。

最好的解决方案是使用刷新令牌。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/23779593

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档