Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

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?

share|improve this question
    
Have you enabled anonymous access? –  David Lozzi Nov 28 '11 at 17:48
    
Yes, the application has enable anonymous = true –  Emad Nov 28 '11 at 18:16
add comment

2 Answers 2

up vote 1 down vote accepted

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

share|improve this answer
    
I am trying to access this page "AFTER" logging in. and Yes I see the user name on the top right. –  Emad 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 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 Nov 28 '11 at 21:19
show 1 more comment

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?

share|improve this answer
    
I am trying to access this page "AFTER" logging in. and Yes I see the user name on the top right. –  Emad Nov 28 '11 at 18:45
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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