当我在应用程序运行时通过注册操作创建用户时,应用程序用户将获得安全标记。当我通过以下方式添加用户时:
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 = "[email protected] /cdn-cgi/l/email-protection", UserName = "[email protected] /cdn-cgi/l/email-protection"},
new ApplicationUser{PasswordHash = hasher.HashPassword("TestPass44!"), Email = "[email protected] /cdn-cgi/l/email-protection", UserName = "[email protected] /cdn-cgi/l/email-protection"}
};
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);
}
}
}
}
用户没有获得安全标记:
然后当我想登录时我得到:
问题:如何生成SecurityStamp
对于用户?
安全印章可以是您想要的任何内容。它经常被误认为是时间戳,但事实并非如此。如果用户实体发生更改,它将被 ASP.NET Identity 覆盖。如果您直接处理上下文,最好的方法是生成一个新的 Guid 并将其用作标记。这是一个简单的例子:
var users = new List<ApplicationUser>
{
new ApplicationUser
{
PasswordHash = hasher.HashPassword("TestPass44!"),
Email = "[email protected] /cdn-cgi/l/email-protection",
UserName = "[email protected] /cdn-cgi/l/email-protection",
SecurityStamp = Guid.NewGuid().ToString()
},
new ApplicationUser
{
PasswordHash = hasher.HashPassword("TestPass44!"),
Email = "[email protected] /cdn-cgi/l/email-protection",
UserName = "[email protected] /cdn-cgi/l/email-protection",
SecurityStamp = Guid.NewGuid().ToString()
}
};
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)