private byte[] GetCheckSum()
        {
            try
            {
                MyPrincipal mp = Thread.CurrentPrincipal as MyPrincipal;

                if (mp.IsInRole("ModifyBlackList"))
                {
                    string fileName = "../../../blacklist.xml";

                    using (var md5 = MD5.Create())
                    {
                        using (var stream = File.OpenRead(fileName))
                        {
                            return(md5.ComputeHash(stream));
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                throw new SecurityException("Authorization failed" + ex.Message);
            }
            return(null);
        }
        protected override bool CheckAccessCore(OperationContext operationContext)
        {
            IPrincipal  principal   = operationContext.ServiceSecurityContext.AuthorizationContext.Properties["Principal"] as IPrincipal;
            MyPrincipal myPrincipal = principal as MyPrincipal;

            if (myPrincipal.IsInRole("ExchangeSessionKey"))
            {
                return(true);
            }
            return(false);
        }
        public void StartService(byte[] array, int size, byte[] iVector)
        {
            try
            {
                MyPrincipal mp = Thread.CurrentPrincipal as MyPrincipal;

                if (mp.IsInRole("RunService"))
                {
                    IFormatter form = new BinaryFormatter();
                    Aes        alg  = Aes.Create();
                    alg.IV = iVector;
                    byte[] aesKey = new byte[32];
                    Array.Copy(Encoding.ASCII.GetBytes(Key), 0, aesKey, 0, 32);
                    alg.Key  = aesKey;
                    alg.Mode = CipherMode.CBC;
                    MemoryStream bencr = new MemoryStream(array);

                    CryptoStream cr = new CryptoStream(bencr, alg.CreateDecryptor(), CryptoStreamMode.Read);

                    byte[] buf = new byte[size];
                    cr.Read(buf, 0, size);

                    MemoryStream unenc = new MemoryStream(buf);
                    unenc.Position = 0;

                    Console.WriteLine("\nRecived data:");
                    ClientInfo resd = (ClientInfo)form.Deserialize(unenc);
                    Console.WriteLine("Port: " + resd.port);
                    Console.WriteLine("Protocol: " + resd.protocol);
                    Console.WriteLine("Machine name: " + resd.machineName);

                    if (CheckBlackList(resd, mp.Groups))
                    {
                        throw new SecurityException("Start service failed, requested parameters are in blacklist.");
                    }
                    else
                    {
                        Process p = new Process();
                        p.StartInfo.FileName  = @"..\..\..\ServiceManagement.Service\bin\Debug\ServiceManagement.Service.exe";
                        p.StartInfo.Arguments = resd.port + " " + resd.protocol;
                        p.Start();
                    }
                }
                else
                {
                    throw new SecurityException("Authorization failed, user can't start services.");
                }
            }
            catch (Exception ex)
            {
                throw new SecurityException("Authorization failed" + ex.Message);
            }
        }
        protected virtual IPrincipal GetPrincipal(IIdentity identity)
        {
            lock (locker)
            {
                IPrincipal      principal       = null;
                WindowsIdentity windowsIdentity = identity as WindowsIdentity;

                if (windowsIdentity != null)
                {
                    principal = new MyPrincipal(windowsIdentity);
                }

                return(principal);
            }
        }
        public void AddProtocol(string protocol, string group)
        {
            try
            {
                MyPrincipal mp = Thread.CurrentPrincipal as MyPrincipal;

                if (mp.IsInRole("ModifyBlackList"))
                {
                    XmlManager.Instance.Write(new BlackClass("-1", protocol, group));

                    Console.WriteLine("AddProtocol() executed!");

                    _checksum = GetCheckSum();
                }
            }
            catch (Exception ex)
            {
                throw new SecurityException("Authorization failed" + ex.Message);
            }
        }
        public string Connect()
        {
            try
            {
                MyPrincipal mp = Thread.CurrentPrincipal as MyPrincipal;

                if (mp.IsInRole("ExchangeSessionKey"))
                {
                    Console.WriteLine("Connect() executed!");
                    Key       = RandomString(32);
                    _checksum = GetCheckSum();
                    return(Key);
                }
                return("");
            }
            catch (Exception ex)
            {
                throw new SecurityException("Authorization failed" + ex.Message);
            }
        }