public string GetConnectionString()
        {
            var csb = new DbConnectionStringBuilder();

            switch (AuthType)
            {
            default:
                csb["AuthType"] = "AD";
                break;

            case AuthenticationProviderType.OnlineFederation:
                csb["AuthType"] = "Office365";
                break;

            case AuthenticationProviderType.Federation:
                csb["AuthType"] = "IFD";
                break;
            }

            csb["Url"] = WebApplicationUrl;

            if (!string.IsNullOrEmpty(UserDomain))
            {
                csb["Domain"] = UserDomain;
            }
            csb["Username"] = UserName;
            csb["Password"] = "******";

            if (!string.IsNullOrEmpty(HomeRealmUrl))
            {
                csb["HomeRealmUri"] = HomeRealmUrl;
            }

            if (UseMfa)
            {
                csb["AuthType"]            = "OAuth";
                csb["ClientId"]            = AzureAdAppId.ToString("B");
                csb["LoginPrompt"]         = "Auto";
                csb["RedirectUri"]         = ReplyUrl;
                csb["TokenCacheStorePath"] = Path.Combine(Path.GetTempPath(), ConnectionId.Value.ToString("B"), "oauth-cache.txt");
            }

            return(csb.ToString());
        }
        private void ConnectOnline()
        {
            AuthType = AuthenticationProviderType.OnlineFederation;

            var password = CryptoManager.Decrypt(userPassword, ConnectionManager.CryptoPassPhrase,
                                                 ConnectionManager.CryptoSaltValue,
                                                 ConnectionManager.CryptoHashAlgorythm,
                                                 ConnectionManager.CryptoPasswordIterations,
                                                 ConnectionManager.CryptoInitVector,
                                                 ConnectionManager.CryptoKeySize);

            Utilities.GetOrgnameAndOnlineRegionFromServiceUri(new Uri(OriginalUrl), out var region, out var orgName, out _);

            if (UseMfa)
            {
                var path = Path.Combine(Path.GetTempPath(), ConnectionId.Value.ToString("B"), "oauth-cache.txt");

                crmSvc = new CrmServiceClient(UserName, CrmServiceClient.MakeSecureString(password),
                                              region,
                                              orgName,
                                              false,
                                              null,
                                              null,
                                              AzureAdAppId.ToString(),
                                              new Uri(ReplyUrl),
                                              path,
                                              null);
            }

            crmSvc = new CrmServiceClient(UserName, CrmServiceClient.MakeSecureString(password),
                                          region,
                                          orgName,
                                          true,
                                          true,
                                          null,
                                          true);
        }