GetUser.ProviderUserKey gives me System.NullReferenceException

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Cirene

    GetUser.ProviderUserKey gives me System.NullReferenceException

    Using asp.net membership. My login.aspx page goes to the loginredirect.a spx
    page after the user logs in.

    For some reason "Membership.Get User.ProviderUs erKey.ToString" gives me this
    error:
    System.NullRefe renceException: Object reference not set to an instance of an
    object. at loginredirect.P age_Load(Object sender, EventArgs e)

    Any reason why? I should be logged in fine.

    Is there another way in which I should get the current users UserId?


  • Tanzim Saqib

    #2
    Re: GetUser.Provide rUserKey gives me System.NullRefe renceException

    Membership.GetU ser() is definitely null in your case. Meaning that the user
    has not actually been logged in. Can't say much since unsure about your
    particular scenario.

    Tanzim Saqib
    W: http://www.TanzimSaqib.com


    "Cirene" <cirene@nowhere .comwrote in message
    news:#kiVgEk3IH A.4800@TK2MSFTN GP02.phx.gbl...
    Using asp.net membership. My login.aspx page goes to the
    loginredirect.a spx page after the user logs in.
    >
    For some reason "Membership.Get User.ProviderUs erKey.ToString" gives me
    this error:
    System.NullRefe renceException: Object reference not set to an instance of
    an object. at loginredirect.P age_Load(Object sender, EventArgs e)
    >
    Any reason why? I should be logged in fine.
    >
    Is there another way in which I should get the current users UserId?
    >

    Comment

    • Eliyahu Goldin

      #3
      Re: GetUser.Provide rUserKey gives me System.NullRefe renceException

      The authentication principal won't get set until the next request to the
      web-server. That's why GetUser returns. But you can use the "UserName"
      property of the Login control
      within the LoggedIn event to identify the user. With this name, you can use
      Membership.GetU ser(userName).


      --
      Eliyahu Goldin,
      Software Developer
      Microsoft MVP [ASP.NET]




      "Cirene" <cirene@nowhere .comwrote in message
      news:%23kiVgEk3 IHA.4800@TK2MSF TNGP02.phx.gbl. ..
      Using asp.net membership. My login.aspx page goes to the
      loginredirect.a spx page after the user logs in.
      >
      For some reason "Membership.Get User.ProviderUs erKey.ToString" gives me
      this error:
      System.NullRefe renceException: Object reference not set to an instance of
      an object. at loginredirect.P age_Load(Object sender, EventArgs e)
      >
      Any reason why? I should be logged in fine.
      >
      Is there another way in which I should get the current users UserId?
      >

      Comment

      • Marc

        #4
        Re: GetUser.Provide rUserKey gives me System.NullRefe renceException

        "Cirene" <cirene@nowhere .comwrote in message
        news:%23kiVgEk3 IHA.4800@TK2MSF TNGP02.phx.gbl. ..
        Using asp.net membership. My login.aspx page goes to the
        loginredirect.a spx page after the user logs in.
        >
        For some reason "Membership.Get User.ProviderUs erKey.ToString" gives me
        this error:
        System.NullRefe renceException: Object reference not set to an instance of
        an object. at loginredirect.P age_Load(Object sender, EventArgs e)
        >
        Any reason why? I should be logged in fine.
        >
        Is there another way in which I should get the current users UserId?
        I recommend you surround the code with some checks first, like this:

        If User.Identity.I sAuthenticated Then Begin
        myString := Membership.GetU ser().ProviderU serKey.ToString
        End Else Begin
        Response.Write( 'User is not logged in.');
        Response.End;
        End;

        {yes, that's Delphi ;-) }

        Step through that code in the debugger to find out if it goes down the
        "IsAuthenticate d " path. If not then you know what the problem is.

        Marc


        Comment

        • Tanzim Saqib

          #5
          Re: GetUser.Provide rUserKey gives me System.NullRefe renceException

          I'd prefer the following to make a safer access to ProviderUserKey property:

          var user = Membership.GetU ser();

          if(user == null)
          {
          // not authenticated, please log in
          }
          else
          {
          // play with the currently logged in user.
          }

          - Tanzim Saqib



          "Marc " <RmEaMrOcVE@ima rc.co.ukwrote in message
          news:ORMXeP53IH A.4332@TK2MSFTN GP06.phx.gbl...
          "Cirene" <cirene@nowhere .comwrote in message
          news:%23kiVgEk3 IHA.4800@TK2MSF TNGP02.phx.gbl. ..
          >Using asp.net membership. My login.aspx page goes to the
          >loginredirect. aspx page after the user logs in.
          >>
          >For some reason "Membership.Get User.ProviderUs erKey.ToString" gives me
          >this error:
          >System.NullRef erenceException : Object reference not set to an instance of
          >an object. at loginredirect.P age_Load(Object sender, EventArgs e)
          >>
          >Any reason why? I should be logged in fine.
          >>
          >Is there another way in which I should get the current users UserId?
          >
          I recommend you surround the code with some checks first, like this:
          >
          If User.Identity.I sAuthenticated Then Begin
          myString := Membership.GetU ser().ProviderU serKey.ToString
          End Else Begin
          Response.Write( 'User is not logged in.');
          Response.End;
          End;
          >
          {yes, that's Delphi ;-) }
          >
          Step through that code in the debugger to find out if it goes down the
          "IsAuthenticate d " path. If not then you know what the problem is.
          >
          Marc
          >

          Comment

          Working...