“没有注册 IUserTokenProvider"使用结构图依赖注入时 [英] "No IUserTokenProvider is registered" when using structuremap dependency injection

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

问题描述

我有一个 MVC 5 项目,该项目已被修改为使用 int 作为身份的主键,如下所示 指南

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 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:调用 UserManager.GenerateEmailConfirmationTokenAsync(userID) 时未注册 IUserTokenProvider.

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

我是依赖注入的新手,很确定我做错了什么.我会感谢您的想法和见解.

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

推荐答案

IUserTokenProvider 默认由 OWIN 插入,但是当您从 DI 容器解析 UserManager 时,组件提供 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"使用结构图依赖注入时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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