Clean up identity DI

This commit is contained in:
Hao Kung 2014-05-12 15:51:51 -07:00
parent 3fcfaeb4e0
commit 9deabcb459
3 changed files with 12 additions and 50 deletions

View File

@ -11,7 +11,7 @@ namespace MusicStore.Controllers
[Authorize] [Authorize]
public class AccountController : Controller public class AccountController : Controller
{ {
public AccountController(ApplicationUserManager userManager, ApplicationSignInManager signInManager) public AccountController(UserManager<ApplicationUser> userManager, SignInManager<ApplicationUser> signInManager)
{ {
UserManager = userManager; UserManager = userManager;
SignInManager = signInManager; SignInManager = signInManager;
@ -19,14 +19,14 @@ namespace MusicStore.Controllers
public UserManager<ApplicationUser> UserManager { get; private set; } public UserManager<ApplicationUser> UserManager { get; private set; }
public ApplicationSignInManager SignInManager { get; private set; } public SignInManager<ApplicationUser> SignInManager { get; private set; }
// //
// GET: /Account/Login // GET: /Account/Login
[AllowAnonymous] [AllowAnonymous]
//Bug: https://github.com/aspnet/WebFx/issues/339 //Bug: https://github.com/aspnet/WebFx/issues/339
[HttpGet] [HttpGet]
public IActionResult Login(string returnUrl) public IActionResult Login(string returnUrl=null)
{ {
ViewBag.ReturnUrl = returnUrl; ViewBag.ReturnUrl = returnUrl;
return View(); return View();
@ -37,7 +37,7 @@ namespace MusicStore.Controllers
[HttpPost] [HttpPost]
[AllowAnonymous] [AllowAnonymous]
[ValidateAntiForgeryToken] [ValidateAntiForgeryToken]
public async Task<IActionResult> Login(LoginViewModel model, string returnUrl) public async Task<IActionResult> Login(LoginViewModel model, string returnUrl=null)
{ {
if (ModelState.IsValid) if (ModelState.IsValid)
{ {
@ -100,7 +100,7 @@ namespace MusicStore.Controllers
// GET: /Account/Manage // GET: /Account/Manage
//Bug: https://github.com/aspnet/WebFx/issues/339 //Bug: https://github.com/aspnet/WebFx/issues/339
[HttpGet] [HttpGet]
public IActionResult Manage(ManageMessageId? message) public IActionResult Manage(ManageMessageId? message=null)
{ {
ViewBag.StatusMessage = ViewBag.StatusMessage =
message == ManageMessageId.ChangePasswordSuccess ? "Your password has been changed." message == ManageMessageId.ChangePasswordSuccess ? "Your password has been changed."

View File

@ -1,29 +1,10 @@
using System; using System;
using Microsoft.AspNet.Http;
using Microsoft.AspNet.Identity;
using Microsoft.AspNet.Identity.Entity; using Microsoft.AspNet.Identity.Entity;
using Microsoft.AspNet.Identity.Security;
using Microsoft.Data.Entity; using Microsoft.Data.Entity;
using Microsoft.Framework.ConfigurationModel; using Microsoft.Framework.ConfigurationModel;
using Microsoft.Framework.DependencyInjection;
namespace MusicStore.Models 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 ApplicationUser : User { }
public class ApplicationDbContext : IdentitySqlContext<ApplicationUser> public class ApplicationDbContext : IdentitySqlContext<ApplicationUser>

View File

@ -1,27 +1,15 @@
using Microsoft.AspNet;
using Microsoft.AspNet.Http;
using Microsoft.AspNet.Builder; using Microsoft.AspNet.Builder;
using Microsoft.Framework.ConfigurationModel;
using Microsoft.Framework.DependencyInjection;
using Microsoft.Framework.DependencyInjection.Fallback;
using Microsoft.AspNet.Diagnostics; using Microsoft.AspNet.Diagnostics;
using Microsoft.AspNet.Http;
using Microsoft.AspNet.Identity; using Microsoft.AspNet.Identity;
using Microsoft.AspNet.Identity.Entity;
using Microsoft.AspNet.Identity.InMemory;
using Microsoft.AspNet.Identity.Security; using Microsoft.AspNet.Identity.Security;
using Microsoft.Framework.Logging;
using Microsoft.AspNet.Mvc;
using Microsoft.AspNet.RequestContainer;
using Microsoft.AspNet.Routing; using Microsoft.AspNet.Routing;
using Microsoft.AspNet.Security.Cookies; using Microsoft.AspNet.Security.Cookies;
using Microsoft.Data.Entity; using Microsoft.Data.Entity;
using Microsoft.Data.Entity.InMemory; using Microsoft.Framework.ConfigurationModel;
using Microsoft.Data.Entity.SqlServer; using Microsoft.Framework.DependencyInjection;
using Microsoft.Framework.Runtime;
using MusicStore.Models; using MusicStore.Models;
using System; using System;
using System.Collections.Generic;
using System.IO;
using System.Security.Claims; using System.Security.Claims;
using System.Threading.Tasks; using System.Threading.Tasks;
@ -47,20 +35,13 @@ public class Startup
services.AddEntityFramework().AddSqlServer(); services.AddEntityFramework().AddSqlServer();
services.AddTransient<MusicStoreContext>(); services.AddTransient<MusicStoreContext>();
//Add all Identity related services to IoC.
/*
* Add all Identity related services to IoC.
*/
services.AddTransient<DbContext, ApplicationDbContext>(); services.AddTransient<DbContext, ApplicationDbContext>();
//Bug: https://github.com/aspnet/Identity/issues/50
services.AddIdentity<ApplicationUser, IdentityRole>(s => services.AddIdentity<ApplicationUser, IdentityRole>(s =>
{ {
s.AddEntity(); 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"); var password = configuration.Get("DefaultAdminPassword");
//const string adminRole = "Administrator"; //const string adminRole = "Administrator";
var userManager = serviceProvider.GetService<ApplicationUserManager>(); var userManager = serviceProvider.GetService<UserManager<ApplicationUser>>();
// Todo: identity sql does not support roles yet // Todo: identity sql does not support roles yet
//var roleManager = serviceProvider.GetService<ApplicationRoleManager>(); //var roleManager = serviceProvider.GetService<RoleManager<IdentityRole>>();
//if (!await roleManager.RoleExistsAsync(adminRole)) //if (!await roleManager.RoleExistsAsync(adminRole))
//{ //{
// await roleManager.CreateAsync(new IdentityRole(adminRole)); // await roleManager.CreateAsync(new IdentityRole(adminRole));