I have found that SPContext.Current.Web.CurrentUser is really unreliable. One out of ten requests that object comes back as null.

Is there an alternative to SPContext.Current.Web.CurrentUser?

Has anyone else seen this unreliability?

Is there a fix out there?

  • Can you share more details about your scenario? What are you trying to achieve? In my experience I have always got the correct value in Web.CurrentUser – Vardhaman Deshpande Jan 25 '13 at 20:04
  • I am running this code in an ASHX handler but am also seeing this issue occur in aspx pages as well. – iambriansreed Jan 25 '13 at 20:15
  • Are you getting value for SPContext.Current.Web? Can you post your code here please. – Vardhaman Deshpande Jan 25 '13 at 20:47
  • I have completely logged off and logged back in again and it would seem I am not having this issue any more. – iambriansreed Jan 25 '13 at 20:50
  • When I use SPContext with my SP-admin account, SharePoint is "friendly" and switch the admin account to SharePoint/System which doesn't have a user profile. Use HttpContext instead. – Benny Skogberg Jan 26 '13 at 15:49

It would be cool if you posted the offending code and its surroundings, but some things to consider out of the top of my head:

  1. You might be getting the object too early in the lifecycle (say PreInit methods and such, or an HttpModule etc)
  2. You might be breaking the pipeline doing things like setting the HttpContext to null (I've seen that in SharePoint workarounds before)
  3. You might be behind a reverse proxy like Apache or Nginx making your KeepAlives go crazy, try testing the code outside your environment
  4. And some "is the green light one" for hardware issues, app pool reaching the maxium RAM or lack of it, SQL Server busy
  5. You may be using some PInvoke impersonation that makes the integrated authentication go bananas
  6. If you are using FBA the provider settings and the backend availability (AD, XML file, SQL Membership) might be failing -- but this is unlikely for it would be a different error.
  7. A messed around HttpModules section loves to break things too, have you tried in a new WebApplication with zero changes?
  8. I Like the number eight, so I thought I should give it eight vague ideas

TL;DR: Post your code :)

  • I am not entirely sure if logging out and logging back in or 1. was what fixed it but, it's fixed. – iambriansreed Jan 27 '13 at 17:35

I have only experienced this when I forget that I'm calling it out of context.

are you sure you are not trying to call this within SPSecurity.RunWithElevatedPrivileges() ?

  • I am absolutely sure that I am not trying to call this within SPSecurity.RunWithElevatedPreviliges(). – iambriansreed Jan 25 '13 at 20:24
  • Even with runwithelevated, the current user is shown as SHAREPOINT/system (app pool account) but never null in my experience. – Vardhaman Deshpande Jan 25 '13 at 20:46

plz try with this..

SPWeb spWeb = Microsoft.SharePoint.WebControls.SPControl.GetContextWeb(System.Web.HttpContext.Current);
SPUser user = spWeb.CurrentUser;


using (SPSite spsite = new SPSite(url))
                    using (SPWeb spweb = spsite.OpenWeb())
                       userObj = spweb.CurrentUser;

plz also try with RunWithElevatedPreviliges()...

  • 2
    Your 1st and 3rd suggestions are identical... and your 2nd option won't return an SPPrinciple object at all (though should return something other than null). – James Love Jan 26 '13 at 16:14
  • 2
    Infact, your 1st and 3rd options are exactly what the original poster said isn't working. – James Love Jan 26 '13 at 16:16
  • 1
    You should 1. Delete your answer. or 2. Fix it. or 3. Delete your answer. – iambriansreed Jan 27 '13 at 17:49
  • Hello guys.... I updated my answer.. – Hitesh Chandegara Jan 28 '13 at 5:21

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.