SharePoint Asked by H. Pauwelyn on October 25, 2021
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?
Sharepoint Online requires SecureString object as password. You can do something like this:
SecureString se = new SecureString();
foreach (var cc in password)
{
se.AppendChar(cc);
}
var cre = new SharePointOnlineCredentials("[email protected]", se);
using (ClientContext context = new ClientContext("https://site.sharepoint.com"))
{
context.Credentials = cre;
// Rest of the code
context.ExecuteQuery();
}
To solve the plain text password problem, you can store it in the Windows Credential Manager then retrieve it using this Library CredentialManager
Answered by Jack Le on October 25, 2021
Get help from others!
Recent Answers
Recent Questions
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP