我試着閱讀關於ASP.NET Core Identity和Entity Framework的文檔。但我覺得還是不明智的。ASP NET核心身份與實體框架自定義DbContext
我不想和IdentityDbContext
,IdentityUser
,IdentityRole
有什麼關係。我只是想用我自己的DbContext
實現與UserManager
,UserStore
,RoleManager
,SignInManager
,以及在什麼其他參與簽約班快樂工作。
所以這已經說了,已經創建了一個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配置連接它?
好吧,我現在有一些「運行」,所以現在調用AccountController構造函數,並注入依賴關係。這是圍繞着實現一個自定義的ApplicationUserStore和ApplicationRoleStore。一旦我找到解決辦法,就會解決問題。 – Andez
它已經在ConfigureServices方法中連接了... – mvermef
以上不起作用。我故意沒有放錯,因爲我已經來回地嘗試了每個類的實現 - 在DI服務中添加不同的組合.AddIdentity(...)。AddXXX。稍後回到家時,我會重提這個問題。 – Andez