static void Main(string[] args) { /// srvCertCN.SubjectName should be set to the service's username. .NET WindowsIdentity class provides information about Windows user running the given process string srvCertCN = Formatter.ParseName(WindowsIdentity.GetCurrent().Name); //sbesserver NetTcpBinding binding = new NetTcpBinding(); binding.Security.Transport.ClientCredentialType = TcpClientCredentialType.Certificate; string address = "net.tcp://localhost:9999/Receiver"; ServiceHost host = new ServiceHost(typeof(WCFService)); host.AddServiceEndpoint(typeof(IWCFService), binding, address); ///Custom validation mode enables creation of a custom validator - CustomCertificateValidator host.Credentials.ClientCertificate.Authentication.CertificateValidationMode = X509CertificateValidationMode.Custom; host.Credentials.ClientCertificate.Authentication.CustomCertificateValidator = new ServiceCertValidator(); ///If CA doesn't have a CRL associated, WCF blocks every client because it cannot be validated host.Credentials.ClientCertificate.Authentication.RevocationMode = X509RevocationMode.NoCheck; ///Set appropriate service's certificate on the host. Use CertManager class to obtain the certificate based on the "srvCertCN" host.Credentials.ServiceCertificate.Certificate = CertManager.GetCertificateFromStorage(StoreName.My, StoreLocation.LocalMachine, srvCertCN); ///Set custom policy host.Authorization.PrincipalPermissionMode = PrincipalPermissionMode.Custom; List <IAuthorizationPolicy> policies = new List <IAuthorizationPolicy>(); policies.Add(new CustomAuthorizationPolicy()); host.Authorization.ExternalAuthorizationPolicies = policies.AsReadOnly(); ///AuditBehaviour ServiceSecurityAuditBehavior newAudit = new ServiceSecurityAuditBehavior(); newAudit.AuditLogLocation = AuditLogLocation.Application; newAudit.ServiceAuthorizationAuditLevel = AuditLevel.SuccessOrFailure; host.Description.Behaviors.Remove <ServiceSecurityAuditBehavior>(); host.Description.Behaviors.Add(newAudit); Database.performances = Database.ReadPerformances(); Database.reservations = Database.ReadReservations(); Database.users = Database.ReadUsers(); Database.ReadDiscount(); 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); Console.ReadLine(); } finally { host.Close(); } }
public static List <User> ReadUsers() { List <User> temp = new List <User>(); try { string fullPath = Path.GetFullPath(path + "users.txt"); FileStream stream = new FileStream(fullPath, FileMode.Open); StreamReader reader = new StreamReader(stream); string line = ""; while ((line = reader.ReadLine()) != null) { string[] tokens = line.Split(';'); List <Reservation> userReservations = new List <Reservation>(); User user = null; int count = tokens[3].Count(x => x == ','); if (count != 0) { string[] idRes = tokens[3].Split(','); for (int i = 0; i < count; i++) { foreach (Reservation res in reservations) { if (int.Parse(idRes[i]) == res.Id) { userReservations.Add(res); } } } user = new User(tokens[0], tokens[1], double.Parse(tokens[2]), userReservations); } else { user = new User(tokens[0], tokens[1]); } temp.Add(user); } reader.Close(); stream.Close(); try { Audit.ReadFromFileSuccess(Formatter.ParseName(WindowsIdentity.GetCurrent().Name), "users.txt"); } catch (Exception e) { Console.WriteLine(e.Message); } } catch (Exception e) { try { Audit.ReadFromFileFailed(Formatter.ParseName(WindowsIdentity.GetCurrent().Name), "users.txt", e.Message); } catch (Exception eror) { Console.WriteLine(eror.Message); } Console.WriteLine($"Error while trying to read users : {e.Message}"); } return(temp); }