asp.net – 从承载令牌(Web API)获取自定义声明值
发布时间:2020-12-15 22:55:53 所属栏目:asp.Net 来源:网络整理
导读:在我的ASP.NET Web API项目中,我使用的是承载令牌授权,我已经添加了一些自定义声明,如下所示: var authType = AuthConfig.OAuthOptions.AuthenticationType;var identity = new ClaimsIdentity(authType);identity.AddClaim(new Claim(ClaimTypes.Name,vm.U
在我的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; (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
推荐文章
站长推荐
- asp.net-mvc – ASP.NET MVC 5身份应用程序用户作
- asp.net – 人们如何调用aspx文件以区别于aspx.c
- asp.net-mvc – jQuery脚本包含在mvc 4模板的页面
- asp.net-mvc-4 – 请求太大
- ListBox中的选定项目是否有ASP.NET集合?
- asp.net – 如果我将ValidateRequest设置为false
- asp.net-web-api – WEB API OnAuthorization被称
- asp.net – App_Offline.htm没有导致网站崩溃的可
- asp.net – 如何在EnableSessionState =“False”
- asp.net-mvc-4 – 如何在Razor MVC4中创建DateTi
热点阅读