gpt4 book ai didi

asp.net - 从不记名 token 获取自定义声明值(Web API)

转载 作者:行者123 更新时间:2023-12-02 04:43:27 26 4
gpt4 key购买 nike

在我的 ASP.NET Web API 项目中,我使用不记名 token 授权,并为其添加了一些自定义声明,如下所示:

var authType = AuthConfig.OAuthOptions.AuthenticationType;
var identity = new ClaimsIdentity(authType);
identity.AddClaim(new Claim(ClaimTypes.Name, vm.Username));

// custom claim
identity.AddClaim(new Claim("CompanyID", profile.CompanyId.ToString()));

有什么方法可以在 Controller 中访问这个额外的声明值,而无需额外访问数据库?

最佳答案

当然,在 protected Controller 中,您可以执行以下操作:

 ClaimsPrincipal principal = Request.GetRequestContext().Principal as ClaimsPrincipal;
var customClaimValue = principal.Claims.Where(c => c.Type == "CompanyID").Single().Value;

关于asp.net - 从不记名 token 获取自定义声明值(Web API),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26587316/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com