0

I am developing website by using ASP.net and I want to implement formbased authentication. In web.config I use this

  <system.web>
    <authentication mode="Forms" >
      <forms loginUrl="login.aspx" protection="All" path="/" timeout="30" name=".ASPXFORMSDEMO">
      </forms>
    </authentication>
  </system.web>

In login button click I use this.

FormsAuthenticationTicket ticket;
                string cookieString;
                HttpCookie cookie;

                ticket = new FormsAuthenticationTicket(1, txtiLoginEmail.Value, DateTime.Now, DateTime.Now.AddMinutes(5), chbRememberMe.Checked, null);
                cookieString = FormsAuthentication.Encrypt(ticket);
                cookie = new HttpCookie(FormsAuthentication.FormsCookieName, cookieString);


                if (chbRememberMe.Checked)
                {
                    cookie.Expires = ticket.Expiration;
                    cookie.Path = FormsAuthentication.FormsCookiePath;
                }
                Response.Cookies.Add(cookie);

                string strRedirect = "post.aspx";
                Response.Redirect(strRedirect);

I have a page called post.aspx . I want this page to be acess only for authenticated users. So at the page load event of post.aspx page I use this code to check it

  bool isAuthenticated = HttpContext.Current.User != null && HttpContext.Current.User.Identity.IsAuthenticated;

But above code always return false. What I am doing wrong here?

Prageeth Liyanage
  • 1,612
  • 2
  • 19
  • 41

1 Answers1

0

I think you forgot to put the:

FormsAuthentication.SetAuthCookie(/*UserHere*/ ,false);
kevinrodriguez-io
  • 1,054
  • 9
  • 15