If you've worked with Identity 2.0, you've seen this piece of code:
public static AppIdentityUserManager Create(
IdentityFactoryOptions<AppIdentityUserManager> options,
IOwinContext context)
{
[snip]
var dataProtectionProvider = options.DataProtectionProvider;
if (dataProtectionProvider != null)
{
manager.UserTokenProvider =
new DataProtectorTokenProvider<AppIdentityUser>(
dataProtectionProvider.Create("ASP.NET Identity"));
}
return manager;
}
I understand that. In my application options.DataProtectionProvider (obviously passed in as a parameter) is null. How and where is that set (or not as the case may be?) Every place I've looked has that exact snippet of code, but no explanation for setting the DataProtectionProvider.
EDIT: I read DataProtectionProvider in the Identity sample project, that explains what the UserTokenProvider is but does not explain how it is set in the IdentityFactoryOptions object.
It's set when the user manager is created.
If you're using the CreatePerOwinContext
method inside your OWIN Startup
class, which is an extension defined in Microsoft.AspNet.Identity.Owin
, that extension creates a new IdentityFactoryOption
object and passes it to the Func
that is the parameter of CreatePerOwinContext
.
You can see the details of CreatePerOwinContext
in the source code here.
public static IAppBuilder CreatePerOwinContext<T>(this IAppBuilder app,
Func<IdentityFactoryOptions<T>, IOwinContext, T> createCallback,
Action<IdentityFactoryOptions<T>, T> disposeCallback) where T : class, IDisposable
{
if (app == null)
{
throw new ArgumentNullException("app");
}
if (createCallback == null)
{
throw new ArgumentNullException("createCallback");
}
if (disposeCallback == null)
{
throw new ArgumentNullException("disposeCallback");
}
app.Use(typeof (IdentityFactoryMiddleware<T, IdentityFactoryOptions<T>>),
new IdentityFactoryOptions<T>
{
DataProtectionProvider = app.GetDataProtectionProvider(),
Provider = new IdentityFactoryProvider<T>
{
OnCreate = createCallback,
OnDispose = disposeCallback
}
});
return app;
}
Note that if you have your own DI mechanism in your app, you don't need to use the CreatePerOwinContext
approach and wire up creation of all the objects by yourself. That way you'll not even need any IdentityFactoryOptions
. You can just inject IUserStore
, DbContext
, IDataProtectionProvider
, and anything else you need through any kind of DI you prefer.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With