如何在没有EF Core的ASP .NET Core 3.1中使用身份?

问题描述 投票:0回答:1

我在ASP.NET Core 3.1中有一个项目,我想对我的应用程序实现Identity。我在EF Core中找到了很多示例,但是我使用的是LinqConnect(Devart),而不是EF Core和数据库-SQL Server。我想知道如何以一种简单的方式为我的项目实现身份。

c# asp.net-core asp.net-identity asp.net-core-identity asp.net-core-3.1
1个回答
0
投票

基本上,您要做的就是创建您的自定义用户存储。

public class UserStore : IUserStore<IdentityUser>,
                         IUserClaimStore<IdentityUser>,
                         IUserLoginStore<IdentityUser>,
                         IUserRoleStore<IdentityUser>,
                         IUserPasswordStore<IdentityUser>,
                         IUserSecurityStampStore<IdentityUser>
{
    // interface implementations not shown
}

然后注入它:

public void ConfigureServices(IServiceCollection services)
{
    // Add identity types
    services.AddIdentity<ApplicationUser, ApplicationRole>()
        .AddDefaultTokenProviders();

    // Identity Services
    services.AddTransient<IUserStore<ApplicationUser>, CustomUserStore>();
    services.AddTransient<IRoleStore<ApplicationRole>, CustomRoleStore>();
    string connectionString = Configuration.GetConnectionString("DefaultConnection");
    services.AddTransient<SqlConnection>(e => new SqlConnection(connectionString));
    services.AddTransient<DapperUsersTable>();

    // additional configuration
}

记录在the official documentation中。

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