Search code examples
c#asp.netasp.net-membershipforms-authentication

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


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);

    }

Solution

  •     if(HttpContext.Current.User.Identity.IsAuthenticated)
        {
    
        //Redirect to Default page
        Response.Redirect("default.aspx");
        }