December 13, 2024

SamTech 365

PowerPlatform, Power Apps, Power Automate, PVA, SharePoint, C#, .Net, SQL, Azure News, Tips ….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);
}