我想在 foreach 循环中向声明数组添加新声明。怎么做?
//userRoles is a list of string contains roles.
var userRoles = _repository.GetRolesOfUser(username);
var claim = new[]
{
new Claim("username", username)
};
//I want to add new claims to claim like below.
//When I put Add I am getting error like this
// "Claim[] doesn't contain definition for Add."
foreach(var userRole in userRoles)
{
claim.Add(new Claim("roles", userRole));
}
我最后想要的是这样的,其中 Role_1、Role_2 等来自 userRole 列表。
var claim = new[]
{
new Claim("username", username)
new Claim("roles", "Role_1")
new Claim("roles", "Role_2")
new Claim("roles", "Role_3")
new Claim("roles", "Role_4")
}