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

10,677

Solution 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.

Solution 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.

Share:
10,677
MedicineMan
Author by

MedicineMan

Java, .NET, and JavaScript developer in the SF Bay Area

Updated on June 28, 2022

Comments

  • MedicineMan
    MedicineMan almost 2 years

    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