“没有注册 IUserTokenProvider"使用结构图依赖注入时

编程入门 行业动态 更新时间:2024-10-27 08:27:35
本文介绍了“没有注册 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"使用结构图依赖注入时

本文发布于:2023-11-16 02:08:06,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1600034.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:结构图   没有注册   IUserTokenProvider   quot

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!