Click here to Skip to main content
15,881,866 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I am working on real estate website.There are pages-Post Property, post your requirement. i wanna if an unregistered user click on Post Property or Post your Requirement page then redirect to login page otherwise go to clicked page.


Please give me a suggestion how to do?
Posted

C#
if(session["userid"]=="")
{
  response.redirect("Loginpage.aspx");
}


Thanks
 
Share this answer
 
Comments
SURBHI TYAGI 22-Oct-12 7:19am    
i have done it.actually i also want to display a user name after login so i have create a master page that have a label to show login person name.I have also apply this master page on Post Property and Post your Requirement page.But these is an error occur because of null reference exception.On page load it move directly to master page.Please suggest..
AshishChaudha 22-Oct-12 7:28am    
I didnt understand your requirement..Post Property and Post your Requirement page..what is the meaning of this
psychic6000 22-Oct-12 10:06am    
always check if the sessional value exist or not, see the solution i posted,
Manas Bhardwaj 24-Oct-12 4:57am    
yup! +5
Here are two links to get the full understadning of these oncepts related to it:

Understanding ASP.NET Roles and Membership - A Beginner's Tutorial[^]
Understanding and Implementing ASP.NET Custom Forms Authentication[^]

If you need a quick way of doing it then here is a project doing the same stuff. you can find some quick-to-use code here:
YaBlogEngine - A Tiny Blog Engine written in ASP.NET/C#[^]

code like:

C#
protected void Page_Load(object sender, EventArgs e)
      {
          if (Session["UserId"] == null)
          {
              Response.Redirect("Login.aspx");
          }
      }
 
Share this answer
 
Comments
SURBHI TYAGI 22-Oct-12 7:46am    
Why is it giving the error of Error-"It is an error to use a section registered as allowDefinition='MachineToApplication' beyond application level. This error can be caused by a virtual directory not being configured as an application in IIS."
I m working on .Net framework 4.0
Manas Bhardwaj 24-Oct-12 4:56am    
Good +5!
C#
if (!session["UserID"]==null && !session["UserName"]==null) 
{
   labelID.text= (string) Session["UserID"];
   labelUserName.text= (string) Session["UserName"];
}
else
{
   Response.Redirect("~/login.aspx");
}


using this, you are checking if user id and the user name are present in session if so then displaying the info on labels, if any of them doesnt exist, redirecting them to login.aspx.


in your log out code, dont rewrite the value of the sessional variables, instead drop them, use
C#
Session.Clear();

to clear all the sessional values.
 
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