UserManager.GetRolesAsync() 方法
UserManager.GetRolesAsync() method
我想将用户角色添加到声明中
var claims = new[]
{
new Claim("Email", model.Email),
new Claim(ClaimTypes.NameIdentifier, user.Id),
new Claim(ClaimTypes.Role, _userManger.GetRolesAsync(user).ToString())
};
但是这个方法行不通。我怎样才能将角色名称作为字符串?
类似的内容可能适合您,
var roles = await _userMgr.GetRolesAsync(user);
var claims = new List<Claim>();
claims.Add(new Claim("Email", model.Email));
claims.Add(new Claim(ClaimTypes.NameIdentifier, user.Id));
foreach (var role in roles)
{
claims.Add(new Claim(ClaimTypes.Role, role));
}
GetRolesAsync(user) returns a List of role names (strings) 可能不止1个。
我想将用户角色添加到声明中
var claims = new[]
{
new Claim("Email", model.Email),
new Claim(ClaimTypes.NameIdentifier, user.Id),
new Claim(ClaimTypes.Role, _userManger.GetRolesAsync(user).ToString())
};
但是这个方法行不通。我怎样才能将角色名称作为字符串?
类似的内容可能适合您,
var roles = await _userMgr.GetRolesAsync(user);
var claims = new List<Claim>();
claims.Add(new Claim("Email", model.Email));
claims.Add(new Claim(ClaimTypes.NameIdentifier, user.Id));
foreach (var role in roles)
{
claims.Add(new Claim(ClaimTypes.Role, role));
}
GetRolesAsync(user) returns a List of role names (strings) 可能不止1个。