locked
Access SharePoint 2010 site from different machine. RRS feed

  • Question

  • Hello All,

    I am trying to access SharePoint 2010 site from different server to my local machine in code.

    I am getting following error. Can anyone please help why this is happening ? Is there anything I missed in code ? 

    Error:

    The remote server returned an error: (401) Unauthorized.

    Code:

      string aa = @"abc123";
                string url = "http://MySharepointSiteName/";
                NetworkCredential myCredentials = new NetworkCredential(aa, "myPassword");
                // Create a WebRequest with the specified URL. 
                WebRequest myWebRequest = WebRequest.Create(url);                    
                Console.WriteLine("\n\nRequest to Url is sent.Waiting for response...");
                // Send the request and wait for a response.
                WebResponse myWebResponse = myWebRequest.GetResponse();
                // Process the response.
                Console.WriteLine("\nResponse received successfully.");
                // Release the resources of the response object.
                myWebResponse.Close();

    Thanks,

    Harish Patil

    Thursday, May 30, 2013 6:49 AM

Answers

  • By default CredentialCache.DefaultCredentials will be application pool account of your web application(that makes the call). You can change this to current user (you) by changing impersonation settings in web.config of your web application(that makes the call). But when calling code and page/service to call are on diffrent computers (which is the case), this will not work without Kerberos; and Kerberos is another quite complex story...

    If you want to connect with certain user and you do have username/pwd of that user, then you don't have to bother with this complicated stuff. Just use myWebRequest.Credentials = new NetworkCredentials(user, pwd, domain)  and it should work.


    Tofig

    Thursday, May 30, 2013 7:35 AM

All replies

  • From first look code seems correct except that you dont do anything with myCredential.

    Anyway I would suggest that you first try without credentials. To do that make the SharePoint site anonymously accesable and try. If this works, second step will be solving it with credential...

    For second step you are missing assignment of the credential to the request.

    If you wish to pass current windows user's credentials:

    myWebRequest.Credentials = CredentialCache.DefaultCredentials;

    If you wish to pass diffrenet credentials :

    myWebRequest.Credentials = new NetworkCredentials(user, pwd, domain);


    Tofig

    Thursday, May 30, 2013 7:10 AM
  • Hello Tofig,

    Actually I trying with following code also. But I am getting same error.

    Can you please tell me is there any other way ?

    string url = "MySharepointSiteName";

     WebRequest myWebRequest = WebRequest.Create(url);

     myWebRequest.Credentials = CredentialCache.DefaultCredentials;

     WebResponse myWebResponse = myWebRequest.GetResponse();

    myWebResponse.Close();

    Thanks,

    Harish

    Thursday, May 30, 2013 7:23 AM
  • By default CredentialCache.DefaultCredentials will be application pool account of your web application(that makes the call). You can change this to current user (you) by changing impersonation settings in web.config of your web application(that makes the call). But when calling code and page/service to call are on diffrent computers (which is the case), this will not work without Kerberos; and Kerberos is another quite complex story...

    If you want to connect with certain user and you do have username/pwd of that user, then you don't have to bother with this complicated stuff. Just use myWebRequest.Credentials = new NetworkCredentials(user, pwd, domain)  and it should work.


    Tofig

    Thursday, May 30, 2013 7:35 AM