2

I would like to get the current MembershipUser from my page.

Membership.GetUser();

returns null.

Cannot use

HttpContext.Current.User.Identity.Name

as it returns my profile name as per my configuration to get User Name also.

How do I get my current user?

Update: Membership provider is SqlMembershipProvider

naveen
  • 53,448
  • 46
  • 161
  • 251
  • 2
    Which membership provider are you using? How does your web.config authentication settings look like? How is the web app set up in IIS, security wise? – Chris Jun 13 '11 at 10:26
  • What does Environment.UserName; return for you does this return profile name? – Richard Forrest Jun 13 '11 at 10:26

3 Answers3

10
  1. Membership.GetUser() will only work for an authenticated user. Otherwise, it's going to return null. To verify you're dealing with an authenticated request call "User.Identity.IsAuthenticated" on the page. If you've got an authenticated request, but Membership.GetUser() is still returning null, then that means the username associated with the authenticated user can't be found in the Membership datasource. Verify the username of the authenticated user with "User.Identity.Name".

  2. If you're calling one of the Membership.GetUser() overloads which takes the username and it's returning null, then that user doesn't exist in the Membership datasource (or we've got a bug). One way to easily verify this is to try a Membership.CreateUser() with the same username. If this doesn't throw an error because of a duplicate user, then you know the user never existed in the first place.

  3. Membership.GetUser() should have never worked for an anonymous user. No support was built into Membership for handling this case.

GEOCHET
  • 21,119
  • 15
  • 74
  • 98
Saurabh
  • 5,661
  • 2
  • 26
  • 32
  • That makes sense. So `MembersipUser.GetUser()` returns null as my `Identity.Name` is my profileName right? – naveen Jun 13 '11 at 10:35
  • you can check by if(User.Identity.IsAuthenticated){} Identity.Name would be username – Saurabh Jun 13 '11 at 10:52
  • yep. i changed my forms authentication ticket to pass the user name and manipulated the asp:loginname to display profile name. thanks – naveen Jun 13 '11 at 10:55
0

Have you tried Page.User.Identity.Name

Mark Redman
  • 24,079
  • 20
  • 92
  • 147
0
Membership.GetUser();

Is perfectly valid, what you may have here is configuration issues, double-check your web.config and make sure everything is correct, particularly the Forms section.

Mantorok
  • 5,168
  • 2
  • 24
  • 32