Identity DI changes
This commit is contained in:
parent
737afbd610
commit
5ba60857a7
|
|
@ -14,7 +14,7 @@ namespace MusicStore.Controllers
|
||||||
{
|
{
|
||||||
public UserManager<ApplicationUser> UserManager
|
public UserManager<ApplicationUser> UserManager
|
||||||
{
|
{
|
||||||
get { return Context.ApplicationServices.GetService<UserManager<ApplicationUser>>(); }
|
get { return Context.ApplicationServices.GetService<ApplicationUserManager>(); }
|
||||||
}
|
}
|
||||||
|
|
||||||
private SignInManager<ApplicationUser> _signInManager;
|
private SignInManager<ApplicationUser> _signInManager;
|
||||||
|
|
@ -23,7 +23,7 @@ namespace MusicStore.Controllers
|
||||||
{
|
{
|
||||||
if (_signInManager == null)
|
if (_signInManager == null)
|
||||||
{
|
{
|
||||||
_signInManager = new SignInManager<ApplicationUser>()
|
_signInManager = new SignInManager<ApplicationUser>
|
||||||
{
|
{
|
||||||
UserManager = UserManager,
|
UserManager = UserManager,
|
||||||
Context = Context,
|
Context = Context,
|
||||||
|
|
|
||||||
|
|
@ -1,7 +1,18 @@
|
||||||
using Microsoft.AspNet.Identity;
|
using System;
|
||||||
|
using Microsoft.AspNet.Identity;
|
||||||
|
|
||||||
namespace MusicStore.Models
|
namespace MusicStore.Models
|
||||||
{
|
{
|
||||||
|
public class ApplicationUserManager : UserManager<ApplicationUser>
|
||||||
|
{
|
||||||
|
public ApplicationUserManager(IServiceProvider services) : base(services) { }
|
||||||
|
}
|
||||||
|
|
||||||
|
public class ApplicationRoleManager : RoleManager<IdentityRole>
|
||||||
|
{
|
||||||
|
public ApplicationRoleManager(IServiceProvider services) : base(services) { }
|
||||||
|
}
|
||||||
|
|
||||||
public class ApplicationUser : IdentityUser
|
public class ApplicationUser : IdentityUser
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -6,6 +6,7 @@ using Microsoft.AspNet.DependencyInjection;
|
||||||
using Microsoft.AspNet.DependencyInjection.Fallback;
|
using Microsoft.AspNet.DependencyInjection.Fallback;
|
||||||
using Microsoft.AspNet.Diagnostics;
|
using Microsoft.AspNet.Diagnostics;
|
||||||
using Microsoft.AspNet.Identity;
|
using Microsoft.AspNet.Identity;
|
||||||
|
using Microsoft.AspNet.Identity.Entity;
|
||||||
using Microsoft.AspNet.Identity.InMemory;
|
using Microsoft.AspNet.Identity.InMemory;
|
||||||
using Microsoft.AspNet.Identity.Security;
|
using Microsoft.AspNet.Identity.Security;
|
||||||
using Microsoft.AspNet.Logging;
|
using Microsoft.AspNet.Logging;
|
||||||
|
|
@ -21,11 +22,11 @@ using MusicStore.Logging;
|
||||||
using MusicStore.Models;
|
using MusicStore.Models;
|
||||||
using MusicStore.Web.Models;
|
using MusicStore.Web.Models;
|
||||||
using System;
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
using System.IO;
|
using System.IO;
|
||||||
|
|
||||||
public class Startup
|
public class Startup
|
||||||
{
|
{
|
||||||
|
|
||||||
public void Configuration(IBuilder app)
|
public void Configuration(IBuilder app)
|
||||||
{
|
{
|
||||||
app.UseServices(services =>
|
app.UseServices(services =>
|
||||||
|
|
@ -38,8 +39,19 @@ public class Startup
|
||||||
services.AddEntityFramework(s => s.AddInMemoryStore());
|
services.AddEntityFramework(s => s.AddInMemoryStore());
|
||||||
#endif
|
#endif
|
||||||
services.AddTransient<MusicStoreContext, MusicStoreContext>();
|
services.AddTransient<MusicStoreContext, MusicStoreContext>();
|
||||||
services.AddInstance<UserManager<ApplicationUser>>(new UserManager<ApplicationUser>(new InMemoryUserStore<ApplicationUser>()));
|
// File an issue trying to use IdentityUser/IdentityRole and open generic UserManager<>
|
||||||
services.AddInstance<RoleManager<IdentityRole>>(new RoleManager<IdentityRole>(new InMemoryRoleStore<IdentityRole>()));
|
services.AddIdentity<ApplicationUser, IdentityRole>(s =>
|
||||||
|
{
|
||||||
|
// Turn off password defaults since register error display blows up
|
||||||
|
s.UsePasswordValidator(() => new PasswordValidator());
|
||||||
|
|
||||||
|
//s.UseDbContext(() => context);
|
||||||
|
//s.UseUserStore(() => new UserStore(context));
|
||||||
|
s.UseUserStore(() => new InMemoryUserStore<ApplicationUser>());
|
||||||
|
s.UseUserManager<ApplicationUserManager>();
|
||||||
|
s.UseRoleStore(() => new InMemoryRoleStore<IdentityRole>());
|
||||||
|
s.UseRoleManager<ApplicationRoleManager>();
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
//ErrorPageOptions.ShowAll to be used only at development time. Not recommended for production.
|
//ErrorPageOptions.ShowAll to be used only at development time. Not recommended for production.
|
||||||
|
|
@ -50,7 +62,13 @@ public class Startup
|
||||||
app.UseCookieAuthentication(new CookieAuthenticationOptions()
|
app.UseCookieAuthentication(new CookieAuthenticationOptions()
|
||||||
{
|
{
|
||||||
AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
|
AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
|
||||||
LoginPath = new PathString("/Account/Login")
|
LoginPath = new PathString("/Account/Login"),
|
||||||
|
Notifications = new CookieAuthenticationNotifications
|
||||||
|
{
|
||||||
|
//OnValidateIdentity = SecurityStampValidator.OnValidateIdentity<ApplicationUserManager, ApplicationUser>(
|
||||||
|
// validateInterval: TimeSpan.FromMinutes(30),
|
||||||
|
// regenerateIdentity: (manager, user) => user.GenerateUserIdentityAsync(manager))
|
||||||
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
app.UseMvc(routes =>
|
app.UseMvc(routes =>
|
||||||
|
|
@ -76,26 +94,24 @@ public class Startup
|
||||||
configuration.AddEnvironmentVariables(); //If configuration flows through environment we should pick that first
|
configuration.AddEnvironmentVariables(); //If configuration flows through environment we should pick that first
|
||||||
configuration.AddJsonFile(Path.Combine(applicationEnvironment.ApplicationBasePath, "Config.json"));
|
configuration.AddJsonFile(Path.Combine(applicationEnvironment.ApplicationBasePath, "Config.json"));
|
||||||
|
|
||||||
string _username = configuration.Get("DefaultAdminUsername");
|
var userName = configuration.Get("DefaultAdminUsername");
|
||||||
string _password = configuration.Get("DefaultAdminPassword");
|
var password = configuration.Get("DefaultAdminPassword");
|
||||||
string _role = "Administrator";
|
const string adminRole = "Administrator";
|
||||||
|
|
||||||
var userManager = serviceProvider.GetService<UserManager<ApplicationUser>>();
|
var userManager = serviceProvider.GetService<ApplicationUserManager>();
|
||||||
var roleManager = serviceProvider.GetService<RoleManager<IdentityRole>>();
|
var roleManager = serviceProvider.GetService<ApplicationRoleManager>();
|
||||||
|
|
||||||
var role = new IdentityRole(_role);
|
if (!await roleManager.RoleExistsAsync(adminRole))
|
||||||
var result = await roleManager.RoleExistsAsync(_role);
|
|
||||||
if (result == false)
|
|
||||||
{
|
{
|
||||||
await roleManager.CreateAsync(role);
|
await roleManager.CreateAsync(new IdentityRole(adminRole));
|
||||||
}
|
}
|
||||||
|
|
||||||
var user = await userManager.FindByNameAsync(_username);
|
var user = await userManager.FindByNameAsync(userName);
|
||||||
if (user == null)
|
if (user == null)
|
||||||
{
|
{
|
||||||
user = new ApplicationUser { UserName = _username };
|
user = new ApplicationUser { UserName = userName };
|
||||||
await userManager.CreateAsync(user, _password);
|
await userManager.CreateAsync(user, password);
|
||||||
await userManager.AddToRoleAsync(user, _role);
|
await userManager.AddToRoleAsync(user, adminRole);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Loading…
Reference in New Issue