gpt4 book ai didi

c# - 如何在 ASP .NET MVC 5 中为 AspNetUser 创建 SecurityStamp

转载 作者:IT王子 更新时间:2023-10-29 04:33:58 26 4
gpt4 key购买 nike

当我在应用程序运行时通过注册操作创建用户时,应用程序用户将获得 SecurityStamp。当我通过以下方式添加用户时:

if (!context.Users.Any()) {
System.Diagnostics.Debug.WriteLine("INSIDE");
var hasher = new PasswordHasher();
try {
var users = new List<ApplicationUser> {
new ApplicationUser{PasswordHash = hasher.HashPassword("TestPass44!"), Email = "informatyka4444@wp.pl", UserName = "informatyka4444@wp.pl"},
new ApplicationUser{PasswordHash = hasher.HashPassword("TestPass44!"), Email = "informatyka4445@wp.pl", UserName = "informatyka4445@wp.pl"}
};

users.ForEach(user => context.Users.AddOrUpdate(user));

context.SaveChanges();
} catch (DbEntityValidationException e) {
System.Diagnostics.Debug.WriteLine("EXC: ");
foreach (DbEntityValidationResult result in e.EntityValidationErrors) {
foreach (DbValidationError error in result.ValidationErrors) {
System.Diagnostics.Debug.WriteLine(error.ErrorMessage);
}
}

}
}

用户没有得到安全戳:

enter image description here

然后当我想登录时,我得到:

enter image description here

问题:如何为用户生成SecurityStamp

最佳答案

安全戳可以是任何你想要的。它经常被误认为是时间戳,但事实并非如此。如果用户实体发生某些变化,它将被 ASP.NET Identity 覆盖。如果您直接处理上下文,最好的方法是生成一个新的 Guid 并将其用作图章。这是一个简单的例子:

var users = new List<ApplicationUser> 
{
new ApplicationUser
{
PasswordHash = hasher.HashPassword("TestPass44!"),
Email = "informatyka4444@wp.pl",
UserName = "informatyka4444@wp.pl",
SecurityStamp = Guid.NewGuid().ToString()
},
new ApplicationUser
{
PasswordHash = hasher.HashPassword("TestPass44!"),
Email = "informatyka4445@wp.pl",
UserName = "informatyka4445@wp.pl",
SecurityStamp = Guid.NewGuid().ToString()
}
};

关于c# - 如何在 ASP .NET MVC 5 中为 AspNetUser 创建 SecurityStamp,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25355536/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com