400 Bad Request - Protocol Error when using GetSharePointOnlineAuthenticatedContextTenant

%3CLINGO-SUB%20id%3D%22lingo-sub-1092649%22%20slang%3D%22en-US%22%3E400%20Bad%20Request%20-%20Protocol%20Error%20when%20using%20GetSharePointOnlineAuthenticatedContextTenant%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1092649%22%20slang%3D%22en-US%22%3E%3CP%3EI%20am%20trying%20to%20create%20an%20app%20using%20CSOM%20in%20C%23%20for%20a%20service%20account%20to%20upload%20reports%20to%20a%20site%20collection.%3C%2FP%3E%3CUL%3E%3CLI%3EThe%20account%20is%20a%20site%20collection%20admin%3C%2FLI%3E%3CLI%3EThe%20account%20can%20be%20used%20through%20a%20browser%20to%20log%20in%20to%20the%20site%20collection%20and%20upload%20files%20through%20the%20UI%3C%2FLI%3E%3C%2FUL%3E%3CP%3EUsing%20the%20very%20basic%20code%20snippet%20below%2C%20when%20I%20attempt%20to%20load%20the%20Web%20object%20after%20authenticating%20with%20the%20user%20name%20and%20SecureString%20password%20for%20the%20account%2C%20I%20receive%20a%20400%20Bad%20Request%20Error%20stating%20that%20there%20was%20a%20protocol%20error.%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI%20tried%20switching%20from%20the%20PnPAuthenticationManager%20to%20manually%20building%20SharePointOnlineCredentials%20with%20the%20same%20result.%26nbsp%3B%20Has%20something%20recently%20changed%20in%20the%20way%20SharePoint%20Online%20credentials%20are%20supposed%20to%20be%20built%3F%26nbsp%3B%20It's%20been%20a%20year%20or%20so%20since%20I%20have%20tried%20do%20things%20this%20way%20but%20I%20don't%20recall%20having%20this%20issue%20in%20the%20past%20with%20the%20same%20account%20and%20tenant.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3Eusing%20(context%20%3D%20authManager.GetSharePointOnlineAuthenticatedContextTenant(urlSiteCollection%2C%20user%2C%20pwd))%3CBR%20%2F%3E%7B%3CBR%20%2F%3Etry%3CBR%20%2F%3E%7B%3CBR%20%2F%3EWeb%20web%20%3D%20context.Web%3B%3CBR%20%2F%3Econtext.Load(web)%3B%3CBR%20%2F%3Econtext.ExecuteQuery()%3B%26nbsp%3B%20%2F%2F%20%26lt%3B--%20Exception%20is%20thrown%20here%3CBR%20%2F%3E%7D%3CBR%20%2F%3Ecatch%20(Exception%20ex)%3CBR%20%2F%3E%7B%3CBR%20%2F%3E%3CBR%20%2F%3E%7D%3CBR%20%2F%3E%7D%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-1095769%22%20slang%3D%22en-US%22%3ERe%3A%20400%20Bad%20Request%20-%20Protocol%20Error%20when%20using%20GetSharePointOnlineAuthenticatedContextTenant%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1095769%22%20slang%3D%22en-US%22%3E%3CP%3EHi%26nbsp%3B%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F110916%22%20target%3D%22_blank%22%3E%40Caleb%20Miller%3C%2FA%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI%20dont%20remember%20using%20this%20function%20you%20are%20using%20....ContextTenant%20so%20I%20guess%20there%20is%20the%20problem.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3Epublic%20static%20SecureString%20ToSecureString(this%20string%20Source)%3CBR%20%2F%3E%7B%3CBR%20%2F%3E%26nbsp%3B%20%26nbsp%3B%20if%20(string.IsNullOrWhiteSpace(Source))%3CBR%20%2F%3E%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20return%20null%3B%3CBR%20%2F%3E%26nbsp%3B%20%26nbsp%3B%20else%3CBR%20%2F%3E%26nbsp%3B%20%26nbsp%3B%20%7B%3CBR%20%2F%3E%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20SecureString%20Result%20%3D%20new%20SecureString()%3B%3CBR%20%2F%3E%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20foreach%20(char%20c%20in%20Source.ToCharArray())%3CBR%20%2F%3E%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20Result.AppendChar(c)%3B%3CBR%20%2F%3E%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20return%20Result%3B%3CBR%20%2F%3E%26nbsp%3B%20%26nbsp%3B%20%7D%3CBR%20%2F%3E%7D%3C%2FP%3E%3CP%3Epublic%20ConnectAndDoStuff()%7B%3CBR%20%2F%3E%26nbsp%3B%20%26nbsp%3B%20ClientContext%20context%20%3D%20new%20ClientContext(%22%3CA%20href%3D%22https%3A%2F%2Fcontoso.sharepoint.com%2Fsites%2FdoStuffSite%22%20target%3D%22_blank%22%20rel%3D%22nofollow%20noopener%20noreferrer%22%3Ehttps%3A%2F%2Fcontoso.sharepoint.com%2Fsites%2FdoStuffSite%3C%2FA%3E%22)%3B%3CBR%20%2F%3E%26nbsp%3B%20%26nbsp%3B%20var%20pass%20%3D%20%22MyPassword%22%3B%3CBR%20%2F%3E%26nbsp%3B%20%26nbsp%3B%20var%20password%20%3D%20pass.ToSecureString()%3B%3CBR%20%2F%3E%26nbsp%3B%20%26nbsp%3B%20context.Credentials%20%3D%20new%20SharePointOnlineCredentials(%22user%40contoso.com%22%2C%20password)%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%20%26nbsp%3B%20var%20web%20%3D%20context.Web%3B%3CBR%20%2F%3E%26nbsp%3B%20%26nbsp%3B%20context.Load(web)%3B%3CBR%20%2F%3E%26nbsp%3B%20%26nbsp%3B%20context.ExecuteQuery()%3B%3CBR%20%2F%3E%7D%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E
Occasional 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();
}