I am getting following error

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

While I'm trying to read a website hosted in SharePoint. I have tried following code to access my SharePoint site via my C#.

String Id="xyz.onmicrosoft.com";
String pass="Password";
SecureString password = new SecureString();
            foreach (char ch in pass)
            Uri uri = new Uri("https://mydomain.sharepoint.com/sites/parts/");
            using (var context = new ClientContext(uri)) 
                context.Credentials = new SharePointOnlineCredentials(Id,password);
                Web web=context.Web;
                context.Load(web, w => w.Title);
                System.Console.WriteLine("Web Title");

I didn't get to know whats wrong with this code. I have tried some forum solutions as well but they didn't worth to resolve this error.

1 Answer 1


Try this:

clientContext.Credentials = new NetworkCredential("username", "password", "domain");

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.