Re: GetUser.ProviderUserKey gives me System.NullReferenceException



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@xxxxxxxxxxx> wrote in message news:ORMXeP53IHA.4332@xxxxxxxxxxxxxxxxxxxxxxx
"Cirene" <cirene@xxxxxxxxxxx> wrote in message news:%23kiVgEk3IHA.4800@xxxxxxxxxxxxxxxxxxxxxxx
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

.



Relevant Pages

  • Unhandled Exception
    ... Object reference not set to an instance of an object. ... at StandardInclude.StoredProcedure.TemplateMain_Load(Object eventSender, EventArgs eventArgs) ... at System.Windows.Forms.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context) ...
    (microsoft.public.dotnet.languages.vb)
  • Re: DataGridItem.DataItem availability?
    ... Post the relevant fragment from the code and the .aspx file, ... said "Object reference not set to an instance of an object." ... code-behind mode. ... So I wonder if this is the reason? ...
    (microsoft.public.dotnet.framework.aspnet)
  • Re: GetUser.ProviderUserKey gives me System.NullReferenceException
    ... property of the Login control ... within the LoggedIn event to identify the user. ... For some reason "Membership.GetUser.ProviderUserKey.ToString" gives me ... Object reference not set to an instance of ...
    (microsoft.public.dotnet.framework.aspnet)
  • NullReferenceException in MessageLoop
    ... drop-down a combo box and select a value...how do I track this error ... Object reference not set to an instance of an object. ... Int32 pvLoopData) ... at System.Windows.Forms.ThreadContext.RunMessageLoop(Int32 reason, ...
    (microsoft.public.dotnet.framework.windowsforms)