Click here to Skip to main content
12,251,027 members (39,227 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: C# ASP.NET
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
http://localhost/WebApplication3/LoginPage.aspx
then the authentication success on the button click we redirecting to
Response.Redirect("~/MainPage.aspx");

without authentication if you use the below URL
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
<authentication mode="Forms">
      <forms loginUrl="Login.aspx" timeout="1" />
    </authentication>
2. and my Login file, we have many images and headers then my button click event
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.
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
<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 24-Jan-13 22:55pm
D-Kishore4.2K
Edited 28-Jan-13 16:07pm
v7
Comments
AshishChaudha 27-Jan-13 23:52pm
   
what you have tried??
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 6

Try this in your web config

<authentication mode="Forms">
     <forms loginUrl="Login.aspx" name=".ASPXFORMSAUTH"/>
   </authentication>
   <authorization>
     <deny users="?"/>
   </authorization>
  Permalink  
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 5

Before calling Response.Redirect in your login page add this:
FormsAuthentication.SetAuthCookie("username", true);
Try this in your MainPage.aspx:
if(!System.Web.HttpContext.Current.User.Identity.IsAuthenticated)
{
    Response.Redirect("Login.aspx");
}



--Amit
  Permalink  
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..
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 3

Create a Session Variable and set it to true when ever user is logged in on the Loginpage.aspx before redirecting to mainpage.aspx
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
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.
  Permalink  
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.
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

You must go for Forms authentication[^] for this.

hope it helps Smile | :)
  Permalink  
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.
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

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>
  Permalink  
v3
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 4

You need to check the session on pageload of MainPage.aspx
  Permalink  

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web02 | 2.8.160426.1 | Last Updated 29 Jan 2013
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100