none
user always null after identity migration

    Pertanyaan

  • I have been migrating from membership to AspNet Identity and would like old users to authenticate in the new system with their old passwords. I try to login users with this code,but user is still null.

      protected void SignIn(object sender, EventArgs e)
    {
        var userStore = new UserStore<User>(new ApplicationDbContext());
        var userManager = new UserManager<User>(userStore);
        var user = userManager.Find(UserName.Text, Password.Text);
        if (user != null)
        {
            var authenticationManager = HttpContext.Current.GetOwinContext().Authentication;
            var userIdentity = userManager.CreateIdentity(user, DefaultAuthenticationTypes.ApplicationCookie);
    
            authenticationManager.SignIn(new AuthenticationProperties() { IsPersistent = false }, userIdentity);
            Response.Redirect("~/Login.aspx");
        }
        else
        {
            StatusText.Text = "Invalid username or password.";
            LoginStatus.Visible = true;
        }
    }

    There are my other classes:

    public class User : IdentityUser
    {
        public User()
        {
            CreateDate = DateTime.Now;
            IsApproved = false;
            LastLoginDate = DateTime.Now;
            LastActivityDate = DateTime.Now;
            LastPasswordChangedDate = DateTime.Now;
            LastLockoutDate = DateTime.Parse("1/1/1754");
            FailedPasswordAnswerAttemptWindowStart = DateTime.Parse("1/1/1754");
            FailedPasswordAttemptWindowStart = DateTime.Parse("1/1/1754");
        }
        public System.DateTime LastActivityDate { get; set; }
        public string LoweredUserName { get; set; }
        public string PasswordQuestion { get; set; }
        public string PasswordAnswer { get; set; }
        public bool IsApproved { get; set; }
        public bool IsLockedOut { get; set; }
        public string Comment { get; set; }
    }

     public class UserManager : UserManager<User>
    {
        public UserManager()
            : base(new UserStore<User>(new ApplicationDbContext()))
        {
            this.PasswordHasher = new SQLPasswordHasher();
        }
    }

     public class ApplicationDbContext : IdentityDbContext<User>
    {
        public ApplicationDbContext()
            : base("ApplicationDbContext", throwIfV1Schema: false)
        {
            this.Database.Log = Logger;
        }
    
        private void Logger(string log)
        {
            Debug.WriteLine(log);
        }
    
        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            base.OnModelCreating(modelBuilder);
    
            var application = modelBuilder.Entity<Application>();
            application.HasKey(t => t.ApplicationId).ToTable("aspnet_Applications");
    
        }
    
        public virtual IDbSet<Application> Applications { get; set; }
    }

    Help, please!!!!!

    • Dipindahkan oleh CoolDadTx Jumat, 02 November 2018 13.47 ASP.NET related
    Kamis, 01 November 2018 18.45

Semua Balasan

  • ASP.NET Identity can be addressed at the ASP.NET forums, and EF can be discussed at the EF forums.

    http://forums.asp.net/

    https://social.msdn.microsoft.com/Forums/en-US/home?forum=adodotnetentityframework

    Kamis, 01 November 2018 19.48
  • Oh, sorry. Thanks DA924x
    Kamis, 01 November 2018 19.58