I'm trying to connect to SharePoint online to read list data. Problem - I need to use a service account since these accounts have passwords. The credentials class needs a password and our user accounts don't have them. We use chip cards, pins and tokens, etc. I noticed through trial and error that it will not accept a username for a service account unless it is formatted like an email address, but then, SharePoint doesn't recognize it. Is that actually the way the password needs to be formatted to work? Any suggestions?

Thank you

2 Answers 2


As a short answer: Yes, you need to provide the username as an email with a secured password in order to connect SharePoint Online using CSOM as below:

C# Sample

using (ClientContext context = new ClientContext("https://SiteURL/")) {

    var pwd = "mypassword";
    var passWord = new SecureString();
    foreach (char c in pwd.ToCharArray()) passWord.AppendChar(c);
    context.Credentials = new SharePointOnlineCredentials("useremail", password);
    // CSOM code


PowerShell Sample

$Password = "your password"
$Cred = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($UserName,(ConvertTo-SecureString $Password -AsPlainText -Force))
$Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteUrl)
$Ctx.Credentials = $Cred

In SharePoint online, The username for login should be UPN:

In Microsoft 365, The UPN is formatted like an email address and it is the default attribute that's used to generate the email address.

it's suggested to use PNP csom lib to help to connect to SPO:


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.