Forum Discussion

Raj4100's avatar
Raj4100
Copper Contributor
Sep 24, 2019

Not able to connect SharePoint online through SharePoint CSOM package.

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?

  • Vikram_Samal's avatar
    Vikram_Samal
    Steel Contributor

    Raj4100  Please find the below code make sure you add the following package as well 

     

     

     
    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;
    }
    }

Resources