static void Main(string[] args) { // Serrifikad za konektovanje string srvCertCN = "wcfservice"; NetTcpBinding bindingAudit = new NetTcpBinding(); bindingAudit.Security.Transport.ClientCredentialType = TcpClientCredentialType.Certificate; string addressForAudit = "net.tcp://localhost:8888/RecieverAudit"; ServiceHost hostForAudit = new ServiceHost(typeof(WCFAudit)); hostForAudit.AddServiceEndpoint(typeof(IWCFAudit), bindingAudit, addressForAudit); hostForAudit.Credentials.ClientCertificate.Authentication.CertificateValidationMode = X509CertificateValidationMode.Custom; // posto je custom moramo samo da validiramo hostForAudit.Credentials.ClientCertificate.Authentication.CustomCertificateValidator = new AuditServiceCertValidator(); hostForAudit.Credentials.ClientCertificate.Authentication.RevocationMode = X509RevocationMode.NoCheck; // Uzima sa masine sertifikat za konektovanje hostForAudit.Credentials.ServiceCertificate.Certificate = AuditCertManager.GetCertificateFromStorage( StoreName.My, StoreLocation.LocalMachine, srvCertCN); try { hostForAudit.Open(); paramsForDoS = ReadParamsForDoS(); Console.WriteLine("WCFService is started.\nPress <enter> to stop ..."); Console.ReadLine(); } catch (Exception e) { Console.WriteLine("[ERROR] {0}", e.Message); Console.WriteLine("[StackTrace] {0}", e.StackTrace); } finally { hostForAudit.Close(); } }
public WCFServiceAudit(NetTcpBinding binding, EndpointAddress address) : base(binding, address) { /// audCertCN.SubjectName should be set to the audit's username. .NET WindowsIdentity class provides information about Windows user running the given process string audCertCN = "wcfclient"; bind = binding; addr = address; this.Credentials.ServiceCertificate.Authentication.CertificateValidationMode = System.ServiceModel.Security.X509CertificateValidationMode.Custom; this.Credentials.ServiceCertificate.Authentication.CustomCertificateValidator = new AuditCertValidator(); this.Credentials.ServiceCertificate.Authentication.RevocationMode = X509RevocationMode.NoCheck; /// Set appropriate client's certificate on the channel. Use CertManager class to obtain the certificate based on the "cltCertCN" this.Credentials.ClientCertificate.Certificate = AuditCertManager.GetCertificateFromStorage(StoreName.My, StoreLocation.LocalMachine, audCertCN); factory = this.CreateChannel(); }
static void Main(string[] args) { /// Define the expected service certificate. It is required to establish cmmunication using certificates. string srvCertCN = "wcfservice"; secretKey = SecretKey.GenerateKey(); NetTcpBinding bindingAudit = new NetTcpBinding(); bindingAudit.Security.Transport.ClientCredentialType = TcpClientCredentialType.Certificate; /// Use CertManager class to obtain the certificate based on the "srvCertCN" representing the expected service identity. X509Certificate2 srvCert = AuditCertManager.GetCertificateFromStorage(StoreName.TrustedPeople, StoreLocation.LocalMachine, srvCertCN); EndpointAddress addressForAudit = new EndpointAddress(new Uri("net.tcp://localhost:8888/RecieverAudit"), new X509CertificateEndpointIdentity(srvCert)); using (WCFServiceAudit proxy = new WCFServiceAudit(bindingAudit, addressForAudit)) { /// 1. Communication test Console.WriteLine("proxy " + proxy.ConnectS("TryConnect")); Console.WriteLine("Connection() established. Press <enter> to continue ..."); } //Windows autentifikacija NetTcpBinding binding = new NetTcpBinding(); binding.Security.Mode = SecurityMode.Transport; binding.Security.Transport.ClientCredentialType = TcpClientCredentialType.Windows; binding.Security.Transport.ProtectionLevel = System.Net.Security.ProtectionLevel.EncryptAndSign; string address = "net.tcp://localhost:9999/Receiver"; host = new ServiceHost(typeof(WCFService)); host.AddServiceEndpoint(typeof(IWCFContract), binding, address); host.Description.Behaviors.Remove(typeof(ServiceDebugBehavior)); host.Description.Behaviors.Add(new ServiceDebugBehavior() { IncludeExceptionDetailInFaults = true }); //autorizacija host.Authorization.ServiceAuthorizationManager = new CustomAuthorizationManager(); host.Authorization.PrincipalPermissionMode = PrincipalPermissionMode.Custom; List <IAuthorizationPolicy> policies = new List <IAuthorizationPolicy>(); policies.Add(new CustomAuthorizationPolicy()); host.Authorization.ExternalAuthorizationPolicies = policies.AsReadOnly(); try { host.Open(); Console.WriteLine("WCFService is started.\nPress <enter> to stop ..."); Console.ReadLine(); } catch (Exception e) { Console.WriteLine("[ERROR] {0}", e.Message); Console.WriteLine("[StackTrace] {0}", e.StackTrace); } finally { host.Close(); } }