Click here to Skip to main content
15,887,240 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
Hi experts,

In my Web application, I am having two pages.

1.LoginPage.aspx
2.MainPage.aspx

controls used in Loginpage
username text box
Password text box
Button

while running
C#
http://localhost/WebApplication3/LoginPage.aspx

then the authentication success on the button click we redirecting to
C#
Response.Redirect("~/MainPage.aspx");


without authentication if you use the below URL
C#
http://localhost/WebApplication3/MainPage.aspx

its going to mainpage.aspx

my problem is if use this URL should be redirected to LoginPage only.

I have searched many examples in Google.But I haven't get the clear solution.
kindly explain me to achieve this.

I have master page and I tried like,

1. in my web.config file
JavaScript
<authentication mode="Forms">
      <forms loginUrl="Login.aspx" timeout="1" />
    </authentication>
                        <authorization>
				<deny users="?" />
			</authorization>

2. and my Login file, we have many images and headers then my button click event
C#
protected void Login_Click(object sender, EventArgs e)
       {
           if ((UserEmail.Value == "12") && (UserPass.Value == "12"))
           {
               FormsAuthentication.RedirectFromLoginPage(UserEmail.Value, true);
               Response.Redirect("MainPage.aspx");
           }
           else
           {
               Msg.Text = "Invalid Credentials: Please try again";
           }
       }

3. My WebForm1 file like one logout button click event.
C#
FormsAuthentication.SignOut();
            Response.Redirect("Login.aspx");


If i run this application my design changed and images were not displayed on the browser.

if i use this part in web.config file
JavaScript
<authorization>
				<deny users="?" />
			</authorization>


design and images were not displayed properly but i am able to redirect login page when other URL typed in the browser, if i remove this tag design is ok but i am unable to redirect to login page when other URL typed in the browser.
Posted
Updated 28-Jan-13 16:07pm
v7
Comments
AshishChaudha 27-Jan-13 23:52pm    
what you have tried??

Try this in your web config

XML
<authentication mode="Forms">
     <forms loginUrl="Login.aspx" name=".ASPXFORMSAUTH"/>
   </authentication>
   <authorization>
     <deny users="?"/>
   </authorization>
 
Share this answer
 
Before calling Response.Redirect in your login page add this:
C#
FormsAuthentication.SetAuthCookie("username", true);

Try this in your MainPage.aspx:
C#
if(!System.Web.HttpContext.Current.User.Identity.IsAuthenticated)
{
    Response.Redirect("Login.aspx");
}




--Amit
 
Share this answer
 
Comments
D-Kishore 29-Jan-13 4:48am    
This is fine amit, But here also we need to recode in each page.
_Amy 30-Jan-13 1:23am    
Just you need to modify your login page with the above code. That is enough for the settings..
Create a Session Variable and set it to true when ever user is logged in on the Loginpage.aspx before redirecting to mainpage.aspx
C#
Session["LoggedIn"] = true

on the mainpage.aspx Check if the above session vairable is true and the redirect the user back to login page if it is false
C#
if (Session["LoggedIn"] == true)
{
//Continue
}
else
{
Response.Redirect("~/LoginPage.aspx");
}


Make sure to set the session variable back to false when user is logged off.
 
Share this answer
 
Comments
Uday P.Singh 28-Jan-13 4:57am    
not good u will need to write it on each and every page, and of-course this is not the feasible solution as well.
member60 28-Jan-13 22:57pm    
agreed.
You must go for Forms authentication[^] for this.

hope it helps :)
 
Share this answer
 
Comments
D-Kishore 27-Jan-13 22:09pm    
Yes uday i followed this,

when i use this authorization
deny users="?" /deny
/authorization
My design is not displayed.
Open your web.config file and add similar code to this:

<authentication mode="Forms">
<forms name=".COOKIENAME"
loginUrl="YourLoginPage.aspx"
defaultUrl="YourDefaultPage.aspx"
protection="All"
timeout="30"
path="/"> 
</forms>
</authentication>
 
Share this answer
 
v3
You need to check the session on pageload of MainPage.aspx
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900