“No IUserTokenProvider is registered” when using structuremap dependency injection

前端 未结 1 1428
梦如初夏
梦如初夏 2021-01-12 21:01

I have an MVC 5 project that has been modified to use int as the primary key for identity as shown in this guide

I then enabled email confirmation as described in t

相关标签:
1条回答
  • 2021-01-12 21:34

    IUserTokenProvider by default is inserted by OWIN, but when you resolve UserManager from your DI container, component that provides IUserTokenProvider is not available and this component is not initialised.

    You'll have to assign token provider to a global static variable when it is available and then re-use it in UserManager constructor:

    public class AuthConfig
    {
        public static IDataProtectionProvider DataProtectionProvider { get; set; }
    
        public void Configuration(IAppBuilder app)
        {
            ConfigureAuth(app);
        }
    
        public void ConfigureAuth(IAppBuilder app)
        {
            DataProtectionProvider = app.GetDataProtectionProvider();
    
            // do other configuration 
        }
    }
    

    And in then re-assign it in constructor of UserManager constructor:

    public UserManager(/*your dependecies*/)
    {
        var dataProtectorProvider = AuthConfig.DataProtectionProvider;
        var dataProtector = dataProtectorProvider.Create("My Asp.Net Identity");
        this.UserTokenProvider = new DataProtectorTokenProvider<ApplicationUser, Guid>(dataProtector)
        {
            TokenLifespan = TimeSpan.FromHours(24),
        };
        // other stuff
    }
    
    0 讨论(0)
提交回复
热议问题