实体类型ApplicationUser不是模型的一部分:使用Webforms

时间:2017-08-16 15:44:44

标签: c# webforms entity-framework-6 asp.net-identity-2

我已经在StackOverflow上回顾了几个这样的答案,我已经应用并比较了我的具体情况没有答案。以下是我的观点:

*****在Web.Config *****

<add name="DefaultConnection" connectionString="Data Source=LAPTOP-2UA8GL6L\SQLEXPRESS;Initial Catalog=FHSF_DEV;Integrated Security=True" providerName="System.Data.SqlClient" />
在IdentityModels.cs

    public class ApplicationUser : IdentityUser
{

    public DateTime BirthDate { get; set; }

    public string City { get; set; }

    public string State { get; set; }

    public int    UserDetailID { get; set; }

    public ClaimsIdentity GenerateUserIdentity(ApplicationUserManager manager)
    {
        // Note the authenticationType must match the one defined in CookieAuthenticationOptions.AuthenticationType
        var userIdentity = manager.CreateIdentity(this, DefaultAuthenticationTypes.ApplicationCookie);
        // Add custom user claims here
        return userIdentity;
    }

    public Task<ClaimsIdentity> GenerateUserIdentityAsync(ApplicationUserManager manager)
    {
        return Task.FromResult(GenerateUserIdentity(manager));
    }
}

public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
    public ApplicationDbContext()
        : base("DefaultConnection", throwIfV1Schema: false)
    {
    }

    public static ApplicationDbContext Create()
    {
        return new ApplicationDbContext();
    }
}

}

在IdentityConfig.cs中

public class EmailService : IIdentityMessageService
{
    public Task SendAsync(IdentityMessage message)
    {
        // Plug in your email service here to send an email.
        return Task.FromResult(0);
    }
}

public class SmsService : IIdentityMessageService
{
    public Task SendAsync(IdentityMessage message)
    {
        // Plug in your SMS service here to send a text message.
        return Task.FromResult(0);
    }
}

// Configure the application user manager used in this application. UserManager is defined in ASP.NET Identity and is used by the application.
public class ApplicationUserManager : UserManager<ApplicationUser>
{
    public ApplicationUserManager(IUserStore<ApplicationUser> store)
        : base(store)
    {
    }


        public class ApplicationUserManager : UserManager<ApplicationUser>
{
    public ApplicationUserManager(IUserStore<ApplicationUser> store)
        : base(store)
    {
    }


    public class ApplicationDbContext : IdentityDbContext
    {
        public ApplicationDbContext()
        : base("DefaultConnection")
        {

        }

        public DbSet<ApplicationUser> ApplicationUsers { get; set; }
    }


    //////// Commented out the rest of the code within this class //////

public static ApplicationUserManager Create(IdentityFactoryOptions选项,IOwinContext上下文)         {             var manager = new ApplicationUserManager(new UserStore(context.Get()));             //配置用户名的验证逻辑             manager.UserValidator = new UserValidator(manager)             {                 AllowOnlyAlphanumericUserNames = false,                 RequireUniqueEmail = true             };

        // Configure validation logic for passwords
        manager.PasswordValidator = new PasswordValidator
        {
            RequiredLength = 6,
            RequireNonLetterOrDigit = true,
            RequireDigit = true,
            RequireLowercase = true,
            RequireUppercase = true,
        };

        // 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 it in here.
        //manager.RegisterTwoFactorProvider("Phone Code", new PhoneNumberTokenProvider<ApplicationUser>
        //{
        //    MessageFormat = "Your security code is {0}"
        //});
        //manager.RegisterTwoFactorProvider("Email Code", new EmailTokenProvider<ApplicationUser>
        //{
        //    Subject = "Security Code",
        //    BodyFormat = "Your security code is {0}"
        //});

        // Configure user lockout defaults
        manager.UserLockoutEnabledByDefault = false;
        manager.DefaultAccountLockoutTimeSpan = TimeSpan.FromMinutes(5);
        manager.MaxFailedAccessAttemptsBeforeLockout = 5;

        manager.EmailService = new EmailService();
        manager.SmsService = new SmsService();
        var dataProtectionProvider = options.DataProtectionProvider;
        if (dataProtectionProvider != null)
        {
            manager.UserTokenProvider = new DataProtectorTokenProvider<ApplicationUser>(dataProtectionProvider.Create("ASP.NET Identity"));
        }
        return manager;
    }
}

并且,在发生错误的调用程序中

            if (IsValid)
        {
            // Validate the user's email address
            //var manager = Context.GetOwinContext().GetUserManager<ApplicationUserManager>();
            //ApplicationUser user = manager.FindByName(Email.Text);
            /////////////////////////////////////////////////////////////////
            //var userStore = new UserStore<IdentityUser>();
            //var userManager = new UserManager<IdentityUser>(userStore);
            //IdentityUser user = userManager.FindByEmail(Email.Text);

            var provider = new DpapiDataProtectionProvider("FHSFReset");
            ApplicationDbContext context = ApplicationDbContext.Create();
            var userManager = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>());

            ///////////////////////////////////////////////////////////
            // The error occurs below /////////////////////////////////
            ///////////////////////////////////////////////////////////

            ApplicationUser user = userManager.FindByEmail(Email.Text);
            ///////////////////////////////////////////////////////////
            // Error msg: System.InvalidOperationException: 'The entity 
            //  type ApplicationUser is not part of the model for the 
            //  current context.'
            ///////////////////////////////////////////////////////////


            if (user == null)
            {
                FailureText.Text = "The user either does not exist or is not confirmed.";
                ErrorMessage.Visible = true;
                return;
            }
            // For more information on how to enable account confirmation and password reset please visit https://go.microsoft.com/fwlink/?LinkID=320771
            // Send email with the code and the redirect to reset password page
            userManager.UserTokenProvider = new DataProtectorTokenProvider<ApplicationUser>(
                provider.Create("EmailRestPW"));

注意:我正在使用身份,实体框架(我认为)和OWIN。我试图在我的数据库中保留授权/身份验证表,表格就在那里。我(如上所示)在AspNetUsers中添加了自定义字段,应该反映在连接中。

有没有明显的迹象表明我已经错误地设置了这个,以便发生错误?

0 个答案:

没有答案