Solved

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

Posted on 2008-10-02
5
860 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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Foolproof security solutions has become one of the key necessities of every e-commerce or Internet banking website. If you too own an online shopping site then its vital for you to equip your web portal with customer security features that can allow…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
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…

820 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