public void OnClickOkButton (object sender, EventArgs e)
		{
			if (string.IsNullOrEmpty (TXTTenantName.StringValue)) {
				UIErrorHelper.ShowAlert ("Please enter valid tenant name", "Alert");
			} else {
				TenantDto = new TenantDto{ Name = TXTTenantName.StringValue };
				this.Close ();
				NSApplication.SharedApplication.StopModalWithCode (1);
			}
		}
		private bool AddNewCertificate(TenantDto tenantDto)
		{
			var result = false;
			ActionHelper.Execute (delegate() {
				var serverDto = GetServerDto ();
				var auth = SnapInContext.Instance.AuthTokenManager.GetAuthToken (serverDto.ServerName);
				result = SnapInContext.Instance.ServiceGateway.Certificate.SetTenantCredentials (auth.ServerDto, tenantDto.Name, tenantDto.Credentials, auth.Token);
			});
			return result;
		}
        public TenantDto Create(ServerDto server, TenantDto tenant, Token token)
        {
            tenant.Username = tenant.Username + "@" + tenant.Name;
            var url = string.Format(ServiceConfigManager.TenantsEndPoint, server.Protocol, server.ServerName, server.Port);
            var json = JsonConvert.Serialize(tenant);
			json = Cleanup (json);
            ServicePointManager.ServerCertificateValidationCallback = delegate { return true; };
            var requestConfig = new RequestSettings
            {
                Method = HttpMethod.Post,
            };
            var headers = ServiceHelper.AddHeaders(ServiceConfigManager.JsonContentType);
            json = "access_token=" + token.AccessToken + "&token_type=" + token.TokenType.ToString().ToLower() + "&" + json;
            var response = _webRequestManager.GetResponse(url, requestConfig, headers, null, json);
            return JsonConvert.Deserialize<TenantDto>(response);
        }
		public override void AwakeFromNib ()
		{
			base.AwakeFromNib ();
			_certs = new List<string> ();
			TxtTenantName.Enabled = !UpdateCredentials;
			txtUsername.Enabled = !UpdateCredentials;
			TxtPassword.Enabled = !UpdateCredentials;
			if (UpdateCredentials)
				TxtTenantName.StringValue = TenantDto.Name;
			else
				TenantDto = new TenantDto ();
			TenantDto.Credentials = new TenantCredentialsDto(){Certificates = new List<CertificateDto>()};
			
			BtnAddCertificate.Activated +=	(object sender, EventArgs e) => {
				var openPanel = new NSOpenPanel();
				openPanel.ReleasedWhenClosed = true;
				openPanel.Prompt = "Select file";

				var result = openPanel.RunModal();
				if (result == 1)
				{
					var filePath = openPanel.Url.AbsoluteString.Replace("file://",string.Empty);
					var cert = new X509Certificate2 ();
					ActionHelper.Execute (delegate() {
						cert.Import (filePath);
						_certs.Add(filePath);
						var certfificateDto = new CertificateDto { Encoded = cert.ToPem(), };
						TenantDto.Credentials.Certificates.Add(certfificateDto);
						ReloadCertificates();
					});
				}
			};

			BtnRemoveCertificate.Activated += (object sender, EventArgs e) => {
				if (CertificateChainTableView.SelectedRows.Count > 0) {
					foreach (var row in CertificateChainTableView.SelectedRows) {
						_certs.RemoveAt ((int)row);
						TenantDto.Credentials.Certificates.RemoveAt ((int)row);
					}
					ReloadCertificates();
				}
			};

			BtnBrowsePrivateKey.Activated += (object sender, EventArgs e) => {
				var openPanel = new NSOpenPanel();
				openPanel.ReleasedWhenClosed = true;
				openPanel.Prompt = "Select file";
				var result = openPanel.RunModal();
				if (result == 1)
				{
					var filePath = openPanel.Url.AbsoluteString.Replace("file://",string.Empty);

					ActionHelper.Execute (delegate() {
							var text = System.IO.File.ReadAllText(filePath);
							var privateKey = PrivateKeyHelper.ExtractBase64EncodedPayload(text);
							TxtPrivateKeyPath.StringValue = filePath;
							TenantDto.Credentials.PrivateKey = new PrivateKeyDto(){ Algorithm = EncrptionAlgorithm.RSA, Encoded = privateKey };
					});
				}
			};

			BtnClose.Activated += (object sender, EventArgs e) => {
				TenantDto = null;
				this.Close ();
				NSApplication.SharedApplication.StopModalWithCode (0);
			};
			this.BtnSave.Activated += (object sender, EventArgs e) => {
				if (!UpdateCredentials && string.IsNullOrEmpty (TxtTenantName.StringValue)) {
					UIErrorHelper.ShowAlert ("Please enter valid tenant name", "Alert");
				} else if (!UpdateCredentials && string.IsNullOrEmpty (txtUsername.StringValue)) {
					UIErrorHelper.ShowAlert ("Please enter valid username", "Alert");
				} else if (!UpdateCredentials && string.IsNullOrEmpty (TxtPassword.StringValue)) {
					UIErrorHelper.ShowAlert ("Please enter valid password", "Alert");
				} else if (string.IsNullOrEmpty (TxtPrivateKeyPath.StringValue)) {
					UIErrorHelper.ShowAlert ("Please enter valid private key", "Alert");
				} else if (_certs.Count < 2) {
					UIErrorHelper.ShowAlert ("Please enter atleast 2 valid Certificates", "Alert");
				} else {
					TenantDto.Name = TxtTenantName.StringValue;
					TenantDto.Username = txtUsername.StringValue;
					TenantDto.Password = TxtPassword.StringValue;
					this.Close ();
					NSApplication.SharedApplication.StopModalWithCode (1);
				}
			};
		}
		private TenantDto AddTenant(TenantDto tenantDto)
		{
			var succcess = false;
			ActionHelper.Execute (delegate() {
				var auth = SnapInContext.Instance.AuthTokenManager.GetAuthToken (DisplayName);
				tenantDto = SnapInContext.Instance.ServiceGateway.Tenant.Create (auth.ServerDto, tenantDto, auth.Token);
				succcess = true;
			});
			return succcess ? tenantDto: null;
		}
		private void AddTenantNode (AuthTokenDto tokenDto, TenantDto tenant)
		{
			var node = new TenantNode (tokenDto.ServerDto, tenant.Name) {
				DisplayName = tenant.Name,
				Tag = tenant.Guid,
				Parent = this
			};
			node.Refresh (this, EventArgs.Empty);
			AddTenantNode (node);
		}