Kirk Liemohn is a Principal Software Engineer at ThreeWill. He has over 20 years of software development experience with most of that time spent in software consulting.
It’s a simple problem with a simple solution, but sometimes the little things hit you when you are deploying from one environment to another and they can take a lot longer than you’d like. So, hopefully, this is a reminder and saves some people some time…
Recently I wrote a couple of web services to be hosted within SharePoint. This problem wouldn’t only occur with web services, though. It could happen with web parts, application pages, site pages, event handlers, etc.
One of my web services did not access the SharePoint object model, but needed to know who the current user was, so it used the following within its code:
Another web service did use the SharePoint object model and it used the following:
The code above worked beautifully in my development environment and two other test environments. When it came time to deploy to test environment at the client, they didn’t work. I was perplexed.
Based on the exception I had, I ascertained that CurrentUser was null in the code above and asked the testers if they were logged in – assuming anonymous access must have been enabled and that they were not logged in. Well, they were logged in, but anonymous access was enabled – and that was the difference in the environments that worked and didn’t work.
It turns out that the problem was that the SharePoint web application (and therefore IIS) allowed anonymous access and the client to the web service calls (in this case InfoPath Forms Services) had negotiated to send as little credentials as possible (none).
So, the first two code snippets resulted in a null or empty string and the last code snippet blew up because CurrentUser was null.
The solution was simple… Require an IIS change to the asmx file or its containing folder to not allow (uncheck) anonymous access to the web service(s).
Another option might be to modify the code to return a 401 Unauthorized to see if the negotiation would begin. I didn’t take it that far, but would love to hear from someone if they have tried this.