Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-core/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 创建新用户时添加声明_C#_Asp.net Core_Asp.net Identity_Asp.net Identity 3 - Fatal编程技术网

C# 创建新用户时添加声明

C# 创建新用户时添加声明,c#,asp.net-core,asp.net-identity,asp.net-identity-3,C#,Asp.net Core,Asp.net Identity,Asp.net Identity 3,我正在使用ASP.NET核心标识创建一个新用户,如下所示: new User { Email = "john@company.com", Name = "John" } await userManager.CreateAsync(user, "password"); 我需要在创建用户时添加声明。我试过: new User { Email = "john@company.com", Name = "John", Claims = new List<Claim>

我正在使用ASP.NET核心标识创建一个新用户,如下所示:

new User {
  Email = "john@company.com",
  Name = "John"
}

await userManager.CreateAsync(user, "password");
我需要在创建用户时添加声明。我试过:

new User {
  Email = "john@company.com",
  Name = "John",
  Claims = new List<Claim> { /* Claims to be added */ }  
}
新用户{
电子邮件=”john@company.com",
Name=“John”,
索赔=新列表{/*要添加的索赔*/}
}
但声明属性是只读的


执行此操作的最佳方法是什么?

您可以使用
UserManager.addClaimesync
方法向用户添加索赔

var user = new User {
  Email = "john@company.com",
  Name = "John"
}

await userManager.CreateAsync(user, "password");

await userManager.AddClaimAsync(user, new System.Security.Claims.Claim("your-claim", "your-value"));
或将索赔添加到用户
索赔
集合

var user = new User {
  Email = "john@company.com",
  Name = "John"
}

user.Claims.Add(new IdentityUserClaim<string> 
{ 
    ClaimType="your-type", 
    ClaimValue="your-value" 
});

await userManager.CreateAsync(user);
var user=新用户{
电子邮件=”john@company.com",
Name=“约翰”
}
user.Claims.Add(新标识)用户声明
{ 
ClaimType=“您的类型”,
ClaimValue=“您的价值”
});
等待userManager.CreateAsync(用户);

查看此答案我不想在身份验证cookie中保存数据。我希望将添加索赔保存到用户并将其保存到数据库我没有测试它,但据我从评论中了解,索赔保存在数据库中。wait UserManager.addclaimersync(用户,新索赔(“您的索赔”,“您的价值”)@是的,没错。。。你能给我加一个答案吗?这样我就可以给它打分了?