Skip to content

Commit

Permalink
Refactorizar AccountController
Browse files Browse the repository at this point in the history
Remover las referencias y dependencias a OwinContext dentro del Controller
  • Loading branch information
feliperomero3 committed Apr 21, 2018
1 parent 30a39d0 commit 9e55e55
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 62 deletions.
34 changes: 13 additions & 21 deletions ApplicantTracking.Data/Identity/ApplicationUserManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,30 +3,25 @@
using Microsoft.AspNet.Identity.EntityFramework;
using Microsoft.AspNet.Identity.Owin;
using Microsoft.Owin;
using Microsoft.Owin.Security.DataProtection;

namespace ApplicantTracking.Data.Identity
{
// Configure the application user manager used in this application.
// UserManager is defined in ASP.NET Identity and is used by the application.
public class ApplicationUserManager : UserManager<ApplicationUser>
{
public ApplicationUserManager(IUserStore<ApplicationUser> store)
: base(store)
public ApplicationUserManager(IUserStore<ApplicationUser> store, IDataProtectionProvider dataProtectionProvider) : base(store)
{
}

public static ApplicationUserManager Create(IdentityFactoryOptions<ApplicationUserManager> options, IOwinContext context)
{
var manager = new ApplicationUserManager(new UserStore<ApplicationUser>(context.Get<ApplicationDbContext>()));
// Configure validation logic for usernames
manager.UserValidator = new UserValidator<ApplicationUser>(manager)
UserValidator = new UserValidator<ApplicationUser>(this)
{
AllowOnlyAlphanumericUserNames = false,
RequireUniqueEmail = true
};

// Configure validation logic for passwords
manager.PasswordValidator = new PasswordValidator
PasswordValidator = new PasswordValidator
{
RequiredLength = 6,
RequireNonLetterOrDigit = true,
Expand All @@ -36,35 +31,32 @@ public static ApplicationUserManager Create(IdentityFactoryOptions<ApplicationUs
};

// Configure user lockout defaults
manager.UserLockoutEnabledByDefault = true;
manager.DefaultAccountLockoutTimeSpan = TimeSpan.FromMinutes(5);
manager.MaxFailedAccessAttemptsBeforeLockout = 5;
UserLockoutEnabledByDefault = true;
DefaultAccountLockoutTimeSpan = TimeSpan.FromMinutes(5);
MaxFailedAccessAttemptsBeforeLockout = 5;

// Register two factor authentication providers. This application uses Phone and Emails as a step of receiving a code for verifying the user
// You can write your own provider and plug it in here.
manager.RegisterTwoFactorProvider("Phone Code", new PhoneNumberTokenProvider<ApplicationUser>
RegisterTwoFactorProvider("Phone Code", new PhoneNumberTokenProvider<ApplicationUser>
{
MessageFormat = "Your security code is {0}"
});

manager.RegisterTwoFactorProvider("Email Code", new EmailTokenProvider<ApplicationUser>
RegisterTwoFactorProvider("Email Code", new EmailTokenProvider<ApplicationUser>
{
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<ApplicationUser>(dataProtectionProvider.Create("ASP.NET Identity"));
}

return manager;
}
}
}
8 changes: 4 additions & 4 deletions ApplicantTracking.Web/App_Start/Startup.Auth.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Web.Mvc;
using Microsoft.AspNet.Identity;
using Microsoft.AspNet.Identity.Owin;
using Microsoft.Owin;
Expand All @@ -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>(ApplicationUserManager.Create);
app.CreatePerOwinContext<ApplicationSignInManager>(ApplicationSignInManager.Create);
// Configure the user manager and signin manager to use a single instance per request
app.CreatePerOwinContext(() => DependencyResolver.Current.GetService<ApplicationUserManager>());
app.CreatePerOwinContext(() => DependencyResolver.Current.GetService<ApplicationSignInManager>());

// 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
Expand Down
53 changes: 16 additions & 37 deletions ApplicantTracking.Web/Controllers/AccountController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<ApplicationSignInManager>();
}
private set
{
_signInManager = value;
}
get { return _signInManager; }
}

public ApplicationUserManager UserManager
private IAuthenticationManager AuthenticationManager
{
get
{
return _userManager ?? HttpContext.GetOwinContext().GetUserManager<ApplicationUserManager>();
}
private set
{
_userManager = value;
}
get { return _authenticationManager; }
}

//
Expand Down Expand Up @@ -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();
}
}

Expand All @@ -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)
Expand Down
5 changes: 5 additions & 0 deletions ApplicantTracking.Web/Startup.cs
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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);
}
}
Expand Down

0 comments on commit 9e55e55

Please sign in to comment.