Not able to connect SharePoint online through SharePoint CSOM package.

%3CLINGO-SUB%20id%3D%22lingo-sub-871879%22%20slang%3D%22en-US%22%3ENot%20able%20to%20connect%20SharePoint%20online%20through%20SharePoint%20CSOM%20package.%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-871879%22%20slang%3D%22en-US%22%3E%3CP%3EI%20am%20trying%20to%20connect%20to%20SharePoint%20online%20using%20SharePoint%20CSOM%20package%20in%20C%23.%20I%20am%20using%20a%20service%20account%20to%20get%20access.%20When%20I%20tried%20to%20logon%20to%20site%20directly%20through%20Website%2C%20I%20can%20connect.%20But%20when%20I%20tried%20to%20connect%20through%20C%23%20code%2C%20I%20couldn%E2%80%99t%20connect%20and%20got%20the%20error%20stating%20%E2%80%9CThe%20sign-in%20name%20or%20password%20does%20not%20match%20one%20in%20the%20Microsoft%20account%20system.%E2%80%9D%3C%2FP%3E%3CP%3EI%20provided%20member%20access%20to%20the%20above-mentioned%20account.%20I%20add%20one%20more%20service%20account%20to%20my%20SharePoint%20Site%20and%20tried%20the%20same%20and%20I%20was%20able%20to%20connect%20through%20C%23%20code%20as%20well.%20Is%20there%20any%20specific%20type%20of%20account%20which%20can%20access%20to%20SharePoint%20online%20through%20CSOM%20package%3F%3C%2FP%3E%3CP%3ECan%20anyone%20help%20me%20to%20resolve%20this%20issue%3F%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-873291%22%20slang%3D%22en-US%22%3ERe%3A%20Not%20able%20to%20connect%20SharePoint%20online%20through%20SharePoint%20CSOM%20package.%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-873291%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F414181%22%20target%3D%22_blank%22%3E%40Raj4100%3C%2FA%3E%26nbsp%3B%20%3CSTRONG%3EPlease%20find%20the%20below%20code%20make%20sure%20you%20add%20the%20following%20package%20as%20well%3C%2FSTRONG%3E%26nbsp%3B%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-left%22%20style%3D%22width%3A%20319px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Fgxcuf89792.i.lithium.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F133376i71BF17BBFAE9B776%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20alt%3D%222019-09-24_1328.png%22%20title%3D%222019-09-24_1328.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%3CDIV%20class%3D%22mceNonEditable%20lia-copypaste-placeholder%22%3E%26nbsp%3B%3C%2FDIV%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CDIV%20class%3D%22mceNonEditable%20lia-copypaste-placeholder%22%3E%26nbsp%3B%3C%2FDIV%3E%3CDIV%20class%3D%22lia-spoiler-container%22%3E%3CA%20class%3D%22lia-spoiler-link%22%20href%3D%22%23%22%20rel%3D%22nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%20target%3D%22_blank%22%3ESpoiler%3C%2FA%3E%3CNOSCRIPT%3E(Highlight%20to%20read)%3C%2FNOSCRIPT%3E%3CDIV%20class%3D%22lia-spoiler-border%22%3E%3CDIV%20class%3D%22lia-spoiler-content%22%3Eusing%20(ClientContext%20clientContext%20%3D%20new%20ClientContext(%22%3CA%20href%3D%22https%3A%2F%2Fyoudomain.sharepoint.com%2Fsites%2Ftest%22%20target%3D%22_blank%22%20rel%3D%22noopener%20nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3Ehttps%3A%2F%2Fyoudomain.sharepoint.com%2Fsites%2Ftest%3C%2FA%3E%22)%3CBR%20%2F%3E%7B%3CBR%20%2F%3EclientContext.Credentials%20%3D%20new%20SharePointOnlineCredentials(%22vsamal%40yourdomain.onmicrosoft.com%22%2C%20GetSPOSecureStringPassword(%22YourPassword%22))%3B%3CBR%20%2F%3EWeb%20web%20%3D%20clientContext.Web%3B%3CBR%20%2F%3EclientContext.Load(web)%3B%3CBR%20%2F%3EclientContext.ExecuteQuery()%3B%3CBR%20%2F%3EConsole.WriteLine(%22Web%20site%20name%20%22%20%2B%20web.Title)%3B%3CBR%20%2F%3E%7D%3C%2FDIV%3E%3CNOSCRIPT%3E%3CDIV%20class%3D%22lia-spoiler-noscript-container%22%3E%3CDIV%20class%3D%22lia-spoiler-noscript-content%22%3Eusing%20(ClientContext%20clientContext%20%3D%20new%20ClientContext(%22%3CA%20href%3D%22https%3A%2F%2Fyoudomain.sharepoint.com%2Fsites%2Ftest%26quot%3B)%7BclientContext.Credentials%22%20target%3D%22_blank%22%20rel%3D%22nofollow%20noopener%20noreferrer%22%3Ehttps%3A%2F%2Fyoudomain.sharepoint.com%2Fsites%2Ftest%22)%7BclientContext.Credentials%3C%2FA%3E%20%3D%20new%20SharePointOnlineCredentials(%22vsamal%40yourdomain.onmicrosoft.com%22%2C%20GetSPOSecureStringPassword(%22YourPassword%22))%3BWeb%20web%20%3D%20clientContext.Web%3BclientContext.Load(web)%3BclientContext.ExecuteQuery()%3BConsole.WriteLine(%22Web%20site%20name%20%22%20%2B%20web.Title)%3B%7D%3C%2FDIV%3E%3C%2FDIV%3E%3C%2FNOSCRIPT%3E%3C%2FDIV%3E%3C%2FDIV%3E%3CP%3EAn%20you%20will%20need%20one%20more%20function%20which%20will%20convert%20your%20text%20password%20to%20secure%20password%3A%3C%2FP%3E%3CDIV%20class%3D%22lia-spoiler-container%22%3E%3CA%20class%3D%22lia-spoiler-link%22%20href%3D%22%23%22%20rel%3D%22nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%20target%3D%22_blank%22%3ESpoiler%3C%2FA%3E%3CNOSCRIPT%3E(Highlight%20to%20read)%3C%2FNOSCRIPT%3E%3CDIV%20class%3D%22lia-spoiler-border%22%3E%3CDIV%20class%3D%22lia-spoiler-content%22%3Eprivate%20static%20SecureString%20GetSPOSecureStringPassword(string%20spoAccPwd)%3CBR%20%2F%3E%7B%3CBR%20%2F%3Etry%3CBR%20%2F%3E%7B%3CBR%20%2F%3ESecureString%20secureString%20%3D%20new%20SecureString()%3B%3CBR%20%2F%3Eforeach%20(char%20c%20in%20spoAccPwd)%3CBR%20%2F%3E%7B%3CBR%20%2F%3EsecureString.AppendChar(c)%3B%3CBR%20%2F%3E%7D%3CBR%20%2F%3Ereturn%20secureString%3B%3CBR%20%2F%3E%7D%3CBR%20%2F%3Ecatch%20(Exception%20ex)%3CBR%20%2F%3E%7B%3CBR%20%2F%3EConsole.WriteLine(%22Got%20error%20Reading%20the%20Password.%20Error%3A%20%22%20%2B%20ex.Message)%3B%3CBR%20%2F%3Ethrow%20ex%3B%3CBR%20%2F%3E%7D%3CBR%20%2F%3E%7D%3C%2FDIV%3E%3CNOSCRIPT%3E%3CDIV%20class%3D%22lia-spoiler-noscript-container%22%3E%3CDIV%20class%3D%22lia-spoiler-noscript-content%22%3Eprivate%20static%20SecureString%20GetSPOSecureStringPassword(string%20spoAccPwd)%7Btry%7BSecureString%20secureString%20%3D%20new%20SecureString()%3Bforeach%20(char%20c%20in%20spoAccPwd)%7BsecureString.AppendChar(c)%3B%7Dreturn%20secureString%3B%7Dcatch%20(Exception%20ex)%7BConsole.WriteLine(%22Got%20error%20Reading%20the%20Password.%20Error%3A%20%22%20%2B%20ex.Message)%3Bthrow%20ex%3B%7D%7D%3C%2FDIV%3E%3C%2FDIV%3E%3C%2FNOSCRIPT%3E%3C%2FDIV%3E%3C%2FDIV%3E%3C%2FLINGO-BODY%3E
Highlighted
Visitor

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
Highlighted

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