I am creating a simple console application that connect to SharePoint Server 2016. However, I receiver error 401 unauthorized when it gets to context.ExecuteQuery. I have no issues connecting to SharePoint Online. The issue is perculiar to SharePoint Onpremise. Below is my code
string siteUrl = "http://abc.mysharepoint.local/sites/mysite";
string userName = "myname@domain.com";
string pwd = "mypassword";
string domain = "domain.com";
using (ClientContext ctx = new ClientContext(siteUrl))
{
try
{
ctx.AuthenticationMode = ClientAuthenticationMode.Default;
ctx.Credentials = new System.Net.NetworkCredential(userName, pwd, domain);
ctx.Load(ctx.Web, w => w.Title, w => w.Description);
ctx.ExecuteQuery();
Console.WriteLine(ctx.Web.Title);
}
catch (Exception ex)
{
Console.WriteLine($"{ex.Message} ");
}
}
Please what could be the possible cause and how to resolve it. I have full admin right to the site and can access the site from the browser. I have the IP to the server configured on my computer host file.
I have checked the SharePoint Server and can confirm I have access and everything looks okay. I also tried with the credentials of a service account but am still getting same 401 unauthorized error though I have full admin access to the site.