October 3, 2022

Samir Daoudi's Technical Blog

SharePoint, Office 365, C#, .Net, SQL Server News, Tips, Ebooks, Events, Webinars ….etc

Connect from your ASP.Net to SharePoint Online or On Prem

In a previous article, I shared with you how to connect from your ASP.Net application to SharePoint. Today, I came across a different scenario, so I thought it might be helpful for you. I am pushing data from my ASP.Net application to a SharePoint list, and in the test environment, it works fine, as soon as I deploy it to a public hosting provider, the same code fails with the following error:

A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond

Apparently, the way I had to pass the credentials was different from SharePoint OnPrem to SharePoint Online.

Authenticating with SharePoint Online

using (srcContext = new ClientContext(txtUrlFrom.Text))
       SecureString passWord = new SecureString();
       foreach (char c in txtPasswordFrom.Text.ToCharArray()) passWord.AppendChar(c);
       srcContext.Credentials = new SharePointOnlineCredentials(txtUserNameFrom.Text, passWord);

Authenticating with SharePoint OnPrem

using (srcContext = new ClientContext(txtUrlFrom.Text))
  NetworkCredential credentials = new NetworkCredential(txtUserNameFrom.Text, txtPasswordFrom.Text);

Leave a comment

You may have missed

Show Buttons
Hide Buttons

Subscribe to Blog via Email

Enter your email address to subscribe to this blog and receive notifications of new posts by email.

%d bloggers like this: