How to check if user is logged in or not in forms based authentication

21,837
    if(HttpContext.Current.User.Identity.IsAuthenticated)
    {

    //Redirect to Default page
    Response.Redirect("default.aspx");
    }
Share:
21,837
Raghuveer
Author by

Raghuveer

merge delete

Updated on July 31, 2020

Comments

  • Raghuveer
    Raghuveer almost 4 years

    I want to implement forms based authentication manually in my website.

    I am using Web.config file for data store

    <authentication mode="Forms">
      <forms loginUrl="~/Login.aspx" 
             name=".ASPXAUTH"
             path="/"
             requireSSL="false"
             slidingExpiration="true"
             defaultUrl="~/Admin/OrderHistory.aspx"
             cookieless="UseDeviceProfile"
             enableCrossAppRedirects="false"
             >
        <credentials passwordFormat="Clear">
          <user name="Admin" password="adm123$"/>
          <user name="Administrator" password="adm234%"/>
        </credentials>
      </forms>
    </authentication>
    <authorization>
      <deny users ="?" />
      <allow users = "*" />
    </authorization>
    

    There is a Login.aspx page at root level in that im using ASP.NET login control to get username and password.

    Everything works fine but when the user is logged in and manually go to login.aspx page, its not redirect the user to defaultUrl page.

    I want to redirect the user to a specific page/defaultUrl page, if he is logged in and came manually to login.aspx page

    How to do it?

    Login Button-Click

    if (FormsAuthentication.Authenticate(LoginUser.UserName, LoginUser.Password))
        {
            FormsAuthentication.RedirectFromLoginPage(LoginUser.UserName, true);
    
        }
    
  • user1509
    user1509 almost 12 years
    But, then how can one stay on that page only and not redirect to the Login.aspx page?
  • Raghuveer
    Raghuveer almost 12 years
    Hi after adding the above code browser showing following message with error This webpage has a redirect loop
  • Raab
    Raab almost 12 years
    Use it on Login.aspx. not default.aspx
  • Raghuveer
    Raghuveer almost 12 years
    i wrote that in login.aspx page load only
  • Raghuveer
    Raghuveer almost 12 years
    may be we have to remove the ! in if statement
  • L_J
    L_J almost 6 years
    While this code may answer the question, providing information on how and why it solves the problem improves its long-term value.