在我的ASP.NET Web API项目中,我使用的是承载令牌授权,我已经添加了一些自定义声明,如下所示:
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()));
解决方法
当然,在受保护的控制器中,您可以执行以下操作:
ClaimsPrincipal principal = Request.GetRequestContext().Principal as ClaimsPrincipal; var customClaimValue = principal.Claims.Where(c => c.Type == "CompanyID").Single().Value;