check login with user control

Discussion in 'ASP .Net Web Controls' started by Tim T, Nov 6, 2003.

  1. Tim T

    Tim T Guest

    Hi,
    I have a web application that has a number of users who administer their own
    data... i have several pages that check to see if a valid userID session
    exists, and if not, send them to a Login.aspx page (where a userID session
    is generated upon successful login). my pages are user specific, so each
    page will load data specific to that user (hence the need to maintain a
    userID).

    if users attempt to hit a page that requires a userID, and the
    Session["supplierID"] returns null then they will be sent to a Login page.
    I have made a user control CheckLogin.axcs that checks for this session var
    and redirects to the login page if it doesn't exist
    here is the code for the Page_Load event in the user control:

    private void Page_Load(object sender, System.EventArgs e)

    {

    string thisParent = this.Parent.Page.ToString();

    thisParent = thisParent.Substring(4,thisParent.Length - 9);

    if(Session["supplierID"] == null)

    {

    HttpCookie cookie = Request.Cookies["supplierID"];

    if (cookie != null)

    {

    Session["supplierID"] = cookie.Value.ToString();

    }

    else

    {

    Server.Transfer("Login.aspx?callingPage="+thisParent);

    }

    }

    }

    this only works fine on parent pages that don't actually look for the
    Session["supplierID"]

    when i host this in a page on a page that looks for Session["supplierID"]
    in the Page_Load then i get this error:

    Object reference not set to an instance of an object.
    on this line:
    spGetSE.Parameters["@supplierID"].Value = Session["supplierID"].ToString();

    obviously this is because the session object doesn't exist, but i shouldn't
    have even go here, i would have thought that the user control would have
    sent me to the Login.aspx page before getting here.

    How do people do this properly? it must be common to want to check and
    redirect on pages using user controls?

    if someone could advise me how to get the user controls Page_Load event to
    fire before the parent pages checks for the invalid session it would be
    great

    Thanks for your help

    Tim
    Tim T, Nov 6, 2003
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. William F. Robertson, Jr.
    Replies:
    0
    Views:
    430
    William F. Robertson, Jr.
    Jul 2, 2003
  2. Tim T

    check login with user control

    Tim T, Nov 6, 2003, in forum: ASP .Net
    Replies:
    0
    Views:
    386
    Tim T
    Nov 6, 2003
  3. sumi
    Replies:
    7
    Views:
    284
    Jorgen Grahn
    Nov 9, 2005
  4. pj
    Replies:
    2
    Views:
    1,922
  5. Tony Johansson
    Replies:
    3
    Views:
    16,111
    Patrice
    Jan 2, 2010
Loading...

Share This Page