I have a SP2010 site with FBA using Active directory membership provider. I am building an application page (under /_layouts) that only authenticated users can access. In this page's code behind, I am trying to get the current user login name. For some reason, I always have SPContext.Current.Web.CurrentUser as NULL. Though the authentication seems to be working correctly as the username is shown correctly on the page (on this menu that has "sign as different user" and "sign out").

Interestingly, HttpContext.Current.User shows the correct username.

Why is the SharePoint always returning NULL for CurrentUser? How can I get the current user from the SP context?

  • Have you enabled anonymous access? – David Lozzi Nov 28 '11 at 17:48
  • Yes, the application has enable anonymous = true – Emad Gabriel Nov 28 '11 at 18:16

You always get NULL for anonymous user. If you use anonymous access, you should always check first if the currentUser is not null.

  • I am trying to access this page "AFTER" logging in. and Yes I see the user name on the top right. – Emad Gabriel Nov 28 '11 at 18:46
  • 2
    Look at this post. I hope it will be helpful. – Alexander Nov 28 '11 at 18:52
  • So Basically, in order for SPContext.Current.Web.Current user to be not NULL, I need to EnsureUser after creating this user and adding it to the FBA Store? Is this accurate? – Emad Gabriel Nov 28 '11 at 20:38
  • I think so... EnsureUser method checks whether the specified logon name belongs to a valid user of the website, and if the logon name does not already exist, adds it to the website. As I understand EnsureUser looks for a valid user in the AD, FBA data base, or mix mode (it depends on you authentication settings). – Alexander Nov 28 '11 at 20:55
  • I see, So the newly created user is saved twice? once in the FBA store and "also" in the SP content DB? and if so, then I need to delete it from both places as well when deleting this user, right? – Emad Gabriel Nov 28 '11 at 21:19

See if these help



The issue is around the anonymous permissions, I'm guessing your site is thinking the user is anonymous. On the actual page you're working on, do you see the user name in the top right?

  • I am trying to access this page "AFTER" logging in. and Yes I see the user name on the top right. – Emad Gabriel Nov 28 '11 at 18:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.