如何使用 IdentityUserRoles?
How do I use IdentityUserRoles?
我是 Razorpages 的新手,想在 IdentityUser 中包含用户的角色(我已经用 ApplicationUser 覆盖了自定义组名)。因此存在 table UserRoles,但我无法将 UserRoles 连接到 IdentityUser。
这将作为一个小型用户管理器来设置组(例如部门)和分配角色。
这是我要实现的目标:
<table class="table">
<thead>
<tr>
<th>
@Html.DisplayNameFor(model => model.ApplicationUser[0].UserName)
</th>
<th>
@Html.DisplayNameFor(model => model.ApplicationUser[0].Group)
</th>
<th>
@Html.DisplayNameFor(model => model.ApplicationUser[0].Role)
</th>
<th></th>
</tr>
</thead>
<tbody>
@foreach (var item in Model.ApplicationUser)
{
<tr>
<td>
@Html.DisplayFor(modelItem => item.UserName)
</td>
<td>
@Html.DisplayFor(modelItem => item.Group.Name)
</td>
<td>
@Html.DisplayFor(modelItem => item.Role.Name)
</td>
<td>
<a asp-page="./Edit" asp-route-id="@item.Id">Edit</a> |
<a asp-page="./Details" asp-route-id="@item.Id">Details</a> |
<a asp-page="./Delete" asp-route-id="@item.Id">Delete</a>
</td>
</tr>
}
</tbody>
感谢任何帮助 - 非常感谢!
在我的 ASP.NET 5.0 Blazor Server 项目中,使用 EF Core,我按照 Identity Model Customisation 中的指南最初创建为自定义 ApplicationUser class,它从 IdentityUser(我'我确定你在这里做了同样的事情)。在我的例子中,我还将 PK 类型更改为 GUID。
public class ApplicationUser : IdentityUser<Guid>
{
public virtual ICollection<IdentityUserClaim<Guid>> Claims { get; set; }
public virtual ICollection<IdentityUserLogin<Guid>> Logins { get; set; }
public virtual ICollection<IdentityUserToken<Guid>> Tokens { get; set; }
public virtual ICollection<ApplicationUserRole> UserRoles { get; set; }
与 IdentityUser 一样,我还必须为 IdentityRole 创建自定义 classes:
public class ApplicationRole : IdentityRole<Guid>
{
public virtual ICollection<ApplicationUserRole> UserRoles { get; set; }
...以及 IdentityUserRole classes:
public class ApplicationUserRole : IdentityUserRole<Guid>
{
public virtual ApplicationUser User { get; set; }
public virtual ApplicationRole Role { get; set; }
}
然后更新我的 ApplicationDBContext 以引用自定义 classes:
public class ApplicationDbContext : IdentityDbContext<
ApplicationUser, ApplicationRole, Guid,
IdentityUserClaim<Guid>, ApplicationUserRole, IdentityUserLogin<Guid>,
IdentityRoleClaim<Guid>, IdentityUserToken<Guid>>
{
最后,我必须在添加身份服务时注册自定义数据库上下文 class(在我的 Blazor 项目中,在 startup.cs 下)。
services.AddIdentity<ApplicationUser, ApplicationRole>()
.AddEntityFrameworkStores<ApplicationDbContext>()
.AddDefaultUI()
.AddDefaultTokenProviders();
这将解决您的问题:
I'm not able to connect UserRoles to IdentityUser.:
我现在能够像处理与任何其他模型的关系一样处理用户及其角色之间的关系(同时还使用 UserManager 和 RoleManager)。例如,要使用 UserManager 将用户分配给角色:await UserManager.AddToRoleAsync(ApplicationUserObject, "admin");
我是 Razorpages 的新手,想在 IdentityUser 中包含用户的角色(我已经用 ApplicationUser 覆盖了自定义组名)。因此存在 table UserRoles,但我无法将 UserRoles 连接到 IdentityUser。
这将作为一个小型用户管理器来设置组(例如部门)和分配角色。
这是我要实现的目标:
<table class="table">
<thead>
<tr>
<th>
@Html.DisplayNameFor(model => model.ApplicationUser[0].UserName)
</th>
<th>
@Html.DisplayNameFor(model => model.ApplicationUser[0].Group)
</th>
<th>
@Html.DisplayNameFor(model => model.ApplicationUser[0].Role)
</th>
<th></th>
</tr>
</thead>
<tbody>
@foreach (var item in Model.ApplicationUser)
{
<tr>
<td>
@Html.DisplayFor(modelItem => item.UserName)
</td>
<td>
@Html.DisplayFor(modelItem => item.Group.Name)
</td>
<td>
@Html.DisplayFor(modelItem => item.Role.Name)
</td>
<td>
<a asp-page="./Edit" asp-route-id="@item.Id">Edit</a> |
<a asp-page="./Details" asp-route-id="@item.Id">Details</a> |
<a asp-page="./Delete" asp-route-id="@item.Id">Delete</a>
</td>
</tr>
}
</tbody>
感谢任何帮助 - 非常感谢!
在我的 ASP.NET 5.0 Blazor Server 项目中,使用 EF Core,我按照 Identity Model Customisation 中的指南最初创建为自定义 ApplicationUser class,它从 IdentityUser(我'我确定你在这里做了同样的事情)。在我的例子中,我还将 PK 类型更改为 GUID。
public class ApplicationUser : IdentityUser<Guid>
{
public virtual ICollection<IdentityUserClaim<Guid>> Claims { get; set; }
public virtual ICollection<IdentityUserLogin<Guid>> Logins { get; set; }
public virtual ICollection<IdentityUserToken<Guid>> Tokens { get; set; }
public virtual ICollection<ApplicationUserRole> UserRoles { get; set; }
与 IdentityUser 一样,我还必须为 IdentityRole 创建自定义 classes:
public class ApplicationRole : IdentityRole<Guid>
{
public virtual ICollection<ApplicationUserRole> UserRoles { get; set; }
...以及 IdentityUserRole classes:
public class ApplicationUserRole : IdentityUserRole<Guid>
{
public virtual ApplicationUser User { get; set; }
public virtual ApplicationRole Role { get; set; }
}
然后更新我的 ApplicationDBContext 以引用自定义 classes:
public class ApplicationDbContext : IdentityDbContext<
ApplicationUser, ApplicationRole, Guid,
IdentityUserClaim<Guid>, ApplicationUserRole, IdentityUserLogin<Guid>,
IdentityRoleClaim<Guid>, IdentityUserToken<Guid>>
{
最后,我必须在添加身份服务时注册自定义数据库上下文 class(在我的 Blazor 项目中,在 startup.cs 下)。
services.AddIdentity<ApplicationUser, ApplicationRole>()
.AddEntityFrameworkStores<ApplicationDbContext>()
.AddDefaultUI()
.AddDefaultTokenProviders();
这将解决您的问题:
I'm not able to connect UserRoles to IdentityUser.:
我现在能够像处理与任何其他模型的关系一样处理用户及其角色之间的关系(同时还使用 UserManager 和 RoleManager)。例如,要使用 UserManager 将用户分配给角色:await UserManager.AddToRoleAsync(ApplicationUserObject, "admin");