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

问题描述 投票:21回答:2

当我通过注册操作创建用户时,应用程序正在运行,应用程序用户将获得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 = "[email protected]", UserName = "[email protected]"},
                        new ApplicationUser{PasswordHash = hasher.HashPassword("TestPass44!"), Email = "[email protected]", UserName = "[email protected]"}
                        };

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

                }
            }

用户未获得安全标记:

<< img src =“ https://image.soinside.com/eyJ1cmwiOiAiaHR0cHM6Ly9pLnN0YWNrLmltZ3VyLmNvbS9Nbnpvdy5wbmcifQ==” alt =“在此处输入图像描述”>

然后我想登录时得到:

<< img src =“ https://image.soinside.com/eyJ1cmwiOiAiaHR0cHM6Ly9pLnN0YWNrLmltZ3VyLmNvbS9FU1Jjby5wbmcifQ==” alt =“在此处输入图像说明”>

问题:如何为用户生成SecurityStamp

c# asp.net-mvc entity-framework asp.net-identity seed
2个回答
37
投票

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

var users = new List<ApplicationUser> 
                { 
                    new ApplicationUser
                        {
                            PasswordHash = hasher.HashPassword("TestPass44!"), 
                            Email = "[email protected]", 
                            UserName = "[email protected]", 
                            SecurityStamp = Guid.NewGuid().ToString()
                        },
                    new ApplicationUser
                        {
                            PasswordHash = hasher.HashPassword("TestPass44!"),
                            Email = "[email protected]", 
                            UserName = "[email protected]", 
                            SecurityStamp = Guid.NewGuid().ToString()
                         }
                };

0
投票

[如果我们查看IdentityUserAspNetUsers的数据,我们会发现SecurityStamp的格式与普通GUID的格式不同:

Identity AspNetUsers

这是因为GUID被转换为十六进制映射字符串。

我们可以创建一个函数来生成新的安全标记,它将生成一个新的GUID并将其转换为十六进制映射字符串:

Func<string> GenerateSecurityStamp = delegate()
{
    var guid = Guid.NewGuid();
    return String.Concat(Array.ConvertAll(guid.ToByteArray(), b => b.ToString("X2")));
};

您可以检查它是否运行到此.NET Fiddle

因此,如果我们想播种身份用户,我们可以使用它来生成SecurityStamp

modelBuilder.Entity<IdentityUser>().HasData(new ApplicationUser
{
    ...

    // Security stamp is a GUID bytes to HEX string
    SecurityStamp = GenerateSecurityStamp(),
});

警告:请非常小心,不要将上面的示例用作种子,因为每次创建新迁移时,它将更改数据。使用HasData时,播种数据应始终预先生成,而不是动态内联生成。

© www.soinside.com 2019 - 2024. All rights reserved.