I need to use windows credentials instead of hard coded password because it's against our company policy to use hard coded username and password. I check some threads here and tried everything but my code is not working. below is my code. It's giving me 403 reponse whenver I don't use SharepointOnlineCredentials(username, password);

ClientContext ctx = new ClientContext(siteUrl);
string excelFilePath = @"C:\Users\PHPaningRa\Documents\test folder\" + fileName;
string libraryname = "ITT Test Library";
ctx.AuthenticationMode = ClientAuthenticationMode.Default;

ctx.Credentials = CredentialCache.DefaultCredentials;
FileCreationInformation fcinfo = new FileCreationInformation();
fcinfo.Url = fileName;
fcinfo.Overwrite = true;
fcinfo.Content = System.IO.File.ReadAllBytes(excelFilePath);
Web myweb = ctx.Web;
List mylibrary = myweb.Lists.GetByTitle(libraryname);

1 Answer 1


Assuming you are using SharePoint online. You cannot pass default windows credentials to SharePoint online to authenticate. Single sign on does not work here.

But this is one option to use APP access token by generating client id and client secret. I have not tried this personally but would be doing so as this is good use case to check.

Please refer to below link to try this out.


Hope this helps... happy coding!!!


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.