0

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);
mylibrary.RootFolder.Files.Add(fcinfo); 
ctx.ExecuteQuery()
0

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.

http://www.sharepointpals.com/post/How-to-Get-the-Client-Context-Using-App-Access-token-by-passing-Client-ID-and-Client-Secret-ID-using-CSOM-in-SharePoint-Office-365

Hope this helps... happy coding!!!

1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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