public T CreateClient <T>(string bindingName, string wcfUrl, string userName, string password, string identityName) { //绑定 //WSHttpBinding binding = new WSHttpBinding(); //binding.Security.Message.ClientCredentialType = MessageCredentialType.UserName; WSHttpBinding binding = new WSHttpBinding(bindingName); //终结点“标识”属性 Uri myUri = new Uri(wcfUrl); DnsEndpointIdentity ei = new DnsEndpointIdentity(identityName); //"ParkingServer" EndpointAddress address = new EndpointAddress(myUri, ei); //创建通道工厂 ChannelFactory <T> factory = new ChannelFactory <T>(binding, address); factory.Credentials.UserName.UserName = userName; // "admin"; factory.Credentials.UserName.Password = password; // "123456"; //安全证书 ClientCredentials ccs = new ClientCredentials(); ccs.ServiceCertificate.Authentication.CertificateValidationMode = System.ServiceModel.Security.X509CertificateValidationMode.Custom; //自动义X509证书验证器 "Jurassic.AppCenter.Wcf.Client.CustomUserValidator,Jurassic.AppCenter.Wcf.Client"; CustomCertificateValidator cuv = new CustomCertificateValidator(); ccs.ServiceCertificate.Authentication.CustomCertificateValidator = cuv; ccs.UserName.UserName = userName; // "admin"; ccs.UserName.Password = password; // "123456"; //增加安全行为配置 factory.Endpoint.Behaviors.Clear(); factory.Endpoint.Behaviors.Add(ccs); //创建 T client = factory.CreateChannel(); return(client); }
/// <summary> /// 为ClientBase<TChannel>追加Authentication认证用户信息 /// </summary> /// <param name="userName"></param> /// <param name="password"></param> public void AppendClientCredentials(string userName, string password) { if (userName.IsEmpty() || password == null) { return; } //安全证书 ClientCredentials ccs = new ClientCredentials(); ccs.ServiceCertificate.Authentication.CertificateValidationMode = System.ServiceModel.Security.X509CertificateValidationMode.Custom; //自动义X509证书验证器,继承的父类是 System.IdentityModel.Selectors.X509CertificateValidator CustomCertificateValidator cuv = new CustomCertificateValidator(); ccs.ServiceCertificate.Authentication.CustomCertificateValidator = cuv; ccs.UserName.UserName = userName; ccs.UserName.Password = password; //增加安全行为配置 this.Endpoint.Behaviors.Clear(); this.Endpoint.Behaviors.Add(ccs); }