代码之家  ›  专栏  ›  技术社区  ›  Yoda ESG

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

  •  26
  • Yoda ESG  · 技术社区  · 11 年前

    当我在应用程序运行时通过注册操作创建用户时,用户将获得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 对于用户?

    2 回复  |  直到 11 年前
        1
  •  42
  •   Nick Jones    11 年前

    安全印章可以是你想要的任何东西。它经常被误认为是时间戳,但事实并非如此。如果用户实体上发生更改,则ASP.NET标识将覆盖它。如果您正在直接处理上下文,最好的方法是生成新的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()
                             }
                    };
    
        2
  •  1
  •   Christos Lytras    6 年前

    如果我们往里面看 IdentityUser 桌子 AspNetUsers 数据,我们将看到 SecurityStamp 具有不同于正常的形式 GUID :

    Identity AspNetUsers

    这是因为 GUID(引导) 转换为HEX映射字符串。

    我们可以创建一个函数来生成新的Security Stamp,它将生成一个新的 GUID(引导) 并将其转换为HEX映射字符串:

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

    你可以检查它运行到这个 .NET Fiddle .

    因此,如果我们想要种子身份用户,我们可以使用它来生成 安全标志 :

    modelBuilder.Entity<IdentityUser>().HasData(new ApplicationUser
    {
        ...
    
        // Security stamp is a GUID bytes to HEX string
        SecurityStamp = GenerateSecurityStamp(),
    });
    

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