public static async Task <bool> Check(ExternalResource externalResource) { var password = await KeyvaultHelper.Get(externalResource.Keyvault); ConnectionInfo ConnNfo = new ConnectionInfo(externalResource.Host, externalResource.Port, externalResource.Username, new AuthenticationMethod[] { // Pasword based Authentication new PasswordAuthenticationMethod(externalResource.Username, password) } ); bool isSuccess = false; using (var sshclient = new SshClient(ConnNfo)) { sshclient.Connect(); if (sshclient.IsConnected) { isSuccess = true; } sshclient.Disconnect(); } return(isSuccess); }
private static async Task <bool> oauth2Connect(ExternalResource rs) { string clientSecrect = await KeyvaultHelper.Get(rs.Keyvault); string accessTokenKey = (rs.CustAccessTokenField != null) ? rs.CustAccessTokenField : "access_token"; var pairs = new List <KeyValuePair <string, string> > { new KeyValuePair <string, string>("grant_type", "client_credentials"), new KeyValuePair <string, string>("client_id", rs.Username), new KeyValuePair <string, string> ("client_secret", clientSecrect), new KeyValuePair <string, string> ("scope", rs.Scope) }; var content = new FormUrlEncodedContent(pairs); string token = string.Empty; using (var client = new HttpClient()) { var response = await client.PostAsync(rs.IdentityEndpoint, content); var resContent = await response.Content.ReadAsStringAsync(); JObject jobj = JObject.Parse(resContent); token = (string)jobj[accessTokenKey]; } if (string.IsNullOrEmpty(token)) { throw new Exception("Cannot get a token"); } var uriBuilder = new UriBuilder($"{rs.Host}:{rs.Port}{rs.Path}"); var uri = uriBuilder.Uri; using (var client = new HttpClient()) { client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token); var result = await client.GetAsync(uri); if (result.StatusCode == System.Net.HttpStatusCode.OK) { return(true); } } return(false); }
public static async Task <bool> Check(ExternalResource externalResource) { var password = await KeyvaultHelper.Get(externalResource.Keyvault); FtpClient ftpClient = (externalResource.Type.Equals("ftps", StringComparison.OrdinalIgnoreCase) || externalResource.Port.Equals(990)) ? new FtpClient(externalResource.Host) { //FTPS explicitly or Port = 990 => automatically create FTPS Credentials = new NetworkCredential(externalResource.Username, password), Port = externalResource.Port, EnableThreadSafeDataConnections = true, //DataConnectionType = config.Ftp.DataConnectionType.Equals(Constants.FtpActiveMode, StringComparison.InvariantCultureIgnoreCase) ? FtpDataConnectionType.AutoActive : FtpDataConnectionType.AutoPassive, EncryptionMode = externalResource.Port.Equals(990) ? FtpEncryptionMode.Implicit : FtpEncryptionMode.Explicit } : new FtpClient(externalResource.Host) { Credentials = new NetworkCredential(externalResource.Username, password), Port = externalResource.Port, EnableThreadSafeDataConnections = true }; ftpClient.ValidateCertificate += (control, e) => { e.Accept = true; }; await ftpClient.ConnectAsync(); bool isSuccess = false; if (ftpClient.IsConnected) { isSuccess = true; await ftpClient.DisconnectAsync(); } return(isSuccess); }
private static async Task <bool> basicAuthConnect(ExternalResource rs) { var isSuccess = false; string passwd = await KeyvaultHelper.Get(rs.Keyvault); var uriBuilder = new UriBuilder($"{rs.Host}:{rs.Port}{rs.Path}"); var uri = uriBuilder.Uri; using (var client = new HttpClient()) { var byteArray = Encoding.ASCII.GetBytes($"{rs.Username}:{passwd}"); client.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Basic", Convert.ToBase64String(byteArray)); var result = await client.GetAsync(uri); if (result.StatusCode == System.Net.HttpStatusCode.OK) { return(true); } } return(isSuccess); }