I'm currently having an issue using SharePointOnlineCredentials within my web application. The error I'm getting is
Catastrophic failure - value does not fall within expected range error
. According to this article here it is related to the application pool identity because SharepointOnlineCredentials requires access to system resources including the registry.
I'm wondering if this is actually a true statement as i cannot believe that you have to give your application full rights to the system just to authenticate. If this is true is there any other way to authenticate to sharepoint online web services from a web application without using SharepointOnlineCredentials?
private CookieContainer GetAuthCookies(Uri webUri, string userName, string password)
{
var securePassword = new SecureString();
foreach (var c in password) { securePassword.AppendChar(c); }
var credentials = new SharePointOnlineCredentials(userName, securePassword);
var authCookie = credentials.GetAuthenticationCookie(webUri);
var cookieContainer = new CookieContainer();
cookieContainer.SetCookies(webUri, authCookie);
return cookieContainer;
}
public void GetListItems()
{
string targetSite = new Uri("https://sharepointOnlineURLRoot/sites/siteName");
var listService = new Lists
{
Url = targetSite + "/_vti_bin/Lists.asmx",
CookieContainer = GetAuthCookies(targetSite, "someuser", "somepassword")
};
//list.Timeout = 15000; //in milliseconds
string listName = "Shared Documents";
string viewName = "";
//string listName = "{1A4A3C5D-360E-45EB-B9ED-E8653981CAC0}";
//string viewName = "{5A4AF2C5-8A9F-427F-B8AA-BC59E3BE8AA0}";
string rowLimit = "5";
// Instantiate an XmlDocument object
var xmlDoc = new System.Xml.XmlDocument();
System.Xml.XmlElement query = xmlDoc.CreateElement("Query");
System.Xml.XmlElement viewFields = xmlDoc.CreateElement("ViewFields");
System.Xml.XmlElement queryOptions = xmlDoc.CreateElement("QueryOptions");
//*Use CAML query*/
//query.InnerXml = "<Where><Gt><FieldRef Name=\"ID\" /><Value Type=\"Counter\">0</Value></Gt></Where>";
viewFields.InnerXml = "<FieldRef Name=\"Title\" />";
//queryOptions.InnerXml = "";
queryOptions.InnerXml = "<IncludeMandatoryColumns>FALSE</IncludeMandatoryColumns><DateInUtc>TRUE</DateInUtc>";
System.Xml.XmlNode nodes =
listService.GetListItems(
listName,
viewName,
query,
viewFields,
rowLimit,
null,
string.Empty);
string ixml = listService.GetList(listName).InnerXml;
}
Here is the code i'm using. I just wanted to note that when i do change my application pool for this web app to Local System the above code works properly. When i leave it at ApplicationPoolIdentity or Network Service i get the above error.