作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
在我的 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/
我是一名优秀的程序员,十分优秀!