Piyush
Piyush

Reputation: 826

Connect to SharePoint Online via proxy server in Client Object Model

I have a desktop application where I am using CSOM to connect to the SharePoint Online site directly. The problem now is that this request has to be made through a proxy server. What I have done so far is that I have passed the proxy servers settings, including the credentials to the ExecutingWebRequest event of the ClientContext. However, the problem is the SharePointOnlineCredentials class. I have to set the Credentials of the ClientContext. When I am passing the userID & password to the constructor of this class, it is internally making a request to SP to validate the credentials. Now, I am unable to set/pass the proxy id/password to this class which is why the proxy server is refusing the request I am getting an IdcrlException. Below is the sample code I am using as of now.

SecureString passWord = new SecureString();
password.ToList().ForEach(passWord.AppendChar);
SP.ClientContext ctx = new SP.ClientContext(targetURL);

ctx.ExecutingWebRequest += (sen, args) =>
    {
         System.Net.WebProxy myProxy = new System.Net.WebProxy();
         myProxy.Address = new Uri(this.proxyUrl);

         myProxy.Credentials = new System.Net.NetworkCredential(this.proxyUserName, this.proxyPassword);
         args.WebRequestExecutor.WebRequest.Proxy = myProxy;
     };


//This is the line which is causing the issue. 
ctx.Credentials = new SP.SharePointOnlineCredentials(this.userName, passWord);

The code expectedly runs successfully on proxy servers which do not require any authentication. It's this SharePointOnlineCredentials that I am not able to configure. I have also tried to use NetworkCredentials in the place of SharePointOnlineCredentials. The code compiles successfully but SP is throwing Forbidden exception.

Upvotes: 2

Views: 5430

Answers (1)

Andrew
Andrew

Reputation: 11

Have you tried setting the Proxy Settings in the web config file? For example:

  <system.net>
    <defaultProxy>
      <proxy
         usesystemdefault="False"
         proxyaddress="https://MyCompanyName.sharepoint.com/"
         bypassonlocal="True"

         />
    </defaultProxy>
  </system.net>

Then override in your code by setting the username to connect with.

Also I am having difficulty following where the line of code is that is crashing your: ctx.Crednetials = new SP.SharePointOnlineCredentials(this.userName, passWord);

Upvotes: 1

Related Questions