我已经创建了用户,并使用以下方法为每个用户分配了一个角色:我必须更新用户信息和角色停留在管理员用户。
var userDet = new ApplicationUser
{
Email = model.Email,
UserName = model.Email,
TenantId = tenantId,
IsAdmin = false
};
var result = await _userManager.CreateAsync(userDet, model.Password);
if (model.Role == "ADMIN")
throw new NullReferenceException("Can't Add Admin Role to User!");
var assignRole = await _userManager.AddToRoleAsync(userDet, model.Role);
但我无法更新创建的用户角色,因为我无法从以下代码的用户ID中获取角色ID和角色名称:
var user = await _userManager.FindByEmailAsync(model.Email);
var admin = await _userManager.FindByEmailAsync(model.AdminEmail);
bool IsAdmin = admin.IsAdmin;
var userDet = new ApplicationUser
{
OrganizationName = model.OrganizationName,
};
var result = await _userManager.UpdateAsync(userDet);
var user = await _userManager.FindAsync(model.Email);
string oldRoleName = await _userManager.GetRoles(user.Id).FirstOrDefault();
var removerole= await _roleManager.RemoveRoleAsync(user.Id, oldRoleName);
var result2 = await _userManager.AddToRoleAsync(user.Id, model.Role);
我无法从上面的代码中检索RoleName,roleid。我如何根据模型的id更新用户。从其他用户向用户发送电子邮件。
var user =await _userManager.FindByEmailAsync(model.Email);
var roles = await _userManager.GetRolesAsync(user);
await _userManager.RemoveFromRoleAsync(user, roles[0]);
await _userManager.AddToRoleAsync(user, model.Role);