我正在从ASP.NET MVC身份1更新到2并且我正在使用MVC 5.当发送电子邮件以完成在身份1中注册时,一切正常但是因为更新我收到此错误消息:
没有注册IUserTokenProvider。
从研究开始,我认为我需要将此代码放入我的应用程序中,但我不知道该把它放在哪里?
var provider = new Microsoft.Owin.Security.DataProtection.DpapiDataProtectionProvider("PaymentPortal");
UserManager<ApplicationUser> userManager = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>());
userManager.UserTokenProvider = new Microsoft.AspNet.Identity.Owin.DataProtectorTokenProvider<ApplicationUser>(provider.Create("EmailConfirmation"));
答案 0 :(得分:1)
我建议您像下面一样创建CustomUserManager并在启动时注册它,因为您可以在一个地方配置适当性配置。
public class CustomUserManager : UserManager<ApplicationUser>
{
public CustomUserManager(IUserStore<ApplicationUser> store) : base(store)
{
}
public static CustomUserManager Create(IdentityFactoryOptions<CustomUserManager> options, IOwinContext context)
{
var manager = new CustomUserManager(new UserStore<ApplicationUser>(context.Get<YourDataContenxt>()));
// Configure validation logic for usernames
manager.UserValidator = new UserValidator<ApplicationUser>(manager)
{
AllowOnlyAlphanumericUserNames = false,
RequireUniqueEmail = true
};
// Configure validation logic for passwords
manager.PasswordValidator = new PasswordValidator
{
RequiredLength = 6,
RequireNonLetterOrDigit = false,
RequireDigit = true,
RequireLowercase = true,
RequireUppercase = true,
};
// Configure user lockout defaults
manager.UserLockoutEnabledByDefault = false;
manager.DefaultAccountLockoutTimeSpan = TimeSpan.FromMinutes(5);
//manager.MaxFailedAccessAttemptsBeforeLockout = 5;
// Register two factor authentication providers. This application uses Phone and Emails as a step of receiving a code for verifying the user
// You can write your own provider and plug in here.
manager.RegisterTwoFactorProvider("PhoneCode", new PhoneNumberTokenProvider<ApplicationUser, string>
{
MessageFormat = "Your security code is: {0}"
});
// Two Factor Authentication
manager.RegisterTwoFactorProvider("EmailCode", new EmailTokenProvider<ApplicationUser, string>
{
Subject = "SecurityCode",
BodyFormat = "Your security code is {0}"
});
// Your Email service
//manager.EmailService = new IdentityEmailMessageService(new EmailService(new MailServer()));
//manager.SmsService = new SmsService();
// Data Protection Provider
var dataProtectionProvider = options.DataProtectionProvider;
if (dataProtectionProvider != null)
{
manager.UserTokenProvider = new DataProtectorTokenProvider<ApplicationUser>(dataProtectionProvider.Create("Asp.NEt Identity"));
}
return manager;
}
}
}
然后将db上下文和用户管理器配置为每个请求使用一个实例Startup.Auth文件。
public partial class Startup
{
public static OAuthAuthorizationServerOptions OAuthOptions { get; private set; }
public static string PublicClientId { get; private set; }
// For more information on configuring authentication, please visit http://go.microsoft.com/fwlink/?LinkId=301864
public void ConfigureAuth(IAppBuilder app)
{
// Configure the db context and user manager to use a single instance per request
app.CreatePerOwinContext(YourDataContext.Create);
app.CreatePerOwinContext<CustomUserManager>(CustomUserManager.Create);
// Other relevant configurations
}
}
在控制器中,您可以获得如下实例。
private CustomUserManager customUserManager;
public CustomUserManager CustomUserManager
{
get
{
return customUserManager?? HttpContext.GetOwinContext().GetUserManager<CustomUserManager >();
}
private set
{
customUserManager= value;
}
}
希望这有帮助。