Skn*_*cht 15 asp.net-mvc rtm claims razor .net-core-rc1
我在我的rc1项目中做过:
User.Claims.ElementAt(#).Value
Run Code Online (Sandbox Code Playgroud)
但是在我切换到rtm后它就不再起作用了.当我调试Razor视图时,对象看起来相同,但User.Claims只是空的.不知道原因是什么.
小智 24
假设您已向当前委托人提出索赔.在你的Razor视图中:
@((ClaimsIdentity) User.Identity)
Run Code Online (Sandbox Code Playgroud)
这将使您可以访问当前用户的ClaimsIdentity.为了使您的声明保持清洁,您可能需要创建一种搜索声明的扩展方法.
public static string GetSpecificClaim(this ClaimsIdentity claimsIdentity, string claimType)
{
var claim = claimsIdentity.Claims.FirstOrDefault(x => x.Type == claimType);
return (claim != null) ? claim.Value : string.Empty;
}
Run Code Online (Sandbox Code Playgroud)
然后你可以访问你想要的任何声明:
@((ClaimsIdentity) User.Identity).GetSpecificClaim("someclaimtype")
Run Code Online (Sandbox Code Playgroud)
希望这可以帮助.
在剃刀视图中快速搜索声明标识提出了类似的问题和答案: MVC 5访问声明身份用户数据
小智 9
在 .net core 2.2 的 razor 页面中测试:
@User.FindFirst("nameOfClaim").Value
归档时间: |
|
查看次数: |
12855 次 |
最近记录: |