I am using ASP.NET Identity 2.0.
In my business layer (a separate DLL), I am using a UserManager for simple tasks like CreateUser oder DeleteUser.
protected UserManager<User> UserManager
{
get
{
if (_userManager == null)
{
_userManager = new UserManager<User>(UserStore);
}
_userManager.UserValidator = new UserValidator<User>(_userManager)
{
AllowOnlyAlphanumericUserNames = false,
RequireUniqueEmail = true
};
return _userManager;
}
}
Everything is fine so far.
Now I want to generate email confirmation tokens as well:
public string GenerateEmailConfirmationToken(User user)
{
if (user == null)
return null;
return UserManager.GenerateEmailConfirmationToken(user.Id);
}
The above code fails during run-time because my UserManager is lacking a DataProtectionProvider.
My problem: I don't know how to add a DataProtectionProvider to my UserManager.
In an MVC web project, this is a simple task (code taken from IdentityConfig.cs):
public static ApplicationUserManager Create(IdentityFactoryOptions<ApplicationUserManager> options, IOwinContext context)
{
var manager = new ApplicationUserManager(new UserStore<User>(context.Get<ApplicationDbContext>()));
// Configure validation logic for usernames
manager.UserValidator = new UserValidator<User>(manager)
{
AllowOnlyAlphanumericUserNames = false,
RequireUniqueEmail = true
};
// Configure validation logic for passwords
manager.PasswordValidator = new PasswordValidator
{
RequiredLength = 6,
RequireNonLetterOrDigit = false,
RequireDigit = true,
RequireLowercase = true,
RequireUppercase = true,
};
var dataProtectionProvider = options.DataProtectionProvider;
if (dataProtectionProvider != null)
{
manager.UserTokenProvider =
new DataProtectorTokenProvider<User>(dataProtectionProvider.Create("semperplus"));
}
return manager;
}
But the UserManger in my business logic does not know the UserManager from my web project. I guess I could pass down my web project's UserManager to my business layer, but I have a couple other client project's too (which are not web based). So, I would rather have it the other way: Create a UserManager in the business layer and have all other projects use this one.
Does anybody know how to create a DataProtectionProvider in my business layer project?