How do I supply credentials to execute a query on a server that's part of another domain?

吃可爱长大的小学妹 提交于 2019-12-22 10:09:38

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!