Clean up identity DI
This commit is contained in:
parent
3fcfaeb4e0
commit
9deabcb459
|
|
@ -11,7 +11,7 @@ namespace MusicStore.Controllers
|
|||
[Authorize]
|
||||
public class AccountController : Controller
|
||||
{
|
||||
public AccountController(ApplicationUserManager userManager, ApplicationSignInManager signInManager)
|
||||
public AccountController(UserManager<ApplicationUser> userManager, SignInManager<ApplicationUser> signInManager)
|
||||
{
|
||||
UserManager = userManager;
|
||||
SignInManager = signInManager;
|
||||
|
|
@ -19,14 +19,14 @@ namespace MusicStore.Controllers
|
|||
|
||||
public UserManager<ApplicationUser> UserManager { get; private set; }
|
||||
|
||||
public ApplicationSignInManager SignInManager { get; private set; }
|
||||
public SignInManager<ApplicationUser> SignInManager { get; private set; }
|
||||
|
||||
//
|
||||
// GET: /Account/Login
|
||||
[AllowAnonymous]
|
||||
//Bug: https://github.com/aspnet/WebFx/issues/339
|
||||
[HttpGet]
|
||||
public IActionResult Login(string returnUrl)
|
||||
public IActionResult Login(string returnUrl=null)
|
||||
{
|
||||
ViewBag.ReturnUrl = returnUrl;
|
||||
return View();
|
||||
|
|
@ -37,7 +37,7 @@ namespace MusicStore.Controllers
|
|||
[HttpPost]
|
||||
[AllowAnonymous]
|
||||
[ValidateAntiForgeryToken]
|
||||
public async Task<IActionResult> Login(LoginViewModel model, string returnUrl)
|
||||
public async Task<IActionResult> Login(LoginViewModel model, string returnUrl=null)
|
||||
{
|
||||
if (ModelState.IsValid)
|
||||
{
|
||||
|
|
@ -100,7 +100,7 @@ namespace MusicStore.Controllers
|
|||
// GET: /Account/Manage
|
||||
//Bug: https://github.com/aspnet/WebFx/issues/339
|
||||
[HttpGet]
|
||||
public IActionResult Manage(ManageMessageId? message)
|
||||
public IActionResult Manage(ManageMessageId? message=null)
|
||||
{
|
||||
ViewBag.StatusMessage =
|
||||
message == ManageMessageId.ChangePasswordSuccess ? "Your password has been changed."
|
||||
|
|
|
|||
|
|
@ -1,29 +1,10 @@
|
|||
using System;
|
||||
using Microsoft.AspNet.Http;
|
||||
using Microsoft.AspNet.Identity;
|
||||
using Microsoft.AspNet.Identity.Entity;
|
||||
using Microsoft.AspNet.Identity.Security;
|
||||
using Microsoft.Data.Entity;
|
||||
using Microsoft.Framework.ConfigurationModel;
|
||||
using Microsoft.Framework.DependencyInjection;
|
||||
|
||||
namespace MusicStore.Models
|
||||
{
|
||||
public class ApplicationUserManager : UserManager<ApplicationUser>
|
||||
{
|
||||
public ApplicationUserManager(IServiceProvider services, IUserStore<ApplicationUser> store, IOptionsAccessor<IdentityOptions> optionsAccessor) : base(services, store, optionsAccessor) { }
|
||||
}
|
||||
|
||||
public class ApplicationRoleManager : RoleManager<IdentityRole>
|
||||
{
|
||||
public ApplicationRoleManager(IServiceProvider services, IRoleStore<IdentityRole> store) : base(services, store) { }
|
||||
}
|
||||
|
||||
public class ApplicationSignInManager : SignInManager<ApplicationUserManager, ApplicationUser>
|
||||
{
|
||||
public ApplicationSignInManager(ApplicationUserManager manager, IContextAccessor<HttpContext> contextAccessor) : base(manager, contextAccessor) { }
|
||||
}
|
||||
|
||||
public class ApplicationUser : User { }
|
||||
|
||||
public class ApplicationDbContext : IdentitySqlContext<ApplicationUser>
|
||||
|
|
|
|||
|
|
@ -1,27 +1,15 @@
|
|||
using Microsoft.AspNet;
|
||||
using Microsoft.AspNet.Http;
|
||||
using Microsoft.AspNet.Builder;
|
||||
using Microsoft.Framework.ConfigurationModel;
|
||||
using Microsoft.Framework.DependencyInjection;
|
||||
using Microsoft.Framework.DependencyInjection.Fallback;
|
||||
using Microsoft.AspNet.Diagnostics;
|
||||
using Microsoft.AspNet.Http;
|
||||
using Microsoft.AspNet.Identity;
|
||||
using Microsoft.AspNet.Identity.Entity;
|
||||
using Microsoft.AspNet.Identity.InMemory;
|
||||
using Microsoft.AspNet.Identity.Security;
|
||||
using Microsoft.Framework.Logging;
|
||||
using Microsoft.AspNet.Mvc;
|
||||
using Microsoft.AspNet.RequestContainer;
|
||||
using Microsoft.AspNet.Routing;
|
||||
using Microsoft.AspNet.Security.Cookies;
|
||||
using Microsoft.Data.Entity;
|
||||
using Microsoft.Data.Entity.InMemory;
|
||||
using Microsoft.Data.Entity.SqlServer;
|
||||
using Microsoft.Framework.Runtime;
|
||||
using Microsoft.Framework.ConfigurationModel;
|
||||
using Microsoft.Framework.DependencyInjection;
|
||||
using MusicStore.Models;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Security.Claims;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
|
|
@ -47,20 +35,13 @@ public class Startup
|
|||
services.AddEntityFramework().AddSqlServer();
|
||||
services.AddTransient<MusicStoreContext>();
|
||||
|
||||
|
||||
/*
|
||||
* Add all Identity related services to IoC.
|
||||
*/
|
||||
//Add all Identity related services to IoC.
|
||||
services.AddTransient<DbContext, ApplicationDbContext>();
|
||||
|
||||
//Bug: https://github.com/aspnet/Identity/issues/50
|
||||
services.AddIdentity<ApplicationUser, IdentityRole>(s =>
|
||||
{
|
||||
s.AddEntity();
|
||||
s.AddUserManager<ApplicationUserManager>();
|
||||
s.AddRoleManager<ApplicationRoleManager>();
|
||||
});
|
||||
services.AddTransient<ApplicationSignInManager>();
|
||||
services.AddTransient<SignInManager<ApplicationUser>>();
|
||||
});
|
||||
|
||||
|
||||
|
|
@ -106,9 +87,9 @@ public class Startup
|
|||
var password = configuration.Get("DefaultAdminPassword");
|
||||
//const string adminRole = "Administrator";
|
||||
|
||||
var userManager = serviceProvider.GetService<ApplicationUserManager>();
|
||||
var userManager = serviceProvider.GetService<UserManager<ApplicationUser>>();
|
||||
// Todo: identity sql does not support roles yet
|
||||
//var roleManager = serviceProvider.GetService<ApplicationRoleManager>();
|
||||
//var roleManager = serviceProvider.GetService<RoleManager<IdentityRole>>();
|
||||
//if (!await roleManager.RoleExistsAsync(adminRole))
|
||||
//{
|
||||
// await roleManager.CreateAsync(new IdentityRole(adminRole));
|
||||
|
|
|
|||
Loading…
Reference in New Issue