如何在ASP .NET MVC 5中为AspNetUser创建SecurityStamp
当我通过Register操作创建用户时,应用程序用户运行SecurityStamp。 当我添加用户时:
if (!context.Users.Any()) { System.Diagnostics.Debug.WriteLine("INSIDE"); var hasher = new PasswordHasher(); try { var users = new List { 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); } } } }
用户没有安全标记:
然后当我想登录时,我得到:
问题:如何为用户生成SecurityStamp
?
安全标记可以是您想要的任何内容。 它经常被误认为是时间戳,但事实并非如此。 如果用户实体上发生了某些变化,ASP.NET身份将覆盖它。 如果您直接处理上下文,最好的方法是生成一个新的Guid并将其用作图章。 这是一个简单的例子:
var users = new List { 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() } };
- 替代在System.Web中为Owin使用HttpContext
- 使用带有Asp.net Identity 1.1 Alpha的Guid和自定义表名创建自定义实现
- Identity 3 MVC 6中的授权策略
- 如何解决ASP.NET MVC Kendo UI网格中的CRUD操作问题
- ASP.Net Identity如何设置目标DB?
- 使用ASP.NET Idenity创建默认用户帐户
- Web API 2 + MVC 5共享标识表
- 在Identity Framework的方法中,HttpContext.Current为null
- 如何从ASP.NET MVC自定义IdentityUser模型获取用户配置文件数据?