일반 저장소 패턴에서 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);
}
}
책임을 왜 혼합해야합니까? 리포지토리는 데이터를 가져 오는 작업을 담당해야합니다. 인증은 저장소의 책임이 아닙니다. 이를 위해 IAuthenticationFilter와 같은 것을 사용해야하며 컨트롤러 나 동작에 적용해야합니다. 저장소를 호출 할 때 사용자는 해당 승인을 받아야합니다. –
인증을 처리하는 기본 방법은 그대로두고 다른 데이터에 대한 일반 저장소를 사용하는 것입니다. 방금 한 지붕 아래에서 모든 것을 얻으려고했습니다. –
@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