400 Bad Request - Protocol Error when using GetSharePointOnlineAuthenticatedContextTenant

Copper Contributor

I am trying to create an app using CSOM in C# for a service account to upload reports to a site collection.

  • The account is a site collection admin
  • The account can be used through a browser to log in to the site collection and upload files through the UI

Using the very basic code snippet below, when I attempt to load the Web object after authenticating with the user name and SecureString password for the account, I receive a 400 Bad Request Error stating that there was a protocol error. 

 

I tried switching from the PnPAuthenticationManager to manually building SharePointOnlineCredentials with the same result.  Has something recently changed in the way SharePoint Online credentials are supposed to be built?  It's been a year or so since I have tried do things this way but I don't recall having this issue in the past with the same account and tenant.

 

using (context = authManager.GetSharePointOnlineAuthenticatedContextTenant(urlSiteCollection, user, pwd))
{
try
{
Web web = context.Web;
context.Load(web);
context.ExecuteQuery();  // <-- Exception is thrown here
}
catch (Exception ex)
{

}
}

1 Reply

Hi @Caleb Miller

 

I dont remember using this function you are using ....ContextTenant so I guess there is the problem.

 

public static SecureString ToSecureString(this string Source)
{
    if (string.IsNullOrWhiteSpace(Source))
        return null;
    else
    {
        SecureString Result = new SecureString();
        foreach (char c in Source.ToCharArray())
            Result.AppendChar(c);
        return Result;
    }
}

public ConnectAndDoStuff(){
    ClientContext context = new ClientContext("https://contoso.sharepoint.com/sites/doStuffSite");
    var pass = "MyPassword";
    var password = pass.ToSecureString();
    context.Credentials = new SharePointOnlineCredentials("user@contoso.com", password);

    var web = context.Web;
    context.Load(web);
    context.ExecuteQuery();
}