0

我試着閱讀關於ASP.NET Core Identity和Entity Framework的文檔。但我覺得還是不明智的。ASP NET核心身份與實體框架自定義DbContext

我不想和IdentityDbContext,IdentityUser,IdentityRole有什麼關係。我只是想用我自己的DbContext實現與UserManagerUserStoreRoleManagerSignInManager,以及在什麼其他參與簽約班快樂工作。

所以這已經說了,已經創建了一個ASP.NET的默認核心項目使用「個人用戶帳戶」。現在我想知道該控制器需要使用DI接線才能「工作」。

看着賬戶控制器構造:

public AccountController(UserManager<ApplicationUser> userManager, 
     SignInManager<ApplicationUser> signInManager, 
     IOptions<IdentityCookieOptions> identityCookieOptions, 
     IEmailSender emailSender, 
     ISmsSender smsSender, 
     ILoggerFactory loggerFactory) 

伴隨着的是,生成以下相關的類:

public class ApplicationUser : IdentityUser 
{ 
} 

public class ApplicationDbContext : IdentityDbContext<ApplicationUser> 
{ 
    ... 
} 

和一些相關DI配置:

public void ConfigureServices(IServiceCollection services) 
{ 
    ... 
    services.AddDbContext<ApplicationDbContext>(options => 
     options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection"))); 

    services.AddIdentity<ApplicationUser, IdentityRole>() 
     .AddEntityFrameworkStores<ApplicationDbContext>() 
     .AddDefaultTokenProviders(); 
} 

public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory) 
{ 
    ... 
    app.UseIdentity(); 
} 

挖掘在ASP.NET Core Identity的源代碼中,UserStore強制約束在IdentityUser

public class UserStore : UserStore<IdentityUser<string>> 

因爲我想要的工作是什麼的東西,如下面的 - 對於初學者:

public class AuthenticationDbContext : DbContext 
{ 
    public AuthenticationDbContext(DbContextOptions options) : base(options) 
    { 
    } 

    public DbSet<ApplicationUser> ApplicationUsers { get; set; } 
    public DbSet<ApplicationRole> ApplicationRoles { get; set; } 

    protected override void OnModelCreating(ModelBuilder builder) 
    { 
     base.OnModelCreating(builder); 

     builder.Entity<ApplicationUser>(te => 
      te.HasKey(user => user.Id)); 

     builder.Entity<ApplicationRole>(te => 
      te.HasKey(role => role.Id)); 
    } 
} 

public class ApplicationRole 
{ 
    public int Id { get; set; } 
    public string UserName { get; set; } 
} 

public class ApplicationUser 
{ 
    public int Id { get; set; } 
    public Guid Guid { get; set; } 
    public string GivenName { get; set; } 
    public string FamilyName { get; set; } 
    public string MiddleName { get; set; } 
    public DateTime? DateOfBirth { get; set; } 
    public string UserName { get; set; } 
    public string EmailAddress { get; set; } 
} 

如果我列出的問題我有,例外等,這個帖子就太長了。

問題是,我怎麼用DI配置連接它?

+0

好吧,我現在有一些「運行」,所以現在調用AccountController構造函數,並注入依賴關係。這是圍繞着實現一個自定義的ApplicationUserStore和ApplicationRoleStore。一旦我找到解決辦法,就會解決問題。 – Andez

+0

它已經在ConfigureServices方法中連接了... – mvermef

+0

以上不起作用。我故意沒有放錯,因爲我已經來回地嘗試了每個類的實現 - 在DI服務中添加不同的組合.AddIdentity(...)。AddXXX。稍後回到家時,我會重提這個問題。 – Andez

回答

0

好的。繼保持我的AuthenticationDbContext相同之後,我可以提供一個IUserStore,IRoleStore的實現,並配置DI如下。

public class ApplicationUserStore : IUserStore<ApplicationUser>, IUserPasswordStore<ApplicationUser> 
{ 
    private readonly AuthenticationDbContext _dbContext; 

    public ApplicationUserStore(AuthenticationDbContext dbContext) 
    { 
     _dbContext = dbContext; 
    } 

    ... 

好的,所以我需要在這裏提供10或11個方法的實現,需要稍微弄清楚。 ApplicationRoleStore也一樣。

public class ApplicationRoleStore : IRoleStore<ApplicationRole> 
{ 
    private AuthenticationDbContext _dbContext; 

    public ApplicationRoleStore(AuthenticationDbContext context, IdentityErrorDescriber describer = null) 
    { 
     _dbContext = context; 
    } 

    ... 

那麼這可以進行接線:

public void ConfigureServices(IServiceCollection services) 
{ 
    services.AddIdentity<ApplicationUser, ApplicationRole>() 
     .AddUserStore<ApplicationUserStore>() 
     .AddRoleStore<ApplicationRoleStore>() 
     .AddDefaultTokenProviders(); 

    services.AddTransient(c => 
      new AuthenticationDbContext(
       new DbContextOptionsBuilder() 
        .UseSqlServer(Configuration.GetConnectionString("DefaultConnection")).Options)); 

所以通過賬戶控制器/登入查看登錄現在調用我ApplicationUserStore.FindByNameAsync當我點擊登錄。

無論這是或不是「適當」還有待觀察。我只是強烈地認爲ASP.NET身份應該適用於「任何」數據存儲實現,因此您不必限制使用IdentityUser。以上應該適用於我感覺的非實體框架實現。

相關問題