C# ASP身份自定义数据库

C# ASP身份自定义数据库,c#,asp.net-mvc,asp.net-identity,C#,Asp.net Mvc,Asp.net Identity,我有一个MVC5项目,我一直遵循本教程: 基本上,我希望使用自己的数据库,而不是使用代码优先的默认方法。我已经涵盖了所有自定义存储和标识,但我不知道如何更改应用程序以使用自定义上下文,因为MVC5项目中的默认代码使用代码优先行为 我还对IdentityModels.cs中的代码和Startup中的CreatePerOwinContext中的代码感到困惑。有人能给我解释一下或者给我一个101教程的链接吗?我试着在互联网上四处寻找,但仍然找不到合适的文档/分步教程 我的自定义用户存储: publi

我有一个MVC5项目,我一直遵循本教程:

基本上,我希望使用自己的数据库,而不是使用代码优先的默认方法。我已经涵盖了所有自定义存储和标识,但我不知道如何更改应用程序以使用自定义上下文,因为MVC5项目中的默认代码使用代码优先行为

我还对IdentityModels.cs中的代码和Startup中的CreatePerOwinContext中的代码感到困惑。有人能给我解释一下或者给我一个101教程的链接吗?我试着在互联网上四处寻找,但仍然找不到合适的文档/分步教程

我的自定义用户存储:

public class UserStore : IUserStore<IdentityUser, int>
{
    public UserStore() { ... }
    public UserStore(MySQLDatabase database) { ... }
    public Task CreateAsync(IdentityUser user) { ... }
    public Task DeleteAsync(IdentityUser user) { ... }
    public Task<IdentityUser> FindByIdAsync(int userId) { ... }
    public Task<IdentityUser> FindByNameAsync(string userName) { ... }
    public Task UpdateAsync(IdentityUser user) { ... }
    public void Dispose() { ... }
}
公共类UserStore:IUserStore
{
公共用户存储(){…}
公共用户存储(MySQLDatabase数据库){…}
公共任务CreateAsync(IdentityUser用户){…}
公共任务DeleteAsync(IdentityUser用户){…}
公共任务FindByIdAsync(int userId){…}
公共任务FindByNameAsync(字符串用户名){…}
公共任务更新同步(IdentityUser用户){…}
public void Dispose(){…}
}
我的自定义标识用户:

public class IdentityUser : IUser<int>
{
    public IdentityUser() { ... }
    public IdentityUser(string userName) { ... }
    public int Id { get; set; }
    public string UserName { get; set; }
}
公共类标识用户:IUser
{
公共标识用户(){…}
公共标识用户(字符串用户名){…}
公共int Id{get;set;}
公共字符串用户名{get;set;}
}
IdentityModels.cs默认代码:

public class ApplicationUser : IdentityUser
{
    public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<ApplicationUser> manager)
    {
        // Note the authenticationType must match the one defined in CookieAuthenticationOptions.AuthenticationType
        var userIdentity = await manager.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie);
        // Add custom user claims here
        return userIdentity;
    }
}

public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
    public ApplicationDbContext()
        : base("DefaultConnection", throwIfV1Schema: false)
    {
    }

    public static ApplicationDbContext Create()
    {
        return new ApplicationDbContext();
    }
}
公共类应用程序用户:IdentityUser
{
公共异步任务GenerateUserIdentityAsync(用户管理器)
{
//注意authenticationType必须与CookieAuthenticationOptions.authenticationType中定义的类型匹配
var userIdentity=wait manager.CreateIdentityAsync(这是DefaultAuthenticationTypes.ApplicationOkie);
//在此处添加自定义用户声明
返回用户身份;
}
}
公共类ApplicationDbContext:IdentityDbContext
{
公共应用程序上下文()
:base(“DefaultConnection”,throwifvv1schema:false)
{
}
公共静态应用程序上下文创建()
{
返回新的ApplicationDbContext();
}
}
启动:

public void ConfigureAuth(IAppBuilder app)
        {
            app.CreatePerOwinContext(ApplicationDbContext.Create);
            app.CreatePerOwinContext<ApplicationUserManager>(ApplicationUserManager.Create);
            app.CreatePerOwinContext<ApplicationSignInManager>(ApplicationSignInManager.Create);
            app.UseCookieAuthentication(new CookieAuthenticationOptions
            {
                AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
                LoginPath = new PathString("/Account/Login"),
                Provider = new CookieAuthenticationProvider
                { 
                    OnValidateIdentity = SecurityStampValidator.OnValidateIdentity<ApplicationUserManager, ApplicationUser>(
                        validateInterval: TimeSpan.FromMinutes(30),
                        regenerateIdentity: (manager, user) => user.GenerateUserIdentityAsync(manager))
                }
            });            
            app.UseExternalSignInCookie(DefaultAuthenticationTypes.ExternalCookie);
            app.UseTwoFactorSignInCookie(DefaultAuthenticationTypes.TwoFactorCookie, TimeSpan.FromMinutes(5));
            app.UseTwoFactorRememberBrowserCookie(DefaultAuthenticationTypes.TwoFactorRememberBrowserCookie);
}
public void ConfigureAuth(IAppBuilder应用程序)
{
app.CreatePerOwinContext(ApplicationDbContext.Create);
app.CreatePerOwinContext(ApplicationUserManager.Create);
app.CreatePerOwinContext(ApplicationSignInManager.Create);
app.UseCookieAuthentication(新的CookieAuthenticationOptions
{
AuthenticationType=DefaultAuthenticationTypes.ApplicationOkie,
LoginPath=新路径字符串(“/Account/Login”),
Provider=新CookieAuthenticationProvider
{ 
OnValidateIdentity=SecurityStampValidator.OnValidateIdentity(
validateInterval:TimeSpan.FromMinutes(30),
regenerateIdentity:(管理器,用户)=>user.GenerateUserIdentityAsync(管理器))
}
});            
app.UseExternalSignInCookie(DefaultAuthenticationTypes.ExternalCookie);
app.UseTwoFactorSignInCookie(DefaultAuthenticationTypes.TwoFactorCookie,TimeSpan.FromMinutes(5));
app.useTowFactoryMemberBrowserCookie(DefaultAuthenticationTypes.TwoFactoryRememberBrowserCookie);
}
如有任何帮助,我们将不胜感激。

您是否已阅读有关和?可能的副本