I'm using CSOM for making REST API calls to a SharePoint list from a C# application. This needs credentials (of course), however I've found that it needs a username and a password in plain text. Something like code below:
using (ClientContext context = new ClientContext("https://site.sharepoint.com"))
{
context.AuthenticationMode = ClientAuthenticationMode.FormsAuthentication;
context.FormsAuthenticationLoginInfo = new FormsAuthenticationLoginInfo("[email protected]", "P@ssw0rd");
// Rest of the code
context.ExecuteQuery(); // <-- error on this line
}
I've two issues with this code:
However I use the correct credentials, it's given me this error on the marked line:
The user's login name or password is not valid.
Security. It's not secure to add the credentials in plain text in the code.
I've also tried code below, but I get a 403 response (also with the correct credentials) and it's not secure too.
using (ClientContext context = new ClientContext("https://site.sharepoint.com"))
{
context.Credentials = new NetworkCredential("[email protected]", "P@ssw0rd");
// Rest of the code
context.ExecuteQuery(); // <-- error on this line
}
In the header stand this:
Access denied. Before opening files in this location: you must first browse to the web site and select the option to login automatically.
How can I solve this, especially the second issue?