public bool SetTenantCredentials(ServerDto serverDto, string tenantName, TenantCredentialsDto credentials, Token token)
        {
            tenantName = Uri.EscapeDataString(tenantName);
            var url  = string.Format(ServiceConfigManager.CertificatePrivateKeyEndPoint, serverDto.Protocol, serverDto.ServerName, serverDto.Port, tenantName);
            var json = JsonConvert.Serialize(credentials);

            json = SerializationJsonHelper.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(string.IsNullOrEmpty(response));
        }
Beispiel #2
0
        private void btnCreateSignerIdentity_Click(object sender, EventArgs e)
        {
            if (ValidateInputs())
            {
                AuthTokenDto auth = null;
                if (txtTenantName.Enabled)
                {
                    var auths = SnapInContext.Instance.AuthTokenManager.GetAuthTokens(_serverDto);
                    auth = auths[0];
                }
                else
                {
                    auth = SnapInContext.Instance.AuthTokenManager.GetAuthToken(_serverDto, _tenantName);
                }

                ActionHelper.Execute(delegate
                {
                    // Get private key
                    var keyText = File.ReadAllText(txtKeyFile.Text);
                    keyText     = PrivateKeyHelper.ExtractBase64EncodedPayload(keyText);
                    EncrptionAlgorithm algo;
                    if (cbAlgo.SelectedIndex > -1)
                    {
                        Enum.TryParse(cbAlgo.SelectedItem.ToString(), false, out algo);
                    }
                    else
                    {
                        algo = EncrptionAlgorithm.RSA;
                    }
                    var privatekey = new PrivateKeyDto {
                        Encoded = keyText, Algorithm = algo
                    };

                    // Get all certificates
                    var certs = new List <CertificateDto>();
                    foreach (var certificate in GetCertificateChain())
                    {
                        var cert = new X509Certificate2();
                        cert.Import(certificate);
                        var certDto = new CertificateDto {
                            Encoded = cert.ExportToPem()
                        };
                        certs.Add(certDto);
                    }
                    var tenantCredentials = new TenantCredentialsDto {
                        Certificates = certs, PrivateKey = privatekey
                    };

                    if (txtTenantName.Enabled)
                    {
                        var tenantDto = new TenantDto()
                        {
                            Name = txtTenantName.Text, Credentials = tenantCredentials, Username = txtUsername.Text, Password = txtPassword.Text
                        };
                        _tenantDto = _service.Tenant.Create(_serverDto, tenantDto, auth.Token);
                    }
                    else
                    {
                        _service.Certificate.SetTenantCredentials(_serverDto, _tenantName, tenantCredentials, auth.Token);
                    }
                }, auth);
                this.DialogResult = DialogResult.OK;
            }
        }