void CreateServiceHost()
        {
            _logger.LogTextMessage("Creating host...");
            try
            {
                _host?.Close();

                string addressTcp    = "net.tcp://" + _serverIp + ":" + _tcpPort + "/D2SecService";
                Uri[]  baseAddresses = { new Uri(addressTcp) };

                _userPasswordServiceInstance = new D2User();
                _host = new ServiceHost(_userPasswordServiceInstance, baseAddresses);

                ServiceMetadataBehavior mexBehavior = new ServiceMetadataBehavior();
                _host.Description.Behaviors.Add(mexBehavior);

                NetTcpBinding tcpBinding = new NetTcpBinding()
                {
                    ReceiveTimeout = TimeSpan.MaxValue
                };
                tcpBinding.ReliableSession.Enabled = true;
                tcpBinding.Security.Mode           = SecurityMode.TransportWithMessageCredential;
                tcpBinding.Security.Message.ClientCredentialType = MessageCredentialType.UserName;

                //tcpBinding.ReliableSession.InactivityTimeout = TimeSpan.FromSeconds(10);

                _host.Credentials.UserNameAuthentication.UserNamePasswordValidationMode = UserNamePasswordValidationMode.Custom;
                string certPath = new D2PathHelper().GetD2Path() + "Server.pfx";
                _logger.LogTextMessage($"Reading certificate: {certPath} ...");
                _host.Credentials.ServiceCertificate.Certificate = new X509Certificate2(certPath);
                _host.Credentials.UserNameAuthentication.CustomUserNamePasswordValidator = new ClientAuthenticator();
                _logger.LogTextMessage($"Reading certificate: {certPath} - OK");
                _host.AddServiceEndpoint(typeof(ID2User), tcpBinding, addressTcp);
                _host.AddServiceEndpoint(typeof(IMetadataExchange), MetadataExchangeBindings.CreateMexTcpBinding(), "mex");
                _host.Open();
            }
            catch (Exception exception)
            {
                _logger.LogTextMessage($"Error while creating host:\n{exception.Message}\nStack:\n{exception.StackTrace}");
            }
            _logger.LogTextMessage("Creating host - OK");
        }
Beispiel #2
0
        public byte[] GetD2UserPassword()
        {
            int currentRetry = 5;

            do
            {
                try
                {
                    var pathHelper = new D2PathHelper();
                    return(File.ReadAllBytes(pathHelper.GetD2Path() + "Lgn"
                                             + new D2IniFileHelper(pathHelper.GetD2IniPath()).GetD2IniKeyValue("Servers", "ServerIP", Environment.MachineName)
                                             + ".dat"));
                }
                catch (Exception)
                {
                    currentRetry--;
                    Thread.Sleep(20);
                }
            } while (currentRetry > 0);
            return(null);
        }