如何在ASP .NET MVC 5中为AspNetUser创建SecurityStamp [英] How to create SecurityStamp for AspNetUser in ASP .NET MVC 5

查看:137
本文介绍了如何在ASP .NET MVC 5中为AspNetUser创建SecurityStamp的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

When I create user by Register action whe application is running the application user gets SecurityStamp. When I add user by:

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);
                        }
                    }

                }
            }

用户未获得安全标记:

然后我想登录时得到:

问题:如何为用户生成SecurityStamp?

推荐答案

安全标记可以是您想要的任何东西.它经常被误认为是时间戳,但事实并非如此.如果用户实体上发生某些更改,它将被ASP.NET Identity覆盖.如果直接在上下文中工作,最好的方法是生成一个新的Guid并将其用作标记.这是一个简单的示例:

The security stamp can be anything you want. It is often mistaken to be a timestamp, but it is not. It will be overriden by ASP.NET Identity if something changes on the user entity. If you're working on the context directly the best way would to generate a new Guid and use it as the stamp. Here's a simple example:

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()
                         }
                };

这篇关于如何在ASP .NET MVC 5中为AspNetUser创建SecurityStamp的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆