This project is read-only.

How to get a IOwinContext object and IdentityFactoryOptions object when not using IAppBuilder startup?

May 7, 2014 at 10:32 PM
Edited May 7, 2014 at 10:32 PM

I have a user administration program that does not use identity framework but I would like to interact with it using the UserManager class I inherit from.

If I use my UserManager class from the identity framework and attempt to call "GeneratePasswordResetTokenAsync" method I get the following error:

No IUserTokenProvider is registered.

Which makes sense, since this only gets setup during the following method:

public static ApplicationUserManager Create(IdentityFactoryOptions<ApplicationUserManager> options, IOwinContext context)
        var manager = new ApplicationUserManager(new ARRTUserStore(new DB2(), context));
        manager.SetContextDefaults(options, context);
        return manager;

    public void SetContextDefaults(IdentityFactoryOptions<ApplicationUserManager> options, IOwinContext context)
        this.UserValidator = new UserValidator<IdentityUser, long>(this)
            AllowOnlyAlphanumericUserNames = false,
            RequireUniqueEmail = true
        this.PasswordValidator = new PasswordValidator()
            RequiredLength = 8,
            RequireNonLetterOrDigit = false,
            RequireDigit = true,
            RequireLowercase = true,
            RequireUppercase = true
        this.DefaultAccountLockoutTimeSpan = new TimeSpan(0, 15, 0); //Default lockout time of 15 minutes
        this.EmailService = new EmailService(); 
        this.MaxFailedAccessAttemptsBeforeLockout = 5;
        this.UserLockoutEnabledByDefault = true;

        var dataProtectionProvider = options.DataProtectionProvider;
        if (dataProtectionProvider != null)
            DataProtectorTokenProvider<IdentityUser, long> provider = new DataProtectorTokenProvider<IdentityUser, long>(dataProtectionProvider.Create("PR"));
            //provider.TokenLifespan (if we want to change default token life span)
            provider.TokenLifespan = new TimeSpan(0, 5, 0); //Change from default of 90 seconds to 5 minutes.
            this.UserTokenProvider = provider;

So how do we set some of these properties on the UserManager object without a valid OwinContext and more importantly some IdentityFactoryOptions that already have the DataProtectionProvider defined so you can get a UserTokenProvider?

May 7, 2014 at 10:41 PM
Figured it out:

var provider = new Microsoft.Owin.Security.DataProtection.DpapiDataProtectionProvider("ApplicationNameHere");
this.UserTokenProvider = new Microsoft.AspNet.Identity.Owin.DataProtectorTokenProvider<IdentityUser, long>(provider.Create("PR"));
Dec 3, 2014 at 8:45 PM
As a follow up to this, see this post for a better explanation. The solution I previous posted doesn't actually work because Microsoft actually uses a MachineKeyDataProtectionProvider class which they have marked internal (can't use in your own code), thus you must roll your own for things to actually work properly.

See here:
Marked as answer by MattOl on 12/3/2014 at 12:45 PM