Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: ASP.NET ADO.NET
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 22-Oct-12 1:56am
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

if(session["userid"]=="")
{
  response.redirect("Loginpage.aspx");
}
 
Thanks
  Permalink  
Comments
SURBHI TYAGI at 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 at 22-Oct-12 7:28am
   
I didnt understand your requirement..Post Property and Post your Requirement page..what is the meaning of this
psychic6000 at 22-Oct-12 10:06am
   
always check if the sessional value exist or not, see the solution i posted,
Manas Bhardwaj at 24-Oct-12 4:57am
   
yup! +5
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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:
 
protected void Page_Load(object sender, EventArgs e)
      {
          if (Session["UserId"] == null)
          {
              Response.Redirect("Login.aspx");
          }
      }
  Permalink  
Comments
SURBHI TYAGI at 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 at 24-Oct-12 4:56am
   
Good +5!
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

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
Session.Clear();
to clear all the sessional values.
  Permalink  

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

  Print Answers RSS
0 OriginalGriff 290
1 Jochen Arndt 165
2 DamithSL 125
3 PIEBALDconsult 110
4 Garth J Lancaster 90
0 OriginalGriff 5,790
1 DamithSL 4,601
2 Maciej Los 4,012
3 Kornfeld Eliyahu Peter 3,480
4 Sergey Alexandrovich Kryukov 3,195


Advertise | Privacy | Mobile
Web03 | 2.8.141220.1 | Last Updated 22 Oct 2012
Copyright © CodeProject, 1999-2014
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