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

Apr 24 2018 1:10 AM
i am trying to access my sharepoint 2013 site from console app using csom. i amgetting the error message. The remote server returned an error: (401) Unauthorized.
 
Code is.
 
 private static void TestConnect(string siteUrl, string userName, string password)

{
try
{
using (ClientContext context = new ClientContext(siteUrl))
{
System.Security.SecureString securePassword = new System.Security.SecureString();
foreach (char c in password.ToCharArray())
{
securePassword.AppendChar(c);
}

context.AuthenticationMode = ClientAuthenticationMode.Default;
context.Credentials = new System.Net.NetworkCredential(userName, securePassword);

Web oWeb = context.Web;
context.Load(oWeb, website => website.Title);
context.ExecuteQuery();
}

}catch (Exception ex)
{

}

Answers (1)