使用结构图dependency injection时,“没有注册IUserTokenProvider”
我有一个MVC 5项目,该项目已被修改为使用int作为身份的主键,如本指南所示
然后我按照本指南中的说明启用了电子邮件确认
一切都按预期工作正常。 然后我安装了structuremap.mvc5用于dependency injection,并添加了修改后的DefaultRegistry.cs
public DefaultRegistry() { Scan( scan => { scan.TheCallingAssembly(); scan.WithDefaultConventions(); scan.AssemblyContainingType(typeof(MyProject.Data.MyDbContext)); scan.With(new ControllerConvention()); }); //For().Use(); For<IUserStore>().Use().LifecycleIs(); For().Use(() => HttpContext.Current.GetOwinContext().Authentication); }
该项目构建正常但在尝试在站点上注册新用户时,发送电子邮件确认现在抛出exceptionSystem.NotSupportedException:在调用UserManager.GenerateEmailConfirmationTokenAsync(userID)时没有注册IUserTokenProvider。
private async Task 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 here"); return callbackUrl; }
我是dependency injection的新手,非常确定我做错了什么。 我会感激你的想法和见解。
默认情况下,IUserTokenProvider由OWIN插入,但是当您从DI容器中解析UserManager
时,提供IUserTokenProvider
组件不可用,并且此组件未初始化。
您必须在可用时将令牌提供程序分配给全局静态变量,然后在UserManager
构造函数中重新使用它:
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构造函数的构造函数中重新赋值:
public UserManager(/*your dependecies*/) { var dataProtectorProvider = AuthConfig.DataProtectionProvider; var dataProtector = dataProtectorProvider.Create("My Asp.Net Identity"); this.UserTokenProvider = new DataProtectorTokenProvider(dataProtector) { TokenLifespan = TimeSpan.FromHours(24), }; // other stuff }
- 为ASP.NET身份配置Unity DI
- 更改ASP.NET标识以使用现有数据库
- Identity Owin是否需要LazyLoading?
- 如何为Identity Framework设置entity framework模型以对抗现有数据库?
- 你能扩展HttpContext.Current.User.Identity属性吗?
- 使ASP.NET Identity 2.0电子邮件确认令牌适用于WCF和MVC
- 需要将Asp.Net Identity(RTM)用户ID从nvarchar(128)更改为uniqueidentifier
- ASP.NET身份检查用户角色无效
- ASP.NET Identity 2是否支持匿名用户?