在通用存储库模式中使用ApplicationUser

时间:2017-02-13 14:52:04

标签: c# generics asp.net-core entity-framework-6

是否可以在通用存储库模式中使用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);
    }
}

1 个答案:

答案 0 :(得分:3)

是的,这是可能的,但要做到这一点需要付出相当大的努力。您必须实施IUserStore,可能IRoleStore(接口herehere)。

实现此接口时,还必须实现所有功能接口(用于获取密码,双因素身份验证(here),安全戳等)。

对于示例实现,您始终可以查看EF Core Identity provider的来源。

喜欢这个

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提供者注册(EF here的来源)。

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 /已知提供商的现有数据库结构,否则它的价值就更大了。