我用谷歌搜索了这件事很久了,但还是得不到正确的答案。我正在使用身份声明进行用户身份验证,并且我的_Layout.cshtml页面中需要一些声明值。但是,我可以检索自定义身份声明值,但不能内置这些值。
我在这里设置了我的身份:
var ident = new ClaimsIdentity(
new[] {
// adding following 2 claim just for supporting default antiforgery provider
new Claim(ClaimTypes.NameIdentifier, user.LoginId),
new Claim("http://schemas.microsoft.com/accesscontrolservice/2010/07/claims/identityprovider", "ASP.NET Identity", "http://www.w3.org/2001/XMLSchema#string"),
new Claim(ClaimTypes.Name,user.UserName),
new Claim(ClaimTypes.Sid,user.UserId.ToString()),
new Claim("OperationType", user.OperationType.ToString()),
new Claim("ImageLink", user.ImageLink.ToString())
},
DefaultAuthenticationTypes.ApplicationCookie);
var claimsPrincipal = new ClaimsPrincipal(ident);
// Set current principal
Thread.CurrentPrincipal = claimsPrincipal;
和我的布局页面代码:
@using System.Security.Claims;
...........
@{
string userDesignation = ((ClaimsIdentity)User.Identity).FindFirst("OperationType").Value; ;
string userImage = ((ClaimsIdentity)User.Identity).FindFirst("ImageLink").Value; ;
}
我可以检索我的自定义声明(ImageLink,OperationType)值,但不能检索具有相同模式的(Name,Sid)。我找到的一些答案是关于扩展方法的。这是检索值唯一方法还是有其他方法?提前感谢
发布于 2018-09-14 01:17:07
请在布局页面顶部使用线程命名空间
@using System.Threading;
并访问您的索赔类型,如下所示
@{
string userDesignation = ((ClaimsIdentity)User.Identity).FindFirst("OperationType").Value;
string userImage = ((ClaimsIdentity)User.Identity).FindFirst("ImageLink").Value;
var identity = (ClaimsPrincipal)Thread.CurrentPrincipal;
// Get the claims values
var id= identity.Claims.Where(c => c.Type == ClaimTypes.Sid)
.Select(c => c.Value).SingleOrDefault();
var s = id;
//so on.......
}
发布于 2018-08-11 03:12:50
您可以使用html.RenderPartial()从您的布局中调用的方法来创建LayoutController (来自下面@Erik Philips的注释)。
TextInfo myTI = new CultureInfo("en-US", false).TextInfo;
var claimsIdentity = HttpContext.Current.User.Identity as System.Security.Claims.ClaimsIdentity;
var displayNameClaim = (claimsIdentity == null) ? null : claimsIdentity.Claims.SingleOrDefault(x => x.Type == ApplicationUser.DisplayNameClaimType);
var nameToDisplay = (displayNameClaim == null) ? HttpContext.Current.User.Identity.Name : displayNameClaim.Value;
var nameToDisplayTitle = myTI.ToTitleCase(nameToDisplay);
在视图中,Name值被注入到<span class="NameDisplay"></span>
中。
$(".NameDisplay").html(@Html.Raw("\"" + nameToDisplayTitle + "\""));
在IdentityModels.cs
中,我定义了以下字段:
public class ApplicationUser : IdentityUser
{
public const string DisplayNameClaimType = "FirstName";
[Display(Name = "First Name")]
public string FirstName { get; set; }
//etc.
}
public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<ApplicationUser> manager)
{
// Note the authenticationType must match the one defined in CookieAuthenticationOptions.AuthenticationType
var userIdentity = await manager.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie);
// Add custom user claims here
userIdentity.AddClaim(new Claim(DisplayNameClaimType, FirstName));
return userIdentity;
}
https://stackoverflow.com/questions/51792577
复制相似问题