C# 如何将自定义属性添加到.Net Core中的角色声明中?
我使用Cookie身份验证而不是身份核心。 我从自定义表中循环浏览用户应该具有哪些角色,并添加如下角色声明:C# 如何将自定义属性添加到.Net Core中的角色声明中?,c#,asp.net-core,C#,Asp.net Core,我使用Cookie身份验证而不是身份核心。 我从自定义表中循环浏览用户应该具有哪些角色,并添加如下角色声明: if (aartsUser != null) { #region Create Claims // UserName claim and StaffId claim. Will need StaffId to write to tables such as UploadStaffId for FacUpload. var claims = new List<
if (aartsUser != null)
{
#region Create Claims
// UserName claim and StaffId claim. Will need StaffId to write to tables such as UploadStaffId for FacUpload.
var claims = new List<Claim>
{
new Claim(ClaimTypes.Name, loginModel.UserName),
new Claim("StaffId", aartsUser.StaffID.ToString())
};
// Role Claims
foreach(Position p in aartsUser.Positions)
{
claims.Add(new Claim(ClaimTypes.Role, p.Role.RoleCd));
}
#endregion
var claimsIdentity = new ClaimsIdentity(claims, CookieAuthenticationDefaults.AuthenticationScheme);
await HttpContext.SignInAsync(new ClaimsPrincipal(claimsIdentity));
return Redirect(loginModel?.ReturnUrl ?? "/");
}
来自我拥有的角色实体:
[Table("role")]
public class Role
{
[Key]
[Column("role_id")]
public short RoleID { get; set; }
[Column("role_nm")]
public string RoleName { get; set; }
[Column("role_cd")]
public string RoleCd { get; set; }
[Column("role_group_nm")]
public string RoleGroupName { get; set; }
[Column("role_class_cd")]
public string RoleClassCd { get; set; }
public List<Position> Positions { get; set; }
}
比如:
claims.Add(new Claim(ClaimTypes.Role, p.Role.RoleCd).Properties.Add("Description", p.Role.RoleName));
但它说不能从void转换为System.Security.Claims.Claims.我明白了
以下是我所做的:
var claims = new List<Claim>
{
new Claim(ClaimTypes.Name, loginModel.UserName),
new Claim("StaffId", aartsUser.StaffID.ToString())
};
// Role Claims
foreach(Position p in aartsUser.Positions)
{
claims.Add(new Claim(ClaimTypes.Role, p.Role.RoleCd));
Claim newClaim = claims.Where(c => c.Value == p.Role.RoleCd).FirstOrDefault();
newClaim.Properties.Add("Description", p.Role.RoleName);
}
var索赔=新列表
{
新声明(ClaimTypes.Name、loginModel.UserName),
新声明(“StaffId”,aartsUser.StaffId.ToString())
};
//角色声明
foreach(aartsUser.位置中的位置p)
{
添加(新的索赔(ClaimTypes.Role,p.Role.RoleCd));
Claim newClaim=claims.Where(c=>c.Value==p.Role.RoleCd).FirstOrDefault();
newClaim.Properties.Add(“Description”,p.Role.RoleName);
}
根据您自己的答案,我有更好的解决方案。因此,您无需再次查询索赔
foreach(Position p in aartsUser.Positions)
{
Claim newClaim = new Claim(ClaimTypes.Role, p.Role.RoleCd));
newClaim.Properties.Add("Description", p.Role.RoleName);
claims.Add(newClaim);
}
FWIW我会在需要它的页面上查找它。名称可能会更改,您在cookie中存储的内容将过时。请说明被否决的原因。如果该方法存在严重问题,请详细说明,以便其他人可以避免!谢谢
var claims = new List<Claim>
{
new Claim(ClaimTypes.Name, loginModel.UserName),
new Claim("StaffId", aartsUser.StaffID.ToString())
};
// Role Claims
foreach(Position p in aartsUser.Positions)
{
claims.Add(new Claim(ClaimTypes.Role, p.Role.RoleCd));
Claim newClaim = claims.Where(c => c.Value == p.Role.RoleCd).FirstOrDefault();
newClaim.Properties.Add("Description", p.Role.RoleName);
}
foreach(Position p in aartsUser.Positions)
{
Claim newClaim = new Claim(ClaimTypes.Role, p.Role.RoleCd));
newClaim.Properties.Add("Description", p.Role.RoleName);
claims.Add(newClaim);
}