Beispiel #1
0
 /// <summary>
 /// 根据key获取终结点
 /// </summary>
 /// <param name="urlKey"></param>
 /// <returns></returns>
 private static EndpointAddress GetEndpointAddress(string urlKey)
 {
     if (!EndpointAddresses.ContainsKey(urlKey))
     {
         try
         {
             EndpointAddresses.Add(urlKey, new EndpointAddress(Configs[urlKey]));
         }
         catch { }
     }
     return(EndpointAddresses[urlKey]);
 }
        private InfoServiceProxy ConnectNetTcp()
        {
            InfoServiceProxy  infoServiceProxy;
            EndpointAddresses addresses = V2.IsPresent ? (EndpointAddresses) new V2EndpointAddresses() : new V3EndpointAddresses();

            if (Trusted.IsPresent)
            {
                var binding = new NetTcpBinding {
                    MaxReceivedMessageSize = int.MaxValue, MaxBufferSize = int.MaxValue
                };
                binding.Security.Mode = SecurityMode.Transport;
                binding.Security.Transport.ClientCredentialType = TcpClientCredentialType.Windows;

                var uri = new Uri(string.Format(addresses.activeDirectory, Hostname ?? "localhost"));

                infoServiceProxy = new InfoServiceProxy(uri, binding, new WindowsCredential());
            }
            else if (Certificate.IsPresent)
            {
                var binding = new NetTcpBinding(SecurityMode.Transport)
                {
                    MaxReceivedMessageSize = int.MaxValue, MaxBufferSize = int.MaxValue
                };
                binding.Security.Mode = SecurityMode.Transport;
                binding.Security.Transport.ClientCredentialType = TcpClientCredentialType.Certificate;
                binding.ReaderQuotas.MaxArrayLength             = int.MaxValue;
                binding.ReaderQuotas.MaxStringContentLength     = int.MaxValue;

                if (Streamed.IsPresent)
                {
                    binding.TransferMode       = TransferMode.Streamed;
                    binding.PortSharingEnabled = true;
                    binding.ReceiveTimeout     = new TimeSpan(15, 0, 0);
                    binding.SendTimeout        = new TimeSpan(15, 0, 0);
                }

                var address = (Streamed && !V2.IsPresent)
                                  ? ((V3EndpointAddresses)addresses).streamedCertificate
                                  : addresses.certificate;

                var uri = new Uri(string.Format(address, Hostname ?? "localhost"));

                ServiceCredentials credentials = new MyCertificateCredential("SolarWinds-Orion", StoreLocation.LocalMachine, StoreName.My);

                infoServiceProxy = new InfoServiceProxy(uri, binding, credentials);
            }
            else
            {
                var binding = new NetTcpBinding {
                    MaxReceivedMessageSize = int.MaxValue, MaxBufferSize = int.MaxValue
                };
                binding.Security.Mode = SecurityMode.TransportWithMessageCredential;
                binding.Security.Message.ClientCredentialType = MessageCredentialType.UserName;

                var uri = new Uri(string.Format(addresses.usernamePassword, Hostname ?? "localhost"));

                string username = string.Empty;
                string password = string.Empty;

                if (IsUserNamePresent)
                {
                    SecureString securePassword = StringToSecureString(Password);
                    Credential = new PSCredential(UserName, securePassword);
                }

                // the credential dialog adds a slash at the beginning, need to strip
                username = Credential.UserName.TrimStart('\\');
                password = SecureStringToString(Credential.Password);

                var credentials = new UsernameCredentials(username, password);

                infoServiceProxy = new InfoServiceProxy(uri, binding, credentials);
            }
            return(infoServiceProxy);
        }