asp.net – 从承载令牌(Web API)获取自定义声明值
发布时间:2020-05-24 05:39:30 所属栏目:asp.Net 来源:互联网
导读:在我的ASP.NET Web API项目中,我使用的是承载令牌授权,我已经添加了一些自定义声明,如下所示: var authType = AuthConfig.OAuthOptions.AuthenticationType;var identity = new ClaimsIdentity(authType);identity.AddClaim(new Claim(ClaimTypes.Nam
|
在我的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-core-mvc – EntityFramework 7(EF7)迁移 DbContex
- asp.net-mvc – 如何使用编辑器内部的foreach
- asp.net-mvc – 在RedirectToRoute和RedirectToAction的ASP
- asp.net – IIS 7.5和ASP .NET 2.0
- vbscript – 服务器端注释:ASP Classic中相当于% - - %的
- asp.net-mvc – MVC视图上的图像显示
- 学习Asp.Net WebForms或Asp.Net MVC
- asp.net-mvc – Href和Url.Content有什么区别?
- asp.net-mvc – 从Ajax表单帖子中重定向
- ASP.NET 5/ASP.NET Core中的关注点和n层架构的分离1
推荐文章
站长推荐
- ASP.NET MVC Model绑定的简单应用
- asp.net-mvc – 我可以从HttpContext获取控制器吗
- .net – 对象数据源或代码隐藏:哪个更好?
- asp.net-mvc – 编译时mvc视图检查与msbuild
- asp.net-mvc – 未在ELMAH中记录的错误
- asp.net-mvc – 如何扩展html.textboxfor以删除n
- asp.net-mvc – 如何覆盖Orchard CMS中导航区域的
- asp.net – HTTP错误403 – 禁止
- ASP.NET linkbutton两次提高onBeforeUnload事件
- asp-classic – 如何在经典ASP中将数据POST到远程
热点阅读
