在 asp.net 核心中发布更新用户角色
Issue Updating User role in asp.net core
注册时,所有用户都被添加到一般角色。管理员如何更新或更改用户角色以授予他们更多权限。
更新角色是现在的问题
这是我到目前为止所做的
EditRole Model
public class EditRole
{
public EditRole()
{
Users = new List<string>();
}
public string Id { get; set; }
[Required(ErrorMessage = "Role Name is Required")]
public string RoleName { get; set; }
public List<string>Users { get; set; }
}
在视图中,我已经将角色 ID 传递给 Url,我还可以获得用户列表,然后 select 我想从下拉列表中更新的用户。我在想,因为我有角色 ID,所以我可以使用 UserManager 查询并将 selected 用户分配给角色。
@model LoanWarehouse.Repo.Models.EditRole
<div class="container" style="margin-top: 100px">
<div class="row">
<div class="col-md-6">
<h2>Please select user and the role you want to assign</h2>
<form asp-action="Assign" >
<div class="dropdown">
<select asp-for="Users" id="roles" class="form-control">
<option>Select User</option>
@foreach (var user in Model.Users)
{
<option asp-for="Users">@user</option>
}
</select>
</div>
<div class="form-group has-feedback">
<input type="hidden" asp-for="@Model.Id" class="form-control" placeholder="Role">
<span asp-validation-for="Id" class="help-block"></span>
</div>
@*<td><a class="btn btn-warning btn-sm" asp-controller="Admin" asp-action="EditRole" asp-route-id="@role.Id">Edit</a></td>*@
<button type="submit" class="btn btn-success" >Assign User to role</button>
</form>
</div>
</div>
</div>
这是控制器
[HttpPost]
public async Task<IActionResult> Assign(string id, EditRole model)
{
try
{
var role = roleManager.FindByIdAsync(model.Id);//find passed in role id
var currentRole = roleManager.FindByNameAsync(model.Users[0]);//find the role of current user
var user = await userManager.FindByNameAsync(model.Users[0]);
if (role == null)
{
ViewBag.ErrorMessage = $"Role with Id = {id} cannot be found";
return RedirectToAction("ListRoles", "Admin");
}
else
{
await userManager.RemoveFromRoleAsync(user, currentRole.Result.Name);
await userManager.AddToRoleAsync(user, role.Result.Name);
return RedirectToAction("ListRoles", "Admin");
}
}
catch (Exception ex)
{
throw ex;
}
}
我似乎无法让它工作
有人可以帮忙吗
在您的代码中,您正在使用 RoleManager.FindByNameAsync(String) method 查找当前用户的角色,并且您提供了用户名(而不是角色名),这导致了问题。
var currentRole = roleManager.FindByNameAsync(model.Users[0]);
要修复它,您可以使用 UserManager.GetRolesAsync(TUser) Method 获取指定用户所属的角色名称,如下所示。
var role = await roleManager.FindByIdAsync(model.Id);
var user = await userManager.FindByNameAsync(model.Users[0]);
//find the role(s) of current user
var currentRoles = await userManager.GetRolesAsync(user);
// ...
//your code logic here
await userManager.RemoveFromRolesAsync(user, currentRoles);
//...
注册时,所有用户都被添加到一般角色。管理员如何更新或更改用户角色以授予他们更多权限。
更新角色是现在的问题 这是我到目前为止所做的 EditRole Model
public class EditRole
{
public EditRole()
{
Users = new List<string>();
}
public string Id { get; set; }
[Required(ErrorMessage = "Role Name is Required")]
public string RoleName { get; set; }
public List<string>Users { get; set; }
}
在视图中,我已经将角色 ID 传递给 Url,我还可以获得用户列表,然后 select 我想从下拉列表中更新的用户。我在想,因为我有角色 ID,所以我可以使用 UserManager 查询并将 selected 用户分配给角色。
@model LoanWarehouse.Repo.Models.EditRole
<div class="container" style="margin-top: 100px">
<div class="row">
<div class="col-md-6">
<h2>Please select user and the role you want to assign</h2>
<form asp-action="Assign" >
<div class="dropdown">
<select asp-for="Users" id="roles" class="form-control">
<option>Select User</option>
@foreach (var user in Model.Users)
{
<option asp-for="Users">@user</option>
}
</select>
</div>
<div class="form-group has-feedback">
<input type="hidden" asp-for="@Model.Id" class="form-control" placeholder="Role">
<span asp-validation-for="Id" class="help-block"></span>
</div>
@*<td><a class="btn btn-warning btn-sm" asp-controller="Admin" asp-action="EditRole" asp-route-id="@role.Id">Edit</a></td>*@
<button type="submit" class="btn btn-success" >Assign User to role</button>
</form>
</div>
</div>
</div>
这是控制器
[HttpPost]
public async Task<IActionResult> Assign(string id, EditRole model)
{
try
{
var role = roleManager.FindByIdAsync(model.Id);//find passed in role id
var currentRole = roleManager.FindByNameAsync(model.Users[0]);//find the role of current user
var user = await userManager.FindByNameAsync(model.Users[0]);
if (role == null)
{
ViewBag.ErrorMessage = $"Role with Id = {id} cannot be found";
return RedirectToAction("ListRoles", "Admin");
}
else
{
await userManager.RemoveFromRoleAsync(user, currentRole.Result.Name);
await userManager.AddToRoleAsync(user, role.Result.Name);
return RedirectToAction("ListRoles", "Admin");
}
}
catch (Exception ex)
{
throw ex;
}
}
我似乎无法让它工作 有人可以帮忙吗
在您的代码中,您正在使用 RoleManager.FindByNameAsync(String) method 查找当前用户的角色,并且您提供了用户名(而不是角色名),这导致了问题。
var currentRole = roleManager.FindByNameAsync(model.Users[0]);
要修复它,您可以使用 UserManager.GetRolesAsync(TUser) Method 获取指定用户所属的角色名称,如下所示。
var role = await roleManager.FindByIdAsync(model.Id);
var user = await userManager.FindByNameAsync(model.Users[0]);
//find the role(s) of current user
var currentRoles = await userManager.GetRolesAsync(user);
// ...
//your code logic here
await userManager.RemoveFromRolesAsync(user, currentRoles);
//...