Let's use Autofac's IoC container for dependency injection.
Using the Nuget package manager, install the necessary packages:
We make the following changes to the Startup class:
private void ConfigureContainer(IAppBuilder app) { var builder = new ContainerBuilder(); // STANDARD MVC SETUP: // Register your MVC controllers. builder.RegisterControllers(typeof(MvcApplication).Assembly); // Run other optional steps, like registering model binders, // web abstractions, etc., then set the dependency resolver // to be Autofac. builder.RegisterType<ApplicationDbContext>().As<DbContext>().InstancePerRequest(); builder.RegisterType<ApplicationSignInManager>() .As<SignInManager<ApplicationUser, string>>().InstancePerRequest(); builder.RegisterType<UserStore<ApplicationUser>>() .As<IUserStore<ApplicationUser>>().InstancePerRequest(); builder.Register<IAuthenticationManager>((c, p) => c.Resolve<IOwinContext>() .Authentication).InstancePerRequest(); var dataProtectionProvider = app.GetDataProtectionProvider(); builder.Register<UserManager<ApplicationUser>>((c, p) => BuildUserManager(c, p, dataProtectionProvider)); var container = builder.Build(); DependencyResolver.SetResolver(new AutofacDependencyResolver(container)); // OWIN MVC SETUP: // Register the Autofac middleware FIRST, then the Autofac MVC middleware. app.UseAutofacMiddleware(container); app.UseAutofacMvc(); } private UserManager<ApplicationUser> BuildUserManager( IComponentContext context, IEnumerable<Parameter> parameters, IDataProtectionProvider dataProtectionProvider) { var manager = new ApplicationUserManager(context.Resolve<IUserStore<ApplicationUser>>()); // Configure validation logic for usernames manager.UserValidator = new UserValidator<ApplicationUser>(manager) { AllowOnlyAlphanumericUserNames = false, RequireUniqueEmail = true }; // Configure validation logic for passwords manager.PasswordValidator = new PasswordValidator { RequiredLength = 6, RequireNonLetterOrDigit = true, RequireDigit = true, RequireLowercase = true, RequireUppercase = true, }; // Configure user lockout defaults manager.UserLockoutEnabledByDefault = true; manager.DefaultAccountLockoutTimeSpan = TimeSpan.FromMinutes(5); manager.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> { MessageFormat = "Your security code is {0}" }); manager.RegisterTwoFactorProvider("Email Code", new EmailTokenProvider<ApplicationUser> { Subject = "Security Code", BodyFormat = "Your security code is {0}" }); //manager.EmailService = new EmailService(); //manager.SmsService = new SmsService(); if (dataProtectionProvider != null) { manager.UserTokenProvider = new DataProtectorTokenProvider<ApplicationUser>( dataProtectionProvider.Create("ASP.NET Identity")); } return manager; } }
Add a call to the ConfigureContainer(IAppBuilder app) method ConfigureContainer(IAppBuilder app) to the Configuration(IAppBuilder app) method Configuration(IAppBuilder app)
public partial class Startup { public void Configuration(IAppBuilder app) { ConfigureAuth(app); ConfigureContainer(app); } }
After the above steps, you can remove the app.CreatePerOwinContext() methods from the ConfigureAuth() method
it is also necessary to modify the AccountControlle, ManageController :
- Remove constructor without parameters.
- Delete the following properties:
UserManager , SignInManager , AuthenticationManager
This is how AccountController may look like after making the necessary changes:
public class AccountController : Controller { private readonly IAuthenticationManager _authenticationManager; private readonly SignInManager<ApplicationUser, string> _signInManager; private readonly UserManager<ApplicationUser> _userManager; public AccountController( UserManager<ApplicationUser> userManager, SignInManager<ApplicationUser, string> signInManager, IAuthenticationManager authenticationManager) { _authenticationManager = authenticationManager; _userManager = userManager; _signInManager = signInManager; } //Прочие необходимые методы }
Information sources used: