Solved

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

Posted on 2008-10-02
5
861 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Using Quotation Marks in PHP This question (http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/Q_28217211.html) seems to come up a lot for developers who are new to PHP.  And it got me thinking, "How can we explain the rule…
If I have to fix slow responding website my first thoughts are server side optimizations: the database may not be optimized or caching is not enabled, or things like that. We often overlook another major part of our web application: the client. We o…
This video teaches viewers how to create their own website using cPanel and Wordpress. Tutorial walks users through how to set up their own domain name from tools like Domain Registrar, Hosting Account, and Wordpress. More specifically, the order in…
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.

734 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