[否IUserTokenProvider注册"使用structuremap依赖注入时 [英] "No IUserTokenProvider is registered" when using structuremap dependency injection

查看:575
本文介绍了[否IUserTokenProvider注册"使用structuremap依赖注入时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个MVC 5项目已被修改为使用INT作为身份的主键如本<一href=\"http://www.asp.net/identity/overview/extensibility/change-primary-key-for-users-in-aspnet-identity\"相对=nofollow>指南

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

然后我启用电子邮件确认在<一个描述href=\"http://www.asp.net/mvc/overview/security/create-an-aspnet-mvc-5-web-app-with-email-confirmation-and-password-reset\"相对=nofollow>本指南

I then enabled email confirmation as described in this guide

一切运行良好预期。然后我安装了依赖注入structuremap.mvc5并补充修改DefaultRegistry.cs到

Everything worked fine as expected. Then I installed structuremap.mvc5 for dependency injection and added modified DefaultRegistry.cs to

public DefaultRegistry() {
        Scan(
            scan => {
                scan.TheCallingAssembly();
                scan.WithDefaultConventions();
                scan.AssemblyContainingType(typeof(MyProject.Data.MyDbContext));
                scan.With(new ControllerConvention());
            });
        //For<IExample>().Use<Example>();
        For<IUserStore<ApplicationUser, int>>().Use<MyUserStore>().LifecycleIs<HttpContextLifecycle>();
        For<IAuthenticationManager>().Use(() => HttpContext.Current.GetOwinContext().Authentication);

    }

该项目建立正常,但试图在网站上注册一个新用户时,发送电子邮件确认现在抛出一个异常System.NotSupportedException:没有IUserTokenProvider注册调用UserManager.GenerateEmailConfirmationTokenAsync(用户ID)时

The project builds fine but when trying to register a new user on the site, the send email confirmation now throws an exception System.NotSupportedException: No IUserTokenProvider is registered when calling UserManager.GenerateEmailConfirmationTokenAsync(userID).

private async Task<string> SendEmailConfirmationTokenAsync(int userID, string subject)
    {
        string code = await UserManager.GenerateEmailConfirmationTokenAsync(userID);
        var callbackUrl = Url.Action("ConfirmEmail", "Account",
           new { userId = userID, code = code }, protocol: Request.Url.Scheme);
        await UserManager.SendEmailAsync(userID, subject,
           "Please confirm your account by clicking <a href=\"" + callbackUrl + "\">here</a>");

        return callbackUrl;
    }

我是新来的依赖注入和pretty知道我做错了什么。我将AP preciate您的想法和见解。

I am new to dependency injection and pretty sure I am doing something wrong. I'll appreciate your thoughts and insights.

推荐答案

IUserTokenProvider 默认情况下是由OWIN插入,但是当你解决的UserManager 从DI容器,组件,提供了 IUserTokenProvider 不可用,并且该组件将不会被初始化。

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.

您将不得不令牌供应商分配到一个全局静态变量可用时,然后在的UserManager 构造重新使用它:

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 
    }
}

和中的UserManager构造函数构造函数,然后重新分配它:

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
}

这篇关于[否IUserTokenProvider注册&QUOT;使用structuremap依赖注入时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆