首页 > 解决方案 > 如何在没有 EF Core 的情况下在 ASP .NET Core 3.1 中使用身份?

问题描述

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

标签: c#asp.net-coreasp.net-identityasp.net-core-identityasp.net-core-3.1

解决方案


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

public class CustomUserStore : 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>();
    string connectionString = Configuration.GetConnectionString("DefaultConnection");
    services.AddTransient<SqlConnection>(e => new SqlConnection(connectionString));
    services.AddTransient<DapperUsersTable>();

    // additional configuration
}

它记录在官方文档中。


推荐阅读