0

I'm trying to connect to SharePoint online in a console App and print the title of the site. Its giving me the error : "The sign-in name or password does not match one in the Microsoft account system." I have checked and made sure the username and password are 100% right. I dont know what else to check Heres my code:

private static void SPCredentialsConnect()
    {
        const string SiteUrl = "https://tenant.sharepoint.com/sites/mysite";
        const string pwd = "appPassword";
        const string username = "username@tenant.onmicrosoft.com";

        SecureString securestring = new SecureString();
        pwd.ToCharArray().ToList().ForEach(s => securestring.AppendChar(s));

        ClientContext context = new ClientContext(SiteUrl);
        context.Credentials = new SharePointOnlineCredentials(username, securestring);

        try
        {
            var web = context.Web;
            context.Load(web);
            context.ExecuteQuery();

            Console.WriteLine($"web title: {web.Title}");
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.Message);
        }
    }
Dave Miller
  • 536
  • 3
  • 19
AdmDTR
  • 35
  • 1
  • 9
  • Update: i've tried through AppOnly Authentication and didnt work either: public static void AppOnlyAuthCall() { using (var clientContext = new AuthenticationManager().GetAppOnlyAuthenticatedContext(siteUrl, clientId, clientSecret)) { clientContext.Load(clientContext.Web, page => page.Title); clientContext.ExecuteQuery(); Console.WriteLine(clientContext.Web.Title); }; } – AdmDTR Nov 18 '21 at 15:47
  • when i used this code it tells me "System.Net.WebException: 'The remote server returned an error: (401) Unauthorized." and yes In Azure Active Directory i've granted API permissions (delegated allsites fullcontrol), "allowPublicClient": true, "oauth2AllowUrlPathMatching": true. – AdmDTR Nov 18 '21 at 15:56

2 Answers2

2

Have your issue fixed? “The sign-in name or password does not match one in the Microsoft account system” Error will occur sometimes and fixed after a while with nothing changed.

AppOnly Authentication for sharepointonline can't be registed in Azure Active Directory. It should be register in

https://contoso.sharepoint.com/_layouts/15/appregnew.aspx

And grant permission in

https://contoso-admin.sharepoint.com/_layouts/15/appinv.aspx

You can refer to following document

https://learn.microsoft.com/en-us/sharepoint/dev/solution-guidance/security-apponly-azureacs

  • i did register it and granted permission - trust - used the AppConfig and still gives me an error 401 denied with app only auth. and when connecting throguh SP Credentials its the same error has not fixed (sign-in name or password incorrect) – AdmDTR Nov 20 '21 at 11:45
  • Your account may be MFA (Multi-factor authentication) enabled! If yes, either MFA must be turned OFF or you have to follow the methods described in How to Connect to SharePoint Online with MFA-enabled accounts from PowerShell – RaytheonXie-MSFT Nov 22 '21 at 07:42
  • 1
    MFA is disabled (not set in the first place - and niether by default) this is so weird i tried to make the whole thing all over frmo creating a new account,new tenant , permissions everything all over and still the same of the two errors - using credentials i get"sign-in username or password incorrect" - using authentication manager i get "remote server forbidden 401" – AdmDTR Nov 23 '21 at 13:01
1

Consider using the PnP.Framework (a NuGet package), and use the AuthenticationManager object for SPO sites. This method bypasses MFA (which is mandatory in our organization, FWIW). You can find a lot more information and examples here, including steps on getting the client id and client secret for a site. Here is what we use to log into SPO sites:

using (ClientContext context = 
    new AuthenticationManager().GetACSAppOnlyContext(SiteUrl, clientID, clientSecret)) 
    {
        ...
    }

Also, once you connect, you should adjust the Context.Load to grab the title if you want to use that value right away. Here's what I used in my code:

context.Load(web, p => p.Id, p => p.Title);
context.ExecuteQuery();
Console.WriteLine($"Logged into source {web.Title} ({web.Id})");

Good luck!

Steve in Spain

Lee Taylor
  • 7,761
  • 16
  • 33
  • 49