【问题标题】:How to use Identity in ASP .NET Core 3.1 without EF Core?如何在没有 EF Core 的情况下在 ASP .NET Core 3.1 中使用身份?
【发布时间】:2020-02-20 08:50:04
【问题描述】:
我在 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】:
基本上,您需要做的就是创建自定义用户存储。
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
}
它记录在the official documentation。