ur.Role.Nome))));-6ren">
gpt4 book ai didi

c# - 如何在 Role Claim 中存储多个角色?

转载 作者:太空宇宙 更新时间:2023-11-03 22:31:41 24 4
gpt4 key购买 nike

claims.Add(new Claim(ClaimTypes.Role, string.Join(",", user.UserRoles.Select(ur => ur.Role.Nome))));

但是如果我这样做

User.IsInRole("myRole")

返回错误

最佳答案

你可以这样做

Claim[] claims = new Claim[] 
{
new Claim(ClaimTypes.Role, "User"),
new Claim(ClaimTypes.Role, "Dev"),
new Claim(ClaimTypes.Role,"QA"),
new Claim(ClaimTypes.Role,"DBA")
};

或者您可以使用 RoleManager 来执行此操作。您可以使用角色管理器添加角色,而不是使用声明将每个角色用逗号连接起来。但是,在您使用 Role Manager 之前,请确保您在 Startup.cs 中正确注册了它

Startup.cs

 services.AddIdentity<AppUser, IdentityRole<string>>(options =>
{
options.Password.RequireDigit = false;
options.Password.RequiredLength = 4;
options.Password.RequireLowercase = false;
options.Password.RequireNonAlphanumeric = false;
options.Password.RequireUppercase = false;

options.User.RequireUniqueEmail = true;

})
.AddRoles<IdentityRole<string>>()
.AddEntityFrameworkStores<AppIdentityDbContext>()
.AddRoleManager<RoleManager<IdentityRole<string>>>()
.AddDefaultTokenProviders();

并且在数据库上下文中,确保您还包含 IdentityRole 或 IdentityRole。

AppIdentityDbContext.cs(自定义名称)

 public class AppIdentityDbContext:
IdentityDbContext<AppUser,IdentityRole<string>,string>
{
public AppIdentityDbContext(DbContextOptions<AppIdentityDbContext> options)
: base(options)
{

}
}

要添加角色,您可以在 AccountController 中指定,或者在您想要的其他 Controller 中指定。确保使用 RoleManager。在此代码片段中,请务必注意 Register 操作,您可以在此处查看如何添加新角色。

AccountController.cs

    public class AccountController : Controller
{
private readonly UserManager<AppUser> _userManager;
private readonly SignInManager<AppUser> _signInManager;
private readonly RoleManager<IdentityRole<string>> _roleManager;

public AccountController(
UserManager<AppUser> userManager,
SignInManager<AppUser> signInManager,
RoleManager<IdentityRole<string>> roleManager)
{
_userManager = userManager;
_signInManager = signInManager;
_roleManager = roleManager;
}

public IActionResult Register()
{
return View();
}

[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Register(RegisterViewModel model)
{
if(ModelState.IsValid)
{
AppUser user = new AppUser
{
FullName = model.FullName,
Email = model.Email,
UserName = model.Email
};
var createResult = await _userManager.CreateAsync(user, model.Password);
if(createResult.Succeeded)
{
await _userManager.AddClaimAsync(user, new Claim("sys:FullName", model.FullName));
if(!await _roleManager.RoleExistsAsync("User"))
{
await _roleManager.CreateAsync(new IdentityRole("User"));
}
if(!await _roleManager.RoleExistsAsync("Dev"))
{
await _roleManager.CreateAsync(new IdentityRole("Dev"));
}
await _userManager.AddToRoleAsync(user, "User");
await _userManager.AddToRoleAsync(user, "Dev");
string token = await _userManager.GenerateEmailConfirmationTokenAsync(user);
string url = Url.Action("ConfirmEmail", "Account", new
{
email = model.Email,
token
}, Request.Scheme);
System.IO.File.WriteAllText("ConfirmEmail.txt", url);
return RedirectToAction(nameof(Confirmation), new
{
confirmation = ConfirmationStatus.EmailConfirmation
});
}
foreach(var error in createResult.Errors)
{
ModelState.AddModelError("", error.Description);
}
}

return View(model);
}

关于c# - 如何在 Role Claim 中存储多个角色?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57306353/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com