如何在 Razor View 中获得 Role.Name?
How to get Role.Name in Razor View?
我正在使用 @model IEnumerable<WebApplication.Models.ApplicationUser>
查看
@foreach (var user in Model)
{
<tr>
<td>
@foreach(var role in user.Roles){
role.Name; //invalid
role.RoleId; //valid
role.UserId; //valid
}
</td>
</tr>
}
型号
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;
}
}
我可以从这个角色中得到 RoleID 和 UserId,但是我怎样才能得到 Role.Name?
IdentityUser [来自元数据]
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
中的 IdentityUserRole
,那么您将无权访问角色名称。您可以从 IdentityUserRole<TKey>
实现自己的角色并设置角色名称或通过查询 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>
}
我能找到在 razor 布局视图中获取角色的唯一方法是从请求中获取 UserManager
。
剃刀视图:
@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];
}
我正在使用 @model IEnumerable<WebApplication.Models.ApplicationUser>
查看
@foreach (var user in Model)
{
<tr>
<td>
@foreach(var role in user.Roles){
role.Name; //invalid
role.RoleId; //valid
role.UserId; //valid
}
</td>
</tr>
}
型号
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;
}
}
我可以从这个角色中得到 RoleID 和 UserId,但是我怎样才能得到 Role.Name?
IdentityUser [来自元数据]
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
中的 IdentityUserRole
,那么您将无权访问角色名称。您可以从 IdentityUserRole<TKey>
实现自己的角色并设置角色名称或通过查询 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>
}
我能找到在 razor 布局视图中获取角色的唯一方法是从请求中获取 UserManager
。
剃刀视图:
@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];
}