带有ASP.NET标识的错误消息



我正在从ASP.NET MVC Identity 1更新到2,并且我正在使用MVC 5。当发送电子邮件以最终在Identity 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"));

我建议您像下面这样创建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;
        }
    }
}

然后将数据库上下文和用户管理器配置为每个请求使用一个实例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;
            }
        }

希望这能有所帮助。

相关内容

  • 没有找到相关文章