【问题标题】:ASP.NET Core encryption and SQL ServerASP.NET Core 加密和 SQL Server
【发布时间】:2018-11-27 15:22:59
【问题描述】:

我正在为我的系统创建一个默认用户。但是我是通过 SQL Server 数据库创建的。

我使用 ASP.NET Core 和 Entity Framework 来处理登录,默认情况下 Entity Framework 会创建一个名为 AspNetUsers 的表;这个表有一列叫PasswordHash,我相信使用的加密是Hash类型的。

我通过数据库输入这个用户的密码如下:

DECLARE @HashThis nvarchar(4000);  
SET @HashThis = CONVERT(nvarchar(4000),'Administrador');  

SELECT HASHBYTES('SHA1', @HashThis)

UPDATE AspNetUsers 
SET PasswordHash = HASHBYTES('SHA1', @HashThis), 
    SecurityStamp = '0b12450e-016d-4cd6-af7b-fa6d2198586f', 
    ConcurrencyStamp = 'a63a5236-4020-4f69-93b1-9f077ba014cd', 
    UserName = 'administrador@administrador.com.br'

但是密码栏在日文中出现奇怪的字符,它跟随图像:

最大的问题,当我登录 ASP.NET Core 时,只有密码无效。

我该怎么做才能绕过这个?

观察:我通过 ASP.NET Core 创建用户时,运行正常。

【问题讨论】:

  • 这不是你计算哈希的方式。您应该在应用程序启动时使用 UserManager 类在您的第一个应用程序启动时使用播种来创建用户。只有这样才能为您生成正确的现金(它比计算特定哈希更复杂,它还涉及安全标记、特定算法以及以非二进制格式对所有这些进行编码,以便可以存储)。您最好在开发系统上创建一次并将该行导出为 sql 语句并在生产环境中运行
  • @Tseng 谢谢回复,确实挺复杂的。我没有想到,我会按照你说的去做。我将检查用户是否已经存在,并且在用户尝试登录之前没有通过应用程序的用户。我相信这会奏效。我会努力让你卷土重来。感谢您的提示。

标签: sql-server entity-framework razor asp.net-core asp.net-identity


【解决方案1】:

这是一个如何为用户播种的示例:

在您的 SecurityDbContext 中,您可以创建以下方法(我添加了 SeedRoles 以防您需要它们):

 public static async Task Seed(IServiceProvider serviceProvider)
    {
        await SeedRoles(serviceProvider);
        await SeedUsers(serviceProvider);
    }

种子角色:

public static async Task SeedRoles(IServiceProvider serviceProvider)
    {
        RoleManager<ApplicationRole> roleManager = serviceProvider.GetRequiredService<RoleManager<ApplicationRole>>();

        string[] roles = ...;

        foreach(var role in roles)
        {
            ApplicationRole appRole = await roleManager.FindByNameAsync(role);
            if (appRole == null)
            {
                await roleManager.CreateAsync(new ApplicationRole(role));
            }
        }
    }

种子用户:

public static async Task SeedUser(IServiceProvider serviceProvider, UserManager<ApplicationUser> userManager,  string email, string password, string roleName = "")
    {
        string userName = roleName;

        ApplicationUser user = await userManager.FindByNameAsync(userName);

        if (user == null)
        {
            // Create user account if it doesn't exist
            user = new ApplicationUser
            {
                UserName = userName,
                Email = email
            };

            IdentityResult result = await userManager.CreateAsync(user, password);

            // Assign role to the user
            if (result.Succeeded)
            {
                user = await userManager.FindByNameAsync(userName);
            }

        }

        if (user != null && roleName.Length > 0)
        {
            await userManager.AddToRoleAsync(user, roleName);
        }
    }

从 SeedUsers 方法中,只需根据需要多次调用 SeedUser。

然后只需从 Startup.cs 的配置方法中调用 Seed 方法:

SecurityDbContextSeed.Seed(app.ApplicationServices).Wait();

【讨论】:

    猜你喜欢
    • 2010-09-08
    • 1970-01-01
    • 2019-05-15
    • 1970-01-01
    • 2012-05-06
    • 1970-01-01
    • 2021-03-07
    • 2018-04-12
    • 1970-01-01
    相关资源
    最近更新 更多