Solved

Why can't I use the Membership.GetUser.ProviderUserKey in the Login.LoggedIn event?

Posted on 2008-10-02
5
858 Views
Last Modified: 2012-05-05
Hello,

Can you tell me what is wrong with the code I attach? When I run it I get this: "Object reference not set to an instance of an object."

If I understand correctly, the membership object has not yet been created. But when is it created? I need to execute a piece of code every time an user logs in (at the beginning of each session). And I need the UserId for that code.

Is there another event I can use to execute my code after the user logs in but before the user sees any page in the application?

Thanks in advance.
Protected Sub Login_LoggedIn(ByVal sender As Object, ByVal e As System.EventArgs) Handles Login.LoggedIn
 
    Dim UserId As Guid = Membership.GetUser.ProviderUserKey
 
End Sub

Open in new window

0
Comment
Question by:richardhaeger
  • 4
5 Comments
 
LVL 22

Expert Comment

by:RedKelvin
ID: 22631095
Hi, try this


Dim guidString As String = Membership.GetUser.ProviderUserKey()
Dim UserId As Guid = New Guid(guidString)

Open in new window

0
 

Author Comment

by:richardhaeger
ID: 22637262
Hello RedKelvin,

Your suggestion didn't work. I keep getting the "Object reference not set to an instance of an object." message in line 1 of your code.

Only thing I can think of is that the membership object is not yet created when the logged in event occurs.

Thanks anyway.

Any more ideas?
0
 

Author Comment

by:richardhaeger
ID: 22637860
I found the reason why the code fails (I still need a solution).

This works:

Protected Sub Login_LoggedIn(ByVal sender As Object, ByVal e As System.EventArgs) Handles Login.LoggedIn

    Dim UserId As Guid = Membership.GetUser(Login.UserName).ProviderUserKey

Then I can process my code to retrieve the company Id of the user that is being logged in, but if I try this:

Profile.CompanyId = CompanyId

An errors occurs saying I cannot set the profile properties of the anonymous user.

So at this point the user is still recognized as anonymous!

What event fires right after logged in? I need to run my code as soon as the user is REALLY logged in, before he sees any other page.

Please help! Thanks in advance.
0
 

Author Comment

by:richardhaeger
ID: 22637944
0
 

Accepted Solution

by:
richardhaeger earned 0 total points
ID: 22638151
I found this solution at http://forums.asp.net/t/1096144.aspx:

Profile.Initialize(Login1.UserName, true);

0

Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction (All good things must come to an end (http://en.wikipedia.org/wiki/All_Good_Things...))The original MySQL API (http://php.net/manual/en/book.mysql.php) has gone away, deprecated by PHP in Version 5.5, and removed from PHP in all current…
These days, all we hear about hacktivists took down so and so websites and retrieved thousands of user’s data. One of the techniques to get unauthorized access to database is by performing SQL injection. This article is quite lengthy which gives bas…
Wufoo.com provides powerful tools for surveying targeted groups, and utilizing data from completed surveys to find trends, discover areas of demand or customer expectation, and make business decisions on products or services.
Use Wufoo, an online form creation tool, to make powerful forms. Learn how to choose which pages of your form are visible to your users based on their inputs. The page rules feature provides you with an opportunity to create if:then statements for y…

776 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question