问题描述
我有这个方法添加带有 ClaimType 角色的 cookie
private async void AddCookies(string role)
{
var claim = new List<Claim>
{
new Claim(ClaimTypes.Role,role.ToString()),}
}
这是我的数据库 table Users And Roels And Users Roles every user have more than role
我尝试使用数组,但不像这个例子那样和我一起工作
private async void AddCookies(string[] role)
{
var claim = new List<Claim>
{
new Claim(ClaimTypes.Role,}
}
解决方法
您没有为数组的每个元素添加角色,而是在 Claim() 构造函数中将整个数组作为参数传递。您必须遍历角色数组并为每个角色创建新声明。
private async void AddCookies(string[] roles)
{
var claims = new List<Claim>();
foreach(var role in roles)
{
var claim = new Claim(ClaimTypes.Role,role);
claims.Add(claim);
}
}