2017-02-13 12 views
0

일반 저장소 패턴에서 asp net core의 기본 인증을 사용할 수 있습니까? 그렇다면 어떻게해야합니까?일반 저장소 패턴에서 ApplicationUser 사용

기본 방식을 사용하는 프로젝트의 인증 측을 제외하고 모든 코드에 일반 저장소 패턴을 사용하는 프로젝트를 만들 수 있습니다.

내 저장소 보이는 같은 :

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); 
    } 
} 
+2

책임을 왜 혼합해야합니까? 리포지토리는 데이터를 가져 오는 작업을 담당해야합니다. 인증은 저장소의 책임이 아닙니다. 이를 위해 IAuthenticationFilter와 같은 것을 사용해야하며 컨트롤러 나 동작에 적용해야합니다. 저장소를 호출 할 때 사용자는 해당 승인을 받아야합니다. –

+0

인증을 처리하는 기본 방법은 그대로두고 다른 데이터에 대한 일반 저장소를 사용하는 것입니다. 방금 ​​한 지붕 아래에서 모든 것을 얻으려고했습니다. –

+1

@MarcusS : 이론 상으로는 그렇습니다. 모든 기능을 갖춘 IUserStore를 구현해야합니다 (암호 등을 얻으려면 여기에서 EF 구현을 참조하십시오.) https://github.com/aspnet/Identity/blob/rel/1.1.0/src/Microsoft.AspNetCore .Identity.EntityFrameworkCore/UserStore.cs # L161-L170) https://github.com/aspnet/Identity/blob/rel/1.1.0/src/Microsoft.AspNetCore.Identity.EntityFrameworkCore/IdentityEntityFrameworkBuilderExtensions에 직접 등록하십시오. cs # L45-L51. EF/알려진 공급자에 매핑 할 수없는 DB 구조가 없으면 더 많은 문제가 있습니다. – Tseng

답변

3

예는 가능하지만, 그렇게 할 수있는 노력이 꽤 있습니다. IUserStore 및 가능하면 IRoleStore (인터페이스 herehere)을 구현해야합니다.

이 인터페이스를 구현할 때는 암호, 이중 인증 (here), 보안 스탬프 등을 위해 모든 기능 인터페이스를 구현해야합니다.

예제 구현의 경우 항상 EF Core Identity provider의 출처를 볼 수 있습니다. RoleStore에 대한

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); 
    } 

    ... 
} 

과 같은이

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> 
{ 
    ... 
} 

사용자 저장소처럼

처럼 볼 수 있었다. 마지막으로 EF Provider 등록을 자신의 EF Provider 등록으로 대체하십시오 (출처 : 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 및 기타 신원 클래스는 사용자/역할 저장소를 사용합니다. 그러나 EF/알려진 공급자에 매핑 할 수없는 기존 DB 구조가 없으면 더 큰 문제가됩니다.