[否IUserTokenProvider注册"使用structuremap依赖注入时

编程入门 行业动态 更新时间:2024-10-27 16:32:37
本文介绍了[否IUserTokenProvider注册"使用structuremap依赖注入时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一个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:没有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依赖注入时

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

发布评论

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

>www.elefans.com

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