diff --git a/ApplicantTracking.Data/Identity/ApplicationUserManager.cs b/ApplicantTracking.Data/Identity/ApplicationUserManager.cs index 08942f5..cf3c04c 100644 --- a/ApplicantTracking.Data/Identity/ApplicationUserManager.cs +++ b/ApplicantTracking.Data/Identity/ApplicationUserManager.cs @@ -3,6 +3,7 @@ using Microsoft.AspNet.Identity.EntityFramework; using Microsoft.AspNet.Identity.Owin; using Microsoft.Owin; +using Microsoft.Owin.Security.DataProtection; namespace ApplicantTracking.Data.Identity { @@ -10,23 +11,17 @@ namespace ApplicantTracking.Data.Identity // UserManager is defined in ASP.NET Identity and is used by the application. public class ApplicationUserManager : UserManager { - public ApplicationUserManager(IUserStore store) - : base(store) + public ApplicationUserManager(IUserStore store, IDataProtectionProvider dataProtectionProvider) : base(store) { - } - - public static ApplicationUserManager Create(IdentityFactoryOptions options, IOwinContext context) - { - var manager = new ApplicationUserManager(new UserStore(context.Get())); // Configure validation logic for usernames - manager.UserValidator = new UserValidator(manager) + UserValidator = new UserValidator(this) { AllowOnlyAlphanumericUserNames = false, RequireUniqueEmail = true }; // Configure validation logic for passwords - manager.PasswordValidator = new PasswordValidator + PasswordValidator = new PasswordValidator { RequiredLength = 6, RequireNonLetterOrDigit = true, @@ -36,35 +31,32 @@ public static ApplicationUserManager Create(IdentityFactoryOptions + RegisterTwoFactorProvider("Phone Code", new PhoneNumberTokenProvider { MessageFormat = "Your security code is {0}" }); - manager.RegisterTwoFactorProvider("Email Code", new EmailTokenProvider + RegisterTwoFactorProvider("Email Code", new EmailTokenProvider { Subject = "Security Code", BodyFormat = "Your security code is {0}" }); - manager.EmailService = new EmailService(); - manager.SmsService = new SmsService(); - - var dataProtectionProvider = options.DataProtectionProvider; + EmailService = new EmailService(); + SmsService = new SmsService(); + // Generate user tokens to confirm account registration and for password reset tokens if (dataProtectionProvider != null) { - manager.UserTokenProvider = + UserTokenProvider = new DataProtectorTokenProvider(dataProtectionProvider.Create("ASP.NET Identity")); } - - return manager; } } } diff --git a/ApplicantTracking.Web/App_Start/Startup.Auth.cs b/ApplicantTracking.Web/App_Start/Startup.Auth.cs index 8546d62..23aac5a 100644 --- a/ApplicantTracking.Web/App_Start/Startup.Auth.cs +++ b/ApplicantTracking.Web/App_Start/Startup.Auth.cs @@ -1,4 +1,5 @@ using System; +using System.Web.Mvc; using Microsoft.AspNet.Identity; using Microsoft.AspNet.Identity.Owin; using Microsoft.Owin; @@ -14,10 +15,9 @@ public partial class Startup // For more information on configuring authentication, please visit http://go.microsoft.com/fwlink/?LinkId=301864 public void ConfigureAuth(IAppBuilder app) { - // Configure the db context, user manager and signin manager to use a single instance per request - app.CreatePerOwinContext(ApplicationDbContext.Create); - app.CreatePerOwinContext(ApplicationUserManager.Create); - app.CreatePerOwinContext(ApplicationSignInManager.Create); + // Configure the user manager and signin manager to use a single instance per request + app.CreatePerOwinContext(() => DependencyResolver.Current.GetService()); + app.CreatePerOwinContext(() => DependencyResolver.Current.GetService()); // Enable the application to use a cookie to store information for the signed in user // and to use a cookie to temporarily store information about a user logging in with a third party login provider diff --git a/ApplicantTracking.Web/Controllers/AccountController.cs b/ApplicantTracking.Web/Controllers/AccountController.cs index b001041..783caf7 100644 --- a/ApplicantTracking.Web/Controllers/AccountController.cs +++ b/ApplicantTracking.Web/Controllers/AccountController.cs @@ -13,41 +13,30 @@ namespace ApplicantTracking.Web.Controllers [Authorize] public class AccountController : Controller { - private ApplicationSignInManager _signInManager; - private ApplicationUserManager _userManager; + private readonly ApplicationUserManager _userManager; + private readonly ApplicationSignInManager _signInManager; + private readonly IAuthenticationManager _authenticationManager; - public AccountController() + public AccountController(ApplicationUserManager userManager, ApplicationSignInManager signInManager, IAuthenticationManager authenticationManager) { + _userManager = userManager; + _signInManager = signInManager; + _authenticationManager = authenticationManager; } - public AccountController(ApplicationUserManager userManager, ApplicationSignInManager signInManager ) + public ApplicationUserManager UserManager { - UserManager = userManager; - SignInManager = signInManager; + get { return _userManager; } } public ApplicationSignInManager SignInManager { - get - { - return _signInManager ?? HttpContext.GetOwinContext().Get(); - } - private set - { - _signInManager = value; - } + get { return _signInManager; } } - public ApplicationUserManager UserManager + private IAuthenticationManager AuthenticationManager { - get - { - return _userManager ?? HttpContext.GetOwinContext().GetUserManager(); - } - private set - { - _userManager = value; - } + get { return _authenticationManager; } } // @@ -405,16 +394,14 @@ protected override void Dispose(bool disposing) { if (disposing) { - if (_userManager != null) + if (UserManager != null) { - _userManager.Dispose(); - _userManager = null; + UserManager.Dispose(); } - if (_signInManager != null) + if (SignInManager != null) { - _signInManager.Dispose(); - _signInManager = null; + SignInManager.Dispose(); } } @@ -425,14 +412,6 @@ protected override void Dispose(bool disposing) // Used for XSRF protection when adding external logins private const string XsrfKey = "XsrfId"; - private IAuthenticationManager AuthenticationManager - { - get - { - return HttpContext.GetOwinContext().Authentication; - } - } - private void AddErrors(IdentityResult result) { foreach (var error in result.Errors) diff --git a/ApplicantTracking.Web/Startup.cs b/ApplicantTracking.Web/Startup.cs index 1a5a2bc..bc75360 100644 --- a/ApplicantTracking.Web/Startup.cs +++ b/ApplicantTracking.Web/Startup.cs @@ -1,5 +1,7 @@ using Microsoft.Owin; +using Microsoft.Owin.Security.DataProtection; using Owin; +using Unity.Lifetime; [assembly: OwinStartup(typeof(ApplicantTracking.Web.Startup))] namespace ApplicantTracking.Web @@ -8,6 +10,9 @@ public partial class Startup { public void Configuration(IAppBuilder app) { + UnityConfig.Container.RegisterInstance(typeof(IDataProtectionProvider), + null, app.GetDataProtectionProvider(), new TransientLifetimeManager()); + ConfigureAuth(app); } }