Home > Mobile >  Dynamics365/O365 login for WinForms Application active MFA
Dynamics365/O365 login for WinForms Application active MFA

Time:07-01

I am working on an application that connects to Dynamics 365 and creates contacts there, for example. My question now is how to configure the login. For testing, multi-factor authentication is disabled and it works so far. However, in the production system, MFA will be active. I want to avoid users using an app password. Is there a way to pop up the standard O365 login popup window from within a WinForms application? Unfortunately I couldn't find anything about it. What would make the most sense here? Thats my code so far:

        public CrmServiceClient connect_crm()
    {
        string ConnectionString = "AuthType = OAuth; "  
              "Username= "   textBox1.Text   ";"  
              "Password="   textBox2.Text   ";"  
              "Url=https://company.dynamics.com;"  
              "RedirectUri=app://;"   
              "AppId=XXXXXXXX-XXXXX-XXXX-XXXX-XXXXXXXXXXXXX;"  
              "LoginPrompt=Auto";
        ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
        svc = new CrmServiceClient(ConnectionString);
        try
        {
            if (svc != null && svc.IsReady)
            {
                label4.BackColor = Color.LightGreen;
                label4.Text = "Connected";
                CreateContact2(svc);
            }
        }
        catch (Exception ex)
        {

            MessageBox.Show(ex.Message);
        }
        return svc;
    }

CodePudding user response:

I've got the solution! You need to use the client secret from the app, you registered in azure. And you have to create an application user in dynamics. (via power platform admin ceter) See here: https://softchief.com/2021/08/03/connect-dynamics-365-from-console-c-application-in-mfa-enabled-access-using-client-secret-and-azure-client-id/

        public CrmServiceClient connect_crm()
    {
        string ConnectionString = "AuthType = ClientSecret; "  
              "Url=https://company.dynamics.com;"  
              "ClientId=XXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXX;"   //AppID
              "ClientSecret=XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"; //ClientKey
        ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
        svc = new CrmServiceClient(ConnectionString);
        try
        {
            if (svc != null && svc.IsReady)
            {
                label4.BackColor = Color.LightGreen;
                label4.Text = "Connected!";
            }
        }
        catch (Exception ex)
        {

            MessageBox.Show(ex.Message);
        }
        return svc;
    }
  • Related