Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does IdentityFactoryOptions<AppIdentityUserManager> options get set?

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.

like image 629
Duston Avatar asked Oct 02 '15 21:10

Duston


1 Answers

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.

like image 50
Tom Pažourek Avatar answered Sep 28 '22 02:09

Tom Pažourek