I use some simple High Trust code to do actions from a remote timerjob (console app) on several sitecollections in my farm. I have simplified the code to just do a executeQuery to get the web title.
var impersonationLoginName = "USER1";
var currentUserWindowsIdentity = new WindowsIdentity(impersonationLoginName);
using (var clientContext = SharePointTokenHelper.GetS2SClientContextWithWindowsIdentity(new Uri("http://dev-local.nl/sites/pd"), windowsIdentity))
{
try
{
var siteCol = clientContext.Site;
var web = siteCol.RootWeb;
clientContext.Load(siteCol);
clientContext.Load(web, w => w.Title);
clientContext.ExecuteQuery();
Console.WriteLine(web.Title);
}
catch (System.Net.WebException wEx)
{
Console.WriteLine("Error: " + wEx.Message);
}
catch (Microsoft.SharePoint.Client.ServerException sEx)
{
Console.WriteLine("Error: " + sEx.Message);
}
}
This code works fine on all my sitecollections except 1. In this sitecollection, USER1 has permissions given through a AD group inside a SharePoint group, instead of being added to a SharePoint group itself. On this site collection I get a error message "Access denied. You are not authorized to perform this operation or access this resource." HttP status 200 (seen using Fiddler). I know my High Trust config is correct because on all other sites it is working ok. I see these errors in the ULS:
08/06/2020 11:44:18.81 w3wp.exe (0x2B28) 0x3910 SharePoint Foundation CSOM ajwqj Medium Request does not have SPBasePermissions.UseRemoteAPIs permission. Need to check it when each API is accessed 50556d9f-d6c0-b088-d589-94b668b53d71
08/06/2020 11:44:18.81 w3wp.exe (0x2B28) 0x3910 SharePoint Foundation CSOM ajwqh High The request does not have required permission to access SP.RequestContext.Current 50556d9f-d6c0-b088-d589-94b668b53d71
08/06/2020 11:44:18.81 w3wp.exe (0x2B28) 0x3910 SharePoint Foundation CSOM ahjq1 High Exception occured in scope Microsoft.SharePoint.SPContext.get_Current. Exception=System.UnauthorizedAccessException: Attempted to perform an unauthorized operation. at Microsoft.SharePoint.Client.ServerStub.CheckRequiredResourceRight(ProxyContext proxyContext, String memberName, ResourceRight right) at Microsoft.SharePoint.Client.ServerStub.CheckBlockedGetProperty(String propName, ProxyContext proxyContext) at Microsoft.SharePoint.ServerStub.SPContextServerStub.GetStaticProperty(String propName, ProxyContext proxyContext) at Microsoft.SharePoint.Client.ServerStub.GetStaticPropertyWithMonitoredScope(String propertyName, ProxyContext proxyContext) 50556d9f-d6c0-b088-d589-94b668b53d71
08/06/2020 11:44:18.81 w3wp.exe (0x2B28) 0x3910 SharePoint Foundation CSOM agmjp High Original error: System.UnauthorizedAccessException: Attempted to perform an unauthorized operation. at Microsoft.SharePoint.Client.ServerStub.CheckRequiredResourceRight(ProxyContext proxyContext, String memberName, ResourceRight right) at Microsoft.SharePoint.Client.ServerStub.CheckBlockedGetProperty(String propName, ProxyContext proxyContext) at Microsoft.SharePoint.ServerStub.SPContextServerStub.GetStaticProperty(String propName, ProxyContext proxyContext) at Microsoft.SharePoint.Client.ServerStub.GetStaticPropertyWithMonitoredScope(String propertyName, ProxyContext proxyContext) 50556d9f-d6c0-b088-d589-94b668b53d71
08/06/2020 11:44:18.81 w3wp.exe (0x2B28) 0x3910 SharePoint Foundation CSOM agmjp High Original error: System.UnauthorizedAccessException: Attempted to perform an unauthorized operation. at Microsoft.SharePoint.Client.ServerStub.CheckRequiredResourceRight(ProxyContext proxyContext, String memberName, ResourceRight right) at Microsoft.SharePoint.Client.ServerStub.CheckBlockedGetProperty(String propName, ProxyContext proxyContext) at Microsoft.SharePoint.ServerStub.SPContextServerStub.GetStaticProperty(String propName, ProxyContext proxyContext) at Microsoft.SharePoint.Client.ServerStub.GetStaticPropertyWithMonitoredScope(String propertyName, ProxyContext proxyContext) at Microsoft.SharePoint.Client.ClientMethodsProcessor.InvokeStaticGetProperty(String typeId, String propName) at Microsoft.SharePoint.Client.ClientMethodsProcessor.GetObjectFromObjectPath(XmlElement xe) at Microsoft.SharePoint.Client.ClientMethodsProcessor.GetObjectFromObjectPathId(String objectPathId) at Microsoft.SharePoint.Client.ClientMethodsProcessor.ProcessInstantiateObjectPath(XmlElement xe) at Microsoft.SharePoint.Client.ClientMethodsProcessor.ProcessOne(XmlElement xe) at Microsoft.SharePoint.Client.ClientMethodsProcessor.ProcessStatements(XmlNode xe) at Microsoft.SharePoint.Client.ClientMethodsProcessor.Process() 50556d9f-d6c0-b088-d589-94b668b53d71
08/06/2020 11:44:18.81 w3wp.exe (0x2B28) 0x3910 SharePoint Foundation CSOM afxv7 High System.UnauthorizedAccessException: Attempted to perform an unauthorized operation. at Microsoft.SharePoint.Client.ServerStub.CheckRequiredResourceRight(ProxyContext proxyContext, String memberName, ResourceRight right) at Microsoft.SharePoint.Client.ServerStub.CheckBlockedGetProperty(String propName, ProxyContext proxyContext) at Microsoft.SharePoint.ServerStub.SPContextServerStub.GetStaticProperty(String propName, ProxyContext proxyContext) at Microsoft.SharePoint.Client.ServerStub.GetStaticPropertyWithMonitoredScope(String propertyName, ProxyContext proxyContext) at Microsoft.SharePoint.Client.ClientMethodsProcessor.InvokeStaticGetProperty(String typeId, String propName) at Microsoft.SharePoint.Client.ClientMethodsProcessor.GetObjectFromObjectPath(XmlElement xe) at Microsoft.SharePoint.Client.ClientMethodsProcessor.GetObjectFromObjectPathId(String objectPathId) at Microsoft.SharePoint.Client.ClientMethodsProcessor.ProcessInstantiateObjectPath(XmlElement xe) at Microsoft.SharePoint.Client.ClientMethodsProcessor.ProcessOne(XmlElement xe) at Microsoft.SharePoint.Client.ClientMethodsProcessor.ProcessStatements(XmlNode xe) at Microsoft.SharePoint.Client.ClientMethodsProcessor.Process() 50556d9f-d6c0-b088-d589-94b668b53d71
08/06/2020 11:44:18.81 w3wp.exe (0x2B28) 0x3910 SharePoint Foundation CSOM aiv4i High RequestMessage: <Request AddExpandoFieldTypeSuffix="true" SchemaVersion="15.0.0.0" LibraryVersion="15.0.0.0" ApplicationName=".NET Library" xmlns="http://schemas.microsoft.com/sharepoint/clientquery/2009"><Actions><ObjectPath Id="2" ObjectPathId="1" /><ObjectPath Id="4" ObjectPathId="3" /><ObjectPath Id="6" ObjectPathId="5" /><Query Id="7" ObjectPathId="3"><Query SelectAllProperties="true"><Properties /></Query></Query><Query Id="8" ObjectPathId="5"><Query SelectAllProperties="false"><Properties><Property Name="Title" ScalarProperty="true" /></Properties></Query></Query></Actions><ObjectPaths><StaticProperty Id="1" TypeId="{3747adcd-a3c3-41b9-bfab-4a64dd2f1e0a}" Name="Current" /><Property Id="3" ParentId="1" Name="Site" /><Property Id="5" ParentId="3" Name="RootWeb" /></ObjectPaths></Request> 50556d9f-d6c0-b088-d589-94b668b53d71
08/06/2020 11:44:18.81 w3wp.exe (0x2B28) 0x3910 SharePoint Foundation CSOM agmjp High Original error: System.UnauthorizedAccessException: Attempted to perform an unauthorized operation. at Microsoft.SharePoint.Client.ServerStub.CheckRequiredResourceRight(ProxyContext proxyContext, String memberName, ResourceRight right) at Microsoft.SharePoint.Client.ServerStub.CheckBlockedGetProperty(String propName, ProxyContext proxyContext) at Microsoft.SharePoint.ServerStub.SPContextServerStub.GetStaticProperty(String propName, ProxyContext proxyContext) at Microsoft.SharePoint.Client.ServerStub.GetStaticPropertyWithMonitoredScope(String propertyName, ProxyContext proxyContext) at Microsoft.SharePoint.Client.ClientMethodsProcessor.InvokeStaticGetProperty(String typeId, String propName) at Microsoft.SharePoint.Client.ClientMethodsProcessor.GetObjectFromObjectPath(XmlElement xe) at Microsoft.SharePoint.Client.ClientMethodsProcessor.GetObjectFromObjectPathId(String objectPathId) at Microsoft.SharePoint.Client.ClientMethodsProcessor.ProcessInstantiateObjectPath(XmlElement xe) at Microsoft.SharePoint.Client.ClientMethodsProcessor.ProcessOne(XmlElement xe) at Microsoft.SharePoint.Client.ClientMethodsProcessor.ProcessStatements(XmlNode xe) at Microsoft.SharePoint.Client.ClientMethodsProcessor.Process() 50556d9f-d6c0-b088-d589-94b668b53d71
When I do a check permissions I do have the permission to access the api. The strange thing that also happens is, when I open the site in the browser using USER1 and then fire the code again the code doesn't give the access denied error and keeps working. When I do a reboot the code stops working again. If I wait a couple of hours the code stops working also.
I also tested with a simple 'normal' cientcontext
using (var clientContext = new ClientContext(new Uri("http://dev-local.nl/sites/pd")))
{
try
{
var siteCol = clientContext.Site;
var web = siteCol.RootWeb;
clientContext.Load(siteCol);
clientContext.Load(web, w => w.Title);
clientContext.ExecuteQuery();
Console.WriteLine(web.Title);
}
catch (System.Net.WebException wEx)
{
Console.WriteLine("Error: " + wEx.Message);
}
catch (Microsoft.SharePoint.Client.ServerException sEx)
{
Console.WriteLine("Error: " + sEx.Message);
}
}
That works. When I fire this code once, the High Trust code works also after this. Until a reboot or a couple of hours. I need the High Trust solution so a normal clientcontext is not usable.
Update
I found out that the code stops working when restarting the "App fabric cache windows service" what happens when a reboot occurs.