1

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?

2
  • Have you enabled anonymous access? Nov 28, 2011 at 17:48
  • Yes, the application has enable anonymous = true Nov 28, 2011 at 18:16

2 Answers 2

1

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

6
  • I am trying to access this page "AFTER" logging in. and Yes I see the user name on the top right. Nov 28, 2011 at 18:46
  • 2
    Look at this post. I hope it will be helpful.
    – Alexander
    Nov 28, 2011 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? Nov 28, 2011 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, 2011 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? Nov 28, 2011 at 21:19
2

See if these help

http://social.technet.microsoft.com/Forums/ar/sharepoint2010programming/thread/e635f3a6-a7b4-4f9a-94e3-a0c90144e365

http://codebender.denniland.com/common-coding-issues-when-working-with-anonymous-sharepoint-sites/

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?

1
  • I am trying to access this page "AFTER" logging in. and Yes I see the user name on the top right. Nov 28, 2011 at 18:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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