si2*_*030 6 asp.net-mvc asp.net-identity-3 asp.net-core
我在Stackoverflow中已经找到了这个,到目前为止看起来有很多关于在Identity 1和2中添加角色的问题,但它在Identity 3中有所不同.
我想在数据库中播种角色.我只有两个.我打算使用我注入到类中的_roleManager.没关系.我的问题是..似乎没有任何实际添加角色的方法.使用CreateAsync是为了将用户添加到角色..你如何编码使用"_userManager"添加角色或者你必须这样做其他方式?
编辑
身分
In 用于创建角色,用于将用户添加到角色.这是一个指向正确方向的示例.以下代码用于创建新角色Identity RoleManagerUserManagerAdministrator
if (!roleManager.RoleExists("Administrator"))
{
MyIdentityRole newRole = new MyIdentityRole("Administrator", "Administrators can do something with data");
roleManager.Create(newRole);
}
Run Code Online (Sandbox Code Playgroud)
编辑
此外,这是用于将用户添加到角色,这也是一个示例:
\\assuming you test if the user has been assigned to the role "Administrator" before adding them to that role
if(RoleAdministrator == true){
userManager.AddToRole(User.Id, "Administrator");
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
11848 次 |
| 最近记录: |