private bool disposedValue = false;         // To detect redundant calls

        /// <summary>
        /// Cleaning up the object.
        /// </summary>
        /// <param name="disposing"></param>
        protected virtual void Dispose(bool disposing)
        {
            if (!disposedValue)
            {
                if (disposing)
                {
                    if (logEntry != null)
                    {
                        logEntry.Dispose();
                    }
                }
                disposedValue = true;
            }
        }
예제 #2
0
        /// <summary>
        /// Constructor for building the hook to call into the platform.
        /// </summary>
        public DataverseTelemetryBehaviors(ConnectionService cli)
        {
            _callerCdsConnectionServiceHandler = cli;

            // reading overrides from app config if present..
            // these values override the values that are set on the client from the server.
            DataverseTraceLogger logg = new DataverseTraceLogger();

            try
            {
                // Initialize user agent
                _userAgent = "Unknown";
                if (AppDomain.CurrentDomain != null)
                {
                    _userAgent = AppDomain.CurrentDomain.FriendlyName;
                }

                _userAgent = $"{_userAgent} (CdsSvcClient:{Environs.FileVersion})";

                if (_maxFaultSize == -1 && ConfigurationManager.AppSettings.AllKeys.Contains("MaxFaultSizeOverride"))
                {
                    var maxFaultSz = ConfigurationManager.AppSettings["MaxFaultSizeOverride"];
                    if (maxFaultSz is string && !string.IsNullOrWhiteSpace(maxFaultSz))
                    {
                        int.TryParse(maxFaultSz, out _maxFaultSize);
                        if (_maxFaultSize != -1)
                        {
                            if (_maxFaultSize < MAXFAULTSIZEDEFAULT)
                            {
                                _maxFaultSize = -1;
                                logg.Log($"Failed to set MaxFaultSizeOverride property. Value found: {maxFaultSz}. Size must be larger then {MAXFAULTSIZEDEFAULT}.", System.Diagnostics.TraceEventType.Warning);
                            }
                        }
                    }
                    else
                    {
                        logg.Log($"Failed to parse MaxFaultSizeOverride property. Value found: {maxFaultSz}. MaxFaultSizeOverride must be a valid integer.", System.Diagnostics.TraceEventType.Warning);
                    }
                }

                if (_maxReceivedMessageSize == -1 && ConfigurationManager.AppSettings.AllKeys.Contains("MaxReceivedMessageSizeOverride"))
                {
                    var maxRecvSz = ConfigurationManager.AppSettings["MaxReceivedMessageSizeOverride"];
                    if (maxRecvSz is string && !string.IsNullOrWhiteSpace(maxRecvSz))
                    {
                        int.TryParse(maxRecvSz, out _maxReceivedMessageSize);
                        if (_maxReceivedMessageSize != -1)
                        {
                            if (_maxReceivedMessageSize < MAXRECVMESSAGESIZEDEFAULT)
                            {
                                _maxReceivedMessageSize = -1;
                                logg.Log($"Failed to set MaxReceivedMessageSizeOverride property. Value found: {maxRecvSz}. Size must be larger then {MAXRECVMESSAGESIZEDEFAULT}.", System.Diagnostics.TraceEventType.Warning);
                            }
                        }
                    }
                    else
                    {
                        logg.Log($"Failed to parse MaxReceivedMessageSizeOverride property. Value found: {maxRecvSz}. MaxReceivedMessageSizeOverride must be a valid integer.", System.Diagnostics.TraceEventType.Warning);
                    }
                }
            }
            catch (Exception ex)
            {
                logg.Log("Failed to process binding override properties,  Only MaxFaultSizeOverride and MaxReceivedMessageSizeOverride are supported and must be integers.", System.Diagnostics.TraceEventType.Warning, ex);
            }
            finally
            {
                logg.Dispose();
            }
        }
        private DataverseConnectionStringProcessor(string serviceUri, string userName, string password, string domain, string homeRealmUri, string authType, string requireNewInstance, string clientId, string redirectUri,
                                                   string tokenCacheStorePath, string loginPrompt, string certStoreName, string certThumbprint, string skipDiscovery, string IntegratedSecurity, string clientSecret, ILogger logger)
        {
            DataverseTraceLogger logEntry = new DataverseTraceLogger();
            Uri _serviceuriName, _realmUri;

            bool tempbool = false;

            if (bool.TryParse(skipDiscovery, out tempbool))
            {
                SkipDiscovery = tempbool;
            }
            else
            {
                SkipDiscovery = true;                  // changed to change defaulting behavior of skip discovery.
            }
            ServiceUri          = GetValidUri(serviceUri, out _serviceuriName) ? _serviceuriName : null;
            HomeRealmUri        = GetValidUri(homeRealmUri, out _realmUri) ? _realmUri : null;
            DomainName          = !string.IsNullOrWhiteSpace(domain) ? domain : string.Empty;
            UserId              = !string.IsNullOrWhiteSpace(userName) ? userName : string.Empty;
            Password            = !string.IsNullOrWhiteSpace(password) ? password : string.Empty;
            ClientId            = !string.IsNullOrWhiteSpace(clientId) ? clientId : string.Empty;
            ClientSecret        = !string.IsNullOrWhiteSpace(clientSecret) ? clientSecret : string.Empty;
            TokenCacheStorePath = !string.IsNullOrWhiteSpace(tokenCacheStorePath) ? tokenCacheStorePath : string.Empty;
            RedirectUri         = ((Uri.IsWellFormedUriString(redirectUri, UriKind.RelativeOrAbsolute)) ? new Uri(redirectUri) : null);
            CertStoreName       = certStoreName;
            CertThumbprint      = certThumbprint;

            // Check to see if use current user is configured.
            bool _IntegratedSecurity = false;

            if (!string.IsNullOrEmpty(IntegratedSecurity))
            {
                bool.TryParse(IntegratedSecurity, out _IntegratedSecurity);
            }

            bool useUniqueConnection = true;              // Set default to true to follow the old behavior.

            if (!string.IsNullOrEmpty(requireNewInstance))
            {
                bool.TryParse(requireNewInstance, out useUniqueConnection);
            }
            UseUniqueConnectionInstance = useUniqueConnection;

            //UserIdentifier = !string.IsNullOrWhiteSpace(UserId) ? new UserIdentifier(UserId, UserIdentifierType.OptionalDisplayableId) : null;

            AuthenticationType authenticationType;

            if (Enum.TryParse(authType, out authenticationType))
            {
                AuthenticationType = authenticationType;
            }
            else
            {
                logEntry?.Log($"Authentication Type \"{authType}\" is not a valid Authentication Type.", System.Diagnostics.TraceEventType.Error);
                AuthenticationType = AuthenticationType.InvalidConnection;
            }

            PromptBehavior loginBehavior;

            if (Enum.TryParse(loginPrompt, out loginBehavior))
            {
                PromptBehavior = loginBehavior;
            }
            else
            {
                PromptBehavior = PromptBehavior.Auto;
            }

            if (ServiceUri != null)
            {
                SetOrgnameAndOnlineRegion(ServiceUri);
            }

            //if the client Id was not passed, use Sample AppID
            if (string.IsNullOrWhiteSpace(ClientId))
            {
                logEntry.Log($"Client ID not supplied, using SDK Sample Client ID for this connection", System.Diagnostics.TraceEventType.Warning);
                ClientId = sampleClientId;                // sample client ID
                if (RedirectUri == null)
                {
                    RedirectUri = new Uri(sampleRedirectUrl);                     // Sample app Redirect URI
                }
            }

            if (!string.IsNullOrWhiteSpace(userName) && !string.IsNullOrWhiteSpace(password))
            {
                ClientCredentials clientCredentials = new ClientCredentials();
                clientCredentials.UserName.UserName = userName;
                clientCredentials.UserName.Password = password;
                ClientCredentials = clientCredentials;
            }

            logEntry.Dispose();
        }