C# 如何在Razor视图中获取Role.Name?
我使用的是C# 如何在Razor视图中获取Role.Name?,c#,asp.net-mvc,razor,asp.net-mvc-5,asp.net-identity-2,C#,Asp.net Mvc,Razor,Asp.net Mvc 5,Asp.net Identity 2,我使用的是@model IEnumerable 查看 @foreach (var user in Model) { <tr> <td> @foreach(var role in user.Roles){ role.Name; //invalid role.RoleId; //valid role.UserId; //valid
@model IEnumerable
查看
@foreach (var user in Model)
{
<tr>
<td>
@foreach(var role in user.Roles){
role.Name; //invalid
role.RoleId; //valid
role.UserId; //valid
}
</td>
</tr>
}
@foreach(模型中的var用户)
{
@foreach(user.Roles中的var角色){
role.Name;//无效
role.RoleId;//有效
role.UserId;//有效
}
}
型号
public class ApplicationUser : IdentityUser
{
[Required]
public string FirstName { get; set; }
[Required]
public string LastName { get; set; }
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
return userIdentity;
}
}
公共类应用程序用户:IdentityUser
{
[必需]
公共字符串名{get;set;}
[必需]
公共字符串LastName{get;set;}
公共异步任务GenerateUserIdentityAsync(用户管理器)
{
//注意authenticationType必须与CookieAuthenticationOptions.authenticationType中定义的类型匹配
var userIdentity=wait manager.CreateIdentityAsync(这是DefaultAuthenticationTypes.ApplicationOkie);
//在此处添加自定义用户声明
返回用户身份;
}
}
我可以从这个角色中获取RoleID和UserId,但如何获取role.Name
标识用户[来自元数据]
namespace Microsoft.AspNet.Identity.EntityFramework
{
// Summary:
// Default EntityFramework IUser implementation
//
// Type parameters:
// TKey:
//
// TLogin:
//
// TRole:
//
// TClaim:
public class IdentityUser<TKey, TLogin, TRole, TClaim> : IUser<TKey>
where TLogin : global::Microsoft.AspNet.Identity.EntityFramework.IdentityUserLogin<TKey>
where TRole : global::Microsoft.AspNet.Identity.EntityFramework.IdentityUserRole<TKey>
where TClaim : global::Microsoft.AspNet.Identity.EntityFramework.IdentityUserClaim<TKey>
{
// Summary:
// Constructor
public IdentityUser();
// Summary:
// Used to record failures for the purposes of lockout
public virtual int AccessFailedCount { get; set; }
//
// Summary:
// Navigation property for user claims
public virtual ICollection<TClaim> Claims { get; }
//
// Summary:
// Email
public virtual string Email { get; set; }
//
// Summary:
// True if the email is confirmed, default is false
public virtual bool EmailConfirmed { get; set; }
//
// Summary:
// User ID (Primary Key)
public virtual TKey Id { get; set; }
//
// Summary:
// Is lockout enabled for this user
public virtual bool LockoutEnabled { get; set; }
//
// Summary:
// DateTime in UTC when lockout ends, any time in the past is considered not
// locked out.
public virtual DateTime? LockoutEndDateUtc { get; set; }
//
// Summary:
// Navigation property for user logins
public virtual ICollection<TLogin> Logins { get; }
//
// Summary:
// The salted/hashed form of the user password
public virtual string PasswordHash { get; set; }
//
// Summary:
// PhoneNumber for the user
public virtual string PhoneNumber { get; set; }
//
// Summary:
// True if the phone number is confirmed, default is false
public virtual bool PhoneNumberConfirmed { get; set; }
//
// Summary:
// Navigation property for user roles
public virtual ICollection<TRole> Roles { get; }
//
// Summary:
// A random value that should change whenever a users credentials have changed
// (password changed, login removed)
public virtual string SecurityStamp { get; set; }
//
// Summary:
// Is two factor enabled for the user
public virtual bool TwoFactorEnabled { get; set; }
//
// Summary:
// User name
public virtual string UserName { get; set; }
}
}
名称空间Microsoft.AspNet.Identity.EntityFramework
{
//总结:
//默认EntityFramework IUser实现
//
//类型参数:
//TKey:
//
//特洛金:
//
//特罗尔:
//
//目标:
公共类标识用户:IUser
其中TLogin:global::Microsoft.AspNet.Identity.EntityFramework.IdentityUserLogin
其中TRole:global::Microsoft.AspNet.Identity.EntityFramework.IdentityUserRole
其中TClaim:global::Microsoft.AspNet.Identity.EntityFramework.IdentityUserClaim
{
//总结:
//建造师
公共身份识别器();
//总结:
//用于记录故障,以便锁定
公共虚拟int AccessFailedCount{get;set;}
//
//总结:
//用户声明的导航属性
公共虚拟ICollection声明{get;}
//
//总结:
//电子邮件
公共虚拟字符串电子邮件{get;set;}
//
//总结:
//如果电子邮件已确认,则默认为false
公共虚拟布尔值{get;set;}
//
//总结:
//用户ID(主键)
公共虚拟密钥Id{get;set;}
//
//总结:
//是否为此用户启用了锁定
公共虚拟bool LockoutEnabled{get;set;}
//
//总结:
//锁定结束时,不考虑过去的任何时间
//锁在外面。
公共虚拟日期时间?LockoutEndDateUtc{get;set;}
//
//总结:
//用户登录的导航属性
公共虚拟ICollection登录名{get;}
//
//总结:
//用户密码的加盐/哈希形式
公共虚拟字符串密码哈希{get;set;}
//
//总结:
//用户的电话号码
公共虚拟字符串PhoneNumber{get;set;}
//
//总结:
//如果确认了电话号码,则默认为false
公共虚拟bool phonenumberconfirm{get;set;}
//
//总结:
//用户角色的导航属性
公共虚拟ICollection角色{get;}
//
//总结:
//当用户凭据发生更改时,应更改的随机值
//(密码已更改,登录名已删除)
公共虚拟字符串SecurityStamp{get;set;}
//
//总结:
//是否为用户启用了双因素
公共虚拟布尔TwoFactorEnabled{get;set;}
//
//总结:
//用户名
公共虚拟字符串用户名{get;set;}
}
}
如果角色类型是Microsoft.AspNet.Identity.EntityFramework中的IdentityUserRole
,则您将无权访问角色名称。您可以从IdentityUserRole
实现自己的角色,并通过查询EF存储设置角色名称或直接加载角色。如果角色类型是IdentityUserRole
fromMicrosoft.AspNet.Identity.EntityFramework
中的IdentityUserRole,则您将无权访问角色名称。您可以从IdentityUserRole
实现自己的角色,并通过查询EF存储设置角色名称或直接加载角色。作为Charlie答案的扩展,您可以通过获取Roles
集合并将其传递到视图模型中来实现这一点
@foreach (var user in Model)
{
<tr>
<td>
@foreach(var role in user.Roles){
Model.Roles.First(x=>x.RoleID == role.RoleID).Name;
role.RoleId; //valid
role.UserId; //valid
}
</td>
</tr>
}
@foreach(模型中的var用户)
{
@foreach(user.Roles中的var角色){
Model.Roles.First(x=>x.RoleID==role.RoleID).Name;
role.RoleId;//有效
role.UserId;//有效
}
}
作为Charlie答案的扩展,实现这一点的一种方法是获取角色集,并将其传递到视图模型中
@foreach (var user in Model)
{
<tr>
<td>
@foreach(var role in user.Roles){
Model.Roles.First(x=>x.RoleID == role.RoleID).Name;
role.RoleId; //valid
role.UserId; //valid
}
</td>
</tr>
}
@foreach(模型中的var用户)
{
@foreach(user.Roles中的var角色){
Model.Roles.First(x=>x.RoleID==role.RoleID).Name;
role.RoleId;//有效
role.UserId;//有效
}
}
我能找到的在razor布局视图中获取角色的唯一方法是从请求中获取UserManager
Razor视图:
@using Microsoft.AspNet.Identity
@using Microsoft.AspNet.Identity.Owin;
@if (Request.IsAuthenticated) {
var userManager = Request.GetOwinContext().GetUserManager<ApplicationUserManager>();
var userRoles = userManager.GetRoles(User.Identity.GetUserId());
var role = userRoles[0];
}
@使用Microsoft.AspNet.Identity
@使用Microsoft.AspNet.Identity.Owin;
@如果(请求已验证){
var userManager=Request.GetOwinContext().GetUserManager();
var userRoles=userManager.GetRoles(User.Identity.GetUserId());
var-role=userRoles[0];
}
我能找到的在razor布局视图中获取角色的唯一方法是从请求中获取UserManager
Razor视图:
@using Microsoft.AspNet.Identity
@using Microsoft.AspNet.Identity.Owin;
@if (Request.IsAuthenticated) {
var userManager = Request.GetOwinContext().GetUserManager<ApplicationUserManager>();
var userRoles = userManager.GetRoles(User.Identity.GetUserId());
var role = userRoles[0];
}
@使用Microsoft.AspNet.Identity
@使用Microsoft.AspNet.Identity