问题
I am doing work for a client that has a server on his domain. My workstation is on my own domain. I typically VPN into the client network using my client domain username and password. The problem with the below query is that I get a 401 Unauthorized access error when I execute the query, presumably because the wrong credentials are being sent.
The client has suggested supplying my client domain credentials, perhaps wrapped in a #if DEBUG so that the code will work properly in development environments.
How do I supply credentials? The following code will be present in an ASP MVC 2 project.
ClientContext clientContext = new ClientContext(URL);
List list = clientContext.Web.Lists.GetByTitle("My Documents");
CamlQuery camlQuery = new CamlQuery();
camlQuery.ViewXml = XML;
ListItemCollection listItems = list.GetItems(camlQuery);
clientContext.Load(
listItems,
items => items.Include(item => item["FileRef"]));
clientContext.ExecuteQuery();
I prefer to do this programatically
回答1:
Is this all I need to do:
System.Net.NetworkCredential cred = new System.Net.NetworkCredential("myname", "mypassword");
ClientContext clientContext = new ClientContext(URL);
clientContext.Credentials = cred;
I am still not getting success, but perhaps there are some other issues here.
回答2:
You could configure the Application Pool credential on the IIS, this way the "user" running the application has permission to access the other server.
回答3:
If the client account is already a valid user of the sharepoint site you just need to use the default credentials before downloading, like so..
WebClient Client=new WebClient();
Client.UseDefaultCredentials=true;
Client.DownloadFile(url, destination);
来源:https://stackoverflow.com/questions/5708084/how-do-i-supply-credentials-to-execute-a-query-on-a-server-thats-part-of-anothe