[英]Using ApplicationUser in a Generic Repository pattern
是否可以在通用存儲庫模式中使用asp net core的默認身份驗證,如果可以,如何使用?
我能夠創建一個對所有代碼都使用通用存儲庫模式的項目,但使用默認方式的項目的身份驗證方除外。
我的存儲庫如下所示:
using System.Linq;
using DemoWebsite.Interfaces;
using Microsoft.EntityFrameworkCore;
namespace DemoWebsite.Data
{
public class Repository<T> : IRepository<T> where T : class
{
protected readonly DbContext Context;
protected DbSet<T> DbSet;
public Repository(ApplicationDbContext context)
{
Context = context;
DbSet = context.Set<T>();
}
public void Add(T entity)
{
Context.Set<T>().Add(entity);
Save();
}
public T Get<TKey>(TKey id)
{
return DbSet.Find(id);
}
public IQueryable<T> GetAll()
{
return DbSet;
}
public void Update(T entity)
{
Save();
}
private void Save()
{
Context.SaveChanges();
}
}
}
我的ApplicationDbContext類:
public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
: base(options)
{
}
protected override void OnModelCreating(ModelBuilder builder)
{
base.OnModelCreating(builder);
// Customize the ASP.NET Identity model and override the defaults if needed.
// For example, you can rename the ASP.NET Identity table names and more.
// Add your customizations after calling base.OnModelCreating(builder);
}
}
是的,這是可能的,但是這樣做需要很多努力。 您將必須實現IUserStore
以及可能的IRoleStore
( 此處和此處的接口)。
在實現此接口時,還必須實現所有功能接口(用於獲取密碼,兩因素身份驗證( 在此處 ),安全標記等)。
對於示例實現,您始終可以查看EF Core Identity提供程序的來源。
像這樣
public abstract class UserStore<TUser, TRole, TContext, TKey, TUserClaim, TUserRole, TUserLogin, TUserToken, TRoleClaim> :
IUserLoginStore<TUser>,
IUserRoleStore<TUser>,
IUserClaimStore<TUser>,
IUserPasswordStore<TUser>,
IUserSecurityStampStore<TUser>,
IUserEmailStore<TUser>,
IUserLockoutStore<TUser>,
IUserPhoneNumberStore<TUser>,
IQueryableUserStore<TUser>,
IUserTwoFactorStore<TUser>,
IUserAuthenticationTokenStore<TUser>
where TUser : IdentityUser<TKey, TUserClaim, TUserRole, TUserLogin>
where TRole : IdentityRole<TKey, TUserRole, TRoleClaim>
where TContext : DbContext
where TKey : IEquatable<TKey>
where TUserClaim : IdentityUserClaim<TKey>
where TUserRole : IdentityUserRole<TKey>
where TUserLogin : IdentityUserLogin<TKey>
where TUserToken : IdentityUserToken<TKey>
where TRoleClaim : IdentityRoleClaim<TKey>
{
...
}
您的用戶商店可能看起來像:
public class GenericUserStore<TUser> : IUserStore<TUser>,
IUserPasswordStore<TUser> where TUser : ApplicationUser
{
public GenericUserStore(IRepository<TUser> userRepo) { }
public Task<TUser> FindByIdAsync(string userId, CancellationToken cancellationToken)
{
return userRepo.Get(userId);
}
...
}
與RoleStore
相同。 然后,用您自己的EF Provider注冊替換EF Provider注冊( 此處是EF的來源)。
userStoreType = typeof(GenericUserStore<,,,>).MakeGenericType(userType, roleType, contextType, keyType);
roleStoreType = typeof(GenericRoleStore<,,>).MakeGenericType(roleType, contextType, keyType);
var services = new ServiceCollection();
services.AddScoped(
typeof(IUserStore<>).MakeGenericType(userType),
userStoreType);
services.AddScoped(
typeof(IRoleStore<>).MakeGenericType(roleType),
roleStoreType);
然后, UserManager
和其他Identity類將使用您的用戶/角色存儲。 但這要麻煩的多是值得的,除非您具有無法映射到EF /已知提供程序的現有數據庫結構。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.