【问题标题】:Move usernames and passwords to ASP.NET Core Identity将用户名和密码移动到 ASP.NET Core Identity
【发布时间】:2019-11-26 08:59:14
【问题描述】:

解密后我可以从当前数据库中以纯文本形式获取所有用户名和密码。然后,我必须使用 Microsoft Identity 将这些数据放入数据库以进行密码加密(AspNetUsers 表)。我正在考虑编写一个控制台应用程序来使用身份和转换密码,但想知道是否有更简单的方法。我已经搜索并找不到任何实用程序来执行此操作。有任何想法吗?

【问题讨论】:

  • 您必须将 Identity 集成到您的控制台应用程序中,才能使用 this 等函数正确执行此操作

标签: c# sql .net asp.net-core


【解决方案1】:

您可以在控制台应用程序中集成身份并将用户添加到数据库中,请按照以下步骤操作:

1.添加以下包:

<PackageReference Include="Microsoft.AspNetCore.Identity" Version="2.2.0" />
<PackageReference Include="Microsoft.AspNetCore.Identity.EntityFrameworkCore" Version="2.2.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="2.2.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="2.2.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="2.2.0">
  <PrivateAssets>all</PrivateAssets>
  <IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.Extensions.Configuration" Version="2.2.0" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="2.2.0" />

2.创建Models目录并创建ApplicationUserApplicationDbContext

public class ApplicationUser : IdentityUser
{
}

public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
    public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
        : base(options)
    {
    }
}

3。添加一个实现IDesignTimeDbContextFactory的类

    public class ApplicationDbContextFactory:IDesignTimeDbContextFactory<ApplicationDbContext>
    {
      public ApplicationDbContext CreateDbContext(string[] args)
      {
        var optionsBuilder = new DbContextOptionsBuilder<ApplicationDbContext>();
        optionsBuilder.UseSqlServer("Server=(localdb)\\mssqllocaldb;Database=ConsoleAppIdentity;Trusted_Connection=True;ConnectRetryCount=0");

        return new ApplicationDbContext(optionsBuilder.Options);
      }
    }

4.添加并执行合并

PM> add-migration CreateInitial
PM> update-database

5.Program.cs的代码

class Program
{
    static void Main(string[] args)
    {
        var services = new ServiceCollection();

        //setup our DI
        // Add framework services.            
        services.AddDbContext<ApplicationDbContext>(options=> {
            options.UseSqlServer("Server=(localdb)\\mssqllocaldb;Database=ConsoleAppIdentity;Trusted_Connection=True;ConnectRetryCount=0");
        });

        services.AddIdentity<ApplicationUser, IdentityRole>()
            .AddEntityFrameworkStores<ApplicationDbContext>()
            .AddDefaultTokenProviders();

        services.AddScoped<IUserCreationService, UserCreationService>();

        // Build the IoC from the service collection
        var provider = services.BuildServiceProvider();

        var userService = provider.GetService<IUserCreationService>();

        userService.CreateUser().GetAwaiter().GetResult();

        Console.ReadKey();
    }

    public interface IUserCreationService
    {
        Task CreateUser();
    }

    public class UserCreationService : IUserCreationService
    {
        public readonly UserManager<ApplicationUser> userManager;

        public UserCreationService(UserManager<ApplicationUser> userManager)
        {
            this.userManager = userManager;
        }

        public async Task CreateUser()
        {
            var user = new ApplicationUser { UserName = "TestUser", Email = "test@example.com" };
            var result = await this.userManager.CreateAsync(user, "Test@123");

            if (result.Succeeded == false)
            {
                foreach (var error in result.Errors)
                {
                    Console.WriteLine(error.Description);
                }
            }
            else
            {
                Console.WriteLine("Done.");
            }
        }
    }
}

【讨论】:

  • 这看起来会起作用,但我会担心在控制台应用程序中定义 ApplicationUser 和上下文。理想情况下,它们将位于可共享的库中。
猜你喜欢
  • 2019-05-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-09-16
  • 2019-09-13
  • 2020-07-06
  • 1970-01-01
  • 2021-05-16
相关资源
最近更新 更多