我在我的rc1项目中做到了:
User.Claims.ElementAt(#).Value
但是,当我切换到rtm之后,它将不再起作用。当我调试Razor视图时,对象看起来相同,但User.Claims只是空的。不知道原因可能是什么。
假设您拥有当前委托人的债权。在您的Razor视图中:
@((ClaimsIdentity) User.Identity)
这将使您可以访问当前用户的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;
}
然后,您可以通过以下方式访问所需的任何声明:
@((ClaimsIdentity) User.Identity).GetSpecificClaim("someclaimtype")
希望这可以帮助。
在razor视图中快速搜索索赔身份也提出了类似的问题和答案:MVC 5访问索赔身份用户数据
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句