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");
}
Comments
-
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 tologin.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 almost 12 yearsBut, then how can one stay on that page only and not redirect to the Login.aspx page?
-
Raghuveer almost 12 yearsHi after adding the above code browser showing following message with error
This webpage has a redirect loop
-
Raab almost 12 yearsUse it on Login.aspx. not default.aspx
-
Raghuveer almost 12 yearsi wrote that in login.aspx page load only
-
Raghuveer almost 12 yearsmay be we have to remove the
!
in if statement -
L_J almost 6 yearsWhile this code may answer the question, providing information on how and why it solves the problem improves its long-term value.