473,394 Members | 1,956 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

GetUser.ProviderUserKey gives me System.NullReferenceException

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?
Jul 5 '08 #1
4 5988
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" <ci****@nowhere.comwrote in message
news:#k**************@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?
Jul 6 '08 #2
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" <ci****@nowhere.comwrote in message
news:%2****************@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?

Jul 6 '08 #3
"Cirene" <ci****@nowhere.comwrote in message
news:%2****************@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
Jul 6 '08 #4
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 " <Rm********@imarc.co.ukwrote in message
news:OR**************@TK2MSFTNGP06.phx.gbl...
"Cirene" <ci****@nowhere.comwrote in message
news:%2****************@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
Jul 7 '08 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Terrence | last post by:
I am doing some of the C# walkthroughs to transition from VB to C#. When I try to execute static void Main() { Aplication.Run(new Form1()) } I raise a 'System.NullReferenceException" in...
1
by: Rafael | last post by:
Hi, I hope I can find some help for this problem IDE: Visual Studio.NET 2003 Developer Editio Language: C# Problem: "An unhandled exception of type 'System.NullReferenceException' occurred in...
2
by: Raed Sawalha | last post by:
i have a windows form(Main) with listview, when click an item in listview i open other window form (Sub) which generate the selected item from parent window in as treeview items when click any item...
1
by: msnews.microsoft.com | last post by:
I'm trying to fill an array of objects but when I add the first object I get a NullReferenceException. ----------------------------------------------------------------------------...
5
by: Davie | last post by:
I am implementing a Custom Provider for ASP.NET 2.0 membership. When I come to write the public override MembershipUser CreateUser(string username, string password, string email, string...
2
by: sxiao | last post by:
Hi, there I got a NullReferenceException when there are more than one users trying to open the same page at the same time. The senerio is: Two users logged into the web application using the...
6
by: William Mild | last post by:
I must be getting brain fried. I can't see the error. Create a new web form with the following code begind: Public Class test Inherits System.Web.UI.Page Public Class ReportCardData ...
2
by: Jav | last post by:
I need to get at the ProviderUserKey of the logged in user. I would have thought that I could do something like: Dim u As MembershipUser = Membership.GetUser Dim ProvKey as Guid =...
3
by: Cirene | last post by:
I'm creating my own create user wizard. Here's my code... If Membership.GetUser(txtEzUsername.Text).ProviderUserKey.ToString <"" Then Me.lblStatus.Text = "Username is in use. Please select...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.