From: Cirene on
Using asp.net membership. My login.aspx page goes to the loginredirect.aspx
page after the user logs in.

For some reason "Membership.GetUser.ProviderUserKey.ToString" gives me this
error:
System.NullReferenceException: Object reference not set to an instance of an
object. at loginredirect.Page_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?


From: Tanzim Saqib on
Membership.GetUser() 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(a)nowhere.com> wrote in message
news:#kiVgEk3IHA.4800(a)TK2MSFTNGP02.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.GetUser.ProviderUserKey.ToString" gives me
> this error:
> System.NullReferenceException: Object reference not set to an instance of
> an object. at loginredirect.Page_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?
>
From: Eliyahu Goldin on
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.GetUser(userName).


--
Eliyahu Goldin,
Software Developer
Microsoft MVP [ASP.NET]
http://msmvps.com/blogs/egoldin
http://usableasp.net


"Cirene" <cirene(a)nowhere.com> wrote in message
news:%23kiVgEk3IHA.4800(a)TK2MSFTNGP02.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.GetUser.ProviderUserKey.ToString" gives me
> this error:
> System.NullReferenceException: Object reference not set to an instance of
> an object. at loginredirect.Page_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?
>


From: Marc on
"Cirene" <cirene(a)nowhere.com> wrote in message
news:%23kiVgEk3IHA.4800(a)TK2MSFTNGP02.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.GetUser.ProviderUserKey.ToString" gives me
> this error:
> System.NullReferenceException: Object reference not set to an instance of
> an object. at loginredirect.Page_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.IsAuthenticated Then Begin
myString := Membership.GetUser().ProviderUserKey.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
"IsAuthenticated " path. If not then you know what the problem is.

Marc


From: Tanzim Saqib on
I'd prefer the following to make a safer access to ProviderUserKey property:

var user = Membership.GetUser();

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

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


"Marc " <RmEaMrOcVE(a)imarc.co.uk> wrote in message
news:ORMXeP53IHA.4332(a)TK2MSFTNGP06.phx.gbl...
> "Cirene" <cirene(a)nowhere.com> wrote in message
> news:%23kiVgEk3IHA.4800(a)TK2MSFTNGP02.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.GetUser.ProviderUserKey.ToString" gives me
>> this error:
>> System.NullReferenceException: Object reference not set to an instance of
>> an object. at loginredirect.Page_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.IsAuthenticated Then Begin
> myString := Membership.GetUser().ProviderUserKey.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
> "IsAuthenticated " path. If not then you know what the problem is.
>
> Marc
>