Re: GetUser.ProviderUserKey gives me System.NullReferenceException
- From: "Tanzim Saqib" <TanzimSaqib@xxxxxxxx>
- Date: Mon, 7 Jul 2008 07:21:45 +0600
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
- References:
- Prev by Date: Re: details view parameter
- Next by Date: Re: IIS Virtual Directory Woes
- Previous by thread: Re: GetUser.ProviderUserKey gives me System.NullReferenceException
- Next by thread: Question about the asp.net 2.0 web configuration tool outside of development
- Index(es):
Relevant Pages
|