Check if Active Directory password is different from cookie

后端 未结 1 1060
广开言路
广开言路 2020-12-20 10:10

I have an asp.net app which needs to log users into Active Directory using forms authentication (windows authentication isn\'t an option with the given requirements).

<
相关标签:
1条回答
  • 2020-12-20 10:44

    Your code should read

    if (Membership.ValidateUser(model.UserName, model.Password))
    {
      string userData = DateTime.Now.ToString();
    
      FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(1,
        username,
        DateTime.Now,
        DateTime.Now.AddMinutes(30),
        isPersistent,
        userData,
        FormsAuthentication.FormsCookiePath);
    
      // Encrypt the ticket.
      string encTicket = FormsAuthentication.Encrypt(ticket);
    
      // Create the cookie.
      Response.Cookies.Add(new HttpCookie(FormsAuthentication.FormsCookieName, encTicket));
    }
    

    Now, when authenticating the user

    HttpCookie authCookie = Request.Cookies[FormsAuthentication.FormsCookieName];
    FormsAuthenticationTicket ticket = FormsAuthentication.Decrypt(authCookie.value);
    if (DateTime.Parse(ticket.UserData) > Membership.GetUser().LastPasswordChangedDate)
    {
        FormsAuthentication.SignOut();
        FormsAuthentication.RedirectToLoginPage();
    }
    
    0 讨论(0)
提交回复
热议问题