Search code examples
dynamics-crmmulti-factor-authentication

Using Multi Factor authentication with Xrm.Tooling.CrmConnectControl


I have tried logging in to an Multi Factor Authentification (MFA) enabled 365 Online environment using both the Xrm.Tooling.CrmConnectControl from the CRM SDK Template (added to existing project) downloadable at https://marketplace.visualstudio.com/items?itemName=DynamicsCRMPG.MicrosoftDynamicsCRMSDKTemplates and the one provided in the lastest SDK SampleCode called QuickStartXRMToolingWPFClient. I have not successfully logged into the MFA environment in either case. Ofcourse all the non-MFA environments I have tried to log into works as intended using both the described solutions.

In Plugin Registration Tool v.8.2.0.8411 it is possible to log into an MFA-enabled environment by leaving both User Name and Password blank when targeting Deployment Type Office 365. However when using the CrmConnectControl from the Template/SDK leaving User Name and Password blank results in the error message "A user name is required." alternatively "A password is required.".

Does anyone know if there is a way to use the Xrm.Tooling.CrmConnectControl to authenticate to a MFA environment in the same way as it is done in the Plugin Registration Tool?


Solution

  • The solution is for the SampleCode from the latest SDK for Dynamics 365.

    First of all I see that this QuickStartXRMToolingWPFClient source code uses outdated SDK - go to nuget package manager and update all Microsoft.CrmSdk packages to the latest versions (don't update Microsoft.IdentityModel by yourself - it will be updated to the proper version as a dependency for the SDK assemblies).

    Now, inside CrmLogin.xaml.cs you have a function Window_Loaded and CrmConnectionManager initialized like so:

    private void Window_Loaded(object sender, RoutedEventArgs e)
    {
        //some code
        mgr = new CrmConnectionManager();
        mgr.ParentControl = CrmLoginCtrl;
        mgr.UseUserLocalDirectoryForConfigStore = true;
        //some code
    }
    

    you must add the following lines:

    private void Window_Loaded(object sender, RoutedEventArgs e)
    {
        //some code
        mgr = new CrmConnectionManager();
        mgr.ParentControl = CrmLoginCtrl;
        mgr.UseUserLocalDirectoryForConfigStore = true;
        mgr.ClientId = "2ad88395-b77d-4561-9441-d0e40824f9bc";
        mgr.RedirectUri = new Uri("app://5d3e90d6-aa8e-48a8-8f2c-58b45cc67315");
        //some code
    }
    

    Rebuilt and it will work exactly as PluginRegistrationTool