Not able to connect SharePoint online through SharePoint CSOM package.

Copper Contributor

I am trying to connect to SharePoint online using SharePoint CSOM package in C#. I am using a service account to get access. When I tried to logon to site directly through Website, I can connect. But when I tried to connect through C# code, I couldn’t connect and got the error stating “The sign-in name or password does not match one in the Microsoft account system.”

I provided member access to the above-mentioned account. I add one more service account to my SharePoint Site and tried the same and I was able to connect through C# code as well. Is there any specific type of account which can access to SharePoint online through CSOM package?

Can anyone help me to resolve this issue?

1 Reply

@Raj4100  Please find the below code make sure you add the following package as well 2019-09-24_1328.png

 

 

 
Spoiler
using (ClientContext clientContext = new ClientContext("https://youdomain.sharepoint.com/sites/test")
{
clientContext.Credentials = new SharePointOnlineCredentials("vsamal@yourdomain.onmicrosoft.com", GetSPOSecureStringPassword("YourPassword"));
Web web = clientContext.Web;
clientContext.Load(web);
clientContext.ExecuteQuery();
Console.WriteLine("Web site name " + web.Title);
}

An you will need one more function which will convert your text password to secure password:

Spoiler
private static SecureString GetSPOSecureStringPassword(string spoAccPwd)
{
try
{
SecureString secureString = new SecureString();
foreach (char c in spoAccPwd)
{
secureString.AppendChar(c);
}
return secureString;
}
catch (Exception ex)
{
Console.WriteLine("Got error Reading the Password. Error: " + ex.Message);
throw ex;
}
}