Extending ASP.NET Identity Roles: IdentityRole is not part of the model for the current context

后端 未结 6 1512
鱼传尺愫
鱼传尺愫 2020-12-15 08:37

I\'m trying to use the new ASP.NET Identity in my MVC5 application, specifically I\'m trying to integrate ASP.NET Identity into an existing database. I\'ve already read the

6条回答
  •  北海茫月
    2020-12-15 08:50

    I solved with a different way. First I splited in two different Projects and Contexts. My project that Handle the Identity has this context:

        public class ApplicationDbContext : IdentityDbContext, IDisposable
    {
        public ApplicationDbContext()
            : base("DefaultConnection", throwIfV1Schema: false)
        {
        }
    
        public static ApplicationDbContext Create()
        {
            return new ApplicationDbContext();
        }
    }
    

    This is my ApplicationUser:

    public class ApplicationUser : IdentityUser
        {
            //Put here the extra properties that Identity does not handle
            [Required]
            [MaxLength(150)]
            public string Nome { get; set; }
    
            public async Task GenerateUserIdentityAsync(UserManager manager)
            {
                // Note the authenticationType must match the one defined in CookieAuthenticationOptions.AuthenticationType
                var userIdentity = await manager.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie);
                // Add custom user claims here
                return userIdentity;
            }
        }
    

    And my ApplicationUserManager looks like this:

    public class ApplicationUserManager : UserManager
        {
            public ApplicationUserManager(IUserStore store)
                : base(store)
            {
                //Setting validator to user name
                UserValidator = new UserValidator(this)
                {
                    AllowOnlyAlphanumericUserNames = false,
                    RequireUniqueEmail = true
                };
    
                //Validation Logic and Password complexity 
                PasswordValidator = new PasswordValidator
                {
                    RequiredLength = 6,
                    RequireNonLetterOrDigit = false,
                    RequireDigit = false,
                    RequireLowercase = false,
                    RequireUppercase = false,
                };
    
                //Lockout
                UserLockoutEnabledByDefault = true;
                DefaultAccountLockoutTimeSpan = TimeSpan.FromMinutes(5);
                MaxFailedAccessAttemptsBeforeLockout = 5;
    
                // Providers de Two Factor Autentication
                RegisterTwoFactorProvider("Código via SMS", new PhoneNumberTokenProvider
                {
                    MessageFormat = "Seu código de segurança é: {0}"
                });
    
                RegisterTwoFactorProvider("Código via E-mail", new EmailTokenProvider
                {
                    Subject = "Código de Segurança",
                    BodyFormat = "Seu código de segurança é: {0}"
                });
    
                //Email service
                EmailService = new EmailService();
    
                // Definindo a classe de serviço de SMS
                SmsService = new SmsService();
    
                var provider = new DpapiDataProtectionProvider("Braian");
                var dataProtector = provider.Create("ASP.NET Identity");
    
                UserTokenProvider = new DataProtectorTokenProvider(dataProtector);
    
            }
        }
    

    I hope that this helps someone. This solution was from this article: Eduardo Pires - But it is in Portuguese

提交回复
热议问题