I got an error when my GeneratePasswordResetTokenAsync()
method is call. I configured autofac with owin identity.
The error is :
No IUserTokenProvider is registered when using dependency injection
In my sample.web
project there is a AutofacConfig.cs
file where I register signinmanager
and usermanager
which I created in sample.repository
project.
AutofacConfig.cs
public class AutofacConfig
{
public static Autofac.IContainer RegisterDependencies()
{
var containerBuilder = new ContainerBuilder();
// REGISTER DEPENDENCIES
containerBuilder.RegisterType<SampleDataContext>()
.As<DbContext>()
.InstancePerDependency();
containerBuilder.RegisterType<UserStore<SampleUser>>()
.As<IUserStore<SampleUser>>()
.InstancePerRequest();
containerBuilder.RegisterType<ApplicationUserManager>()
.AsSelf()
.InstancePerRequest();
containerBuilder.RegisterType<ApplicationSignInManager>()
.AsSelf()
.InstancePerRequest();
containerBuilder.RegisterType<EmailService>();
containerBuilder.Register<IAuthenticationManager>(c => HttpContext.Current.GetOwinContext().Authentication)
.InstancePerRequest();
var container = containerBuilder.Build();
DependencyResolver.SetResolver(new AutofacDependencyResolver(container));
var resolver = new AutofacWebApiDependencyResolver(container);
GlobalConfiguration.Configuration.DependencyResolver = resolver;
return container;
}
}
ApplicationUserManager.cs
public class ApplicationUserManager : UserManager<SampleUser>
{
public ApplicationUserManager(IUserStore<SampleUser> store)
: base(store)
{
}
public static ApplicationUserManager Create(IdentityFactoryOptions<ApplicationUserManager> options, IOwinContext context)
{
var manager = new ApplicationUserManager(new UserStore<SampleUser>(context.Get<SampleDataContext>()));
// Configure validation logic for usernames
manager.UserValidator = new UserValidator<SampleUser>(manager)
{
AllowOnlyAlphanumericUserNames = false,
RequireUniqueEmail = true
};
// Configure validation logic for passwords
manager.PasswordValidator = new PasswordValidator
{
RequiredLength = 8,
RequireNonLetterOrDigit = false,
RequireDigit = false,
RequireLowercase = false,
RequireUppercase = false,
};
// 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<SampleUser>
{
MessageFormat = "Your security code is {0}"
});
manager.RegisterTwoFactorProvider("Email Code", new EmailTokenProvider<SampleUser>
{
Subject = "Security Code",
BodyFormat = "Your security code is {0}"
});
manager.EmailService = new EmailService();
manager.SmsService = new SmsService();
var provider = new DpapiDataProtectionProvider("Sample");
manager.UserTokenProvider = new DataProtectorTokenProvider<SampleUser>(
provider.Create("ASP.NET Identity"));
return manager;
}
}