I'm getting the following error when attempting to access a SharePoint 2010 site via the SharePoint API from within an ASP.NET MVC Application:
Access is denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED))
The MVC Application is hosted on IIS7 in 64-bit mode with an Application Pool User who is a Site Administrator on the SharePoint installation. Importantly, a WCF Service calling the SharePoint API hosted within the very same web application works without any issues, so I find it strange that I can't get the same results from within a Controller Action.
I have tried:
- Setting
System.web/identity impersonate="true" etc etc
- Setting
System.web/authentication mode="Windows"
- A combination of the above.
Regardless of what is tried above, System.Security.Principal.WindowsIdentity.GetCurrent().Name
returns the expected Application Pool User but Request.IsAuthenticated
returns false. This is verified both programmatically by examination through the debugger as well as in the Event Log:
Event code: 4011
Event message: An unhandled access exception has occurred.
...
Process information:
Process ID: 1234
Process name: w3wp.exe
Account name: VALIDDOMAIN\VALIDUSER
Request information:
Request URL: http://localhost:2000/Service/
Request path: /Service/
User host address: ::1
User:
Is authenticated: False
Authentication Type:
Thread account name: VALIDDOMAIN\VALIDUSER
How can I call the SharePoint 2010 API from a standard MVC3 Web Application Controller Action?
UPDATE 01:
Here's the code I'm using:
using (SPSite site = new SPSite(siteUrl))
{
workflow.SPSiteCollectionID = site.ID;
using (SPWeb web = site.OpenWeb())
{
foreach (SPGroup group in web.SiteGroups)
{
//some code
}
}
}
This fails on the line foreach (SPGroup group in web.SiteGroups)