Use DI for Identity
This commit is contained in:
parent
e61107a381
commit
3c4a2df2cb
|
|
@ -1,4 +1,5 @@
|
||||||
using Microsoft.AspNet.Identity;
|
using Microsoft.AspNet.DependencyInjection;
|
||||||
|
using Microsoft.AspNet.Identity;
|
||||||
using Microsoft.AspNet.Identity.Security;
|
using Microsoft.AspNet.Identity.Security;
|
||||||
using Microsoft.AspNet.Mvc;
|
using Microsoft.AspNet.Mvc;
|
||||||
using Microsoft.AspNet.Mvc.ModelBinding;
|
using Microsoft.AspNet.Mvc.ModelBinding;
|
||||||
|
|
@ -11,20 +12,11 @@ namespace MusicStore.Controllers
|
||||||
//[Authorize]
|
//[Authorize]
|
||||||
public class AccountController : Controller
|
public class AccountController : Controller
|
||||||
{
|
{
|
||||||
public AccountController()
|
public UserManager<ApplicationUser> UserManager
|
||||||
//Bug: Using an in memory store
|
|
||||||
//: this(new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(new ApplicationDbContext())))
|
|
||||||
: this(new UserManager<ApplicationUser>(Startup.UserStore))
|
|
||||||
{
|
{
|
||||||
|
get { return Context.ApplicationServices.GetService<UserManager<ApplicationUser>>(); }
|
||||||
}
|
}
|
||||||
|
|
||||||
public AccountController(UserManager<ApplicationUser> userManager)
|
|
||||||
{
|
|
||||||
UserManager = userManager;
|
|
||||||
}
|
|
||||||
|
|
||||||
public UserManager<ApplicationUser> UserManager { get; set; }
|
|
||||||
|
|
||||||
private SignInManager<ApplicationUser> _signInManager;
|
private SignInManager<ApplicationUser> _signInManager;
|
||||||
public SignInManager<ApplicationUser> SignInManager {
|
public SignInManager<ApplicationUser> SignInManager {
|
||||||
get
|
get
|
||||||
|
|
|
||||||
|
|
@ -1,8 +1,8 @@
|
||||||
using System.Security.Principal;
|
using Microsoft.AspNet.Mvc;
|
||||||
using Microsoft.AspNet.Mvc;
|
|
||||||
using MusicStore.Models;
|
using MusicStore.Models;
|
||||||
using System;
|
using System;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
|
using System.Security.Principal;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
namespace MusicStore.Controllers
|
namespace MusicStore.Controllers
|
||||||
|
|
|
||||||
|
|
@ -22,22 +22,18 @@ using System.IO;
|
||||||
|
|
||||||
public class Startup
|
public class Startup
|
||||||
{
|
{
|
||||||
/// <summary>
|
|
||||||
/// TODO: Temporary ugly work around (making this static) to enable creating a static InMemory UserManager. Will go away shortly.
|
|
||||||
/// </summary>
|
|
||||||
public static InMemoryUserStore<ApplicationUser> UserStore = new InMemoryUserStore<ApplicationUser>();
|
|
||||||
public static InMemoryRoleStore<IdentityRole> RoleStore = new InMemoryRoleStore<IdentityRole>();
|
|
||||||
|
|
||||||
private static void ConfigureServices(ServiceCollection services)
|
private static void ConfigureServices(ServiceCollection services)
|
||||||
{
|
{
|
||||||
services.AddInstance<ILoggerFactory>(new NullLoggerFactory());
|
services.AddInstance<ILoggerFactory>(new NullLoggerFactory());
|
||||||
services.AddMvc();
|
services.AddMvc();
|
||||||
|
|
||||||
|
services.AddInstance<UserManager<ApplicationUser>>(new UserManager<ApplicationUser>(new InMemoryUserStore<ApplicationUser>()));
|
||||||
|
services.AddInstance<RoleManager<IdentityRole>>(new RoleManager<IdentityRole>(new InMemoryRoleStore<IdentityRole>()));
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Configuration(IBuilder app)
|
public void Configuration(IBuilder app)
|
||||||
{
|
{
|
||||||
CreateAdminUser(app.ServiceProvider);
|
|
||||||
|
|
||||||
app.UseContainer(ConfigureServices);
|
app.UseContainer(ConfigureServices);
|
||||||
|
|
||||||
//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.
|
||||||
|
|
@ -67,6 +63,7 @@ public class Startup
|
||||||
app.UseRouter(routes);
|
app.UseRouter(routes);
|
||||||
|
|
||||||
SampleData.InitializeMusicStoreDatabaseAsync().Wait();
|
SampleData.InitializeMusicStoreDatabaseAsync().Wait();
|
||||||
|
CreateAdminUser(app.ServiceProvider);
|
||||||
}
|
}
|
||||||
|
|
||||||
private async void CreateAdminUser(IServiceProvider serviceProvider)
|
private async void CreateAdminUser(IServiceProvider serviceProvider)
|
||||||
|
|
@ -81,8 +78,8 @@ public class Startup
|
||||||
string _password = configuration.Get("DefaultAdminPassword");
|
string _password = configuration.Get("DefaultAdminPassword");
|
||||||
string _role = "Administrator";
|
string _role = "Administrator";
|
||||||
|
|
||||||
var userManager = new UserManager<ApplicationUser>(UserStore);
|
var userManager = serviceProvider.GetService<UserManager<ApplicationUser>>();
|
||||||
var roleManager = new RoleManager<IdentityRole>(RoleStore);
|
var roleManager = serviceProvider.GetService<RoleManager<IdentityRole>>();
|
||||||
|
|
||||||
var role = new IdentityRole(_role);
|
var role = new IdentityRole(_role);
|
||||||
var result = await roleManager.RoleExistsAsync(_role);
|
var result = await roleManager.RoleExistsAsync(_role);
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue