I have a MVC5 project and I've been following this tutorial:
Basically I want to use my own database than using the code-first default approach. I've got all my custom Store and Identity covered, but I have no idea how to change my application to use my custom Context because the default code from MVC5 project use code-first behaviour.
I'm also confused about the code in IdentityModels.cs and CreatePerOwinContext in Startup does. Can someone explain this to me or give me a link to 101 tutorial? I've tried looking around the Internet and SO but I still can't find a proper documentation/step by step tutorial.
My custom UserStore:
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() { ... }
}
My custom IdentityUser:
public class IdentityUser : IUser<int>
{
public IdentityUser() { ... }
public IdentityUser(string userName) { ... }
public int Id { get; set; }
public string UserName { get; set; }
}
IdentityModels.cs default code:
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();
}
}
StartUp:
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);
}
Any help will be appreciated.