private async void ConnectButton_Click(object sender, RoutedEventArgs e) { string server = TextBoxHost.Text.Trim(); if (server.NullEmpty()) { DialogResult = false; return; } NetworkClient.Host = server; NetworkClient.UserName = TextBoxUserName.Text.Trim(); string password = TextBoxPassword.Password; if (!password.NullEmpty()) { if (!CryptoHashing.IsEncrypted(password)) { password = CryptoHashing.Encrypt(password); } else { password = users.Item.Password; } } NetworkClient.Password = password; NetworkClient.Port = TextBoxPort.Text.Int(21); _setOptions(); DialogResult = true; await save(); }
internal override async Task <bool> ConnectAsync(bool reconnect = false, bool tryReconnect = true) { if (UserName.NullEmpty()) { UserName = "******"; } if (Password.NullEmpty()) { Password = CryptoHashing.Encrypt("*****@*****.**"); } if (Port == 0) { Port = 21; } Connecting(); if (reconnect) { closeConnection(); closeDataConnection(); InfoMessage("Connection Dropped! Reconnecting...", MessageType.Info); } else { InfoMessage("Connecting to " + Host + ":" + Port.ToString(), MessageType.Info); } secureConnection = (Encryption.Type == EncryptionType.ImplicitType); ControlSocket = await createStreamAsync(Host, Port); if ((ControlSocket == null) && tryReconnect) { for (int i = 1; i < RETRY_TIMES; i++) { if (IsCanceled) { return(false); } InfoMessage("Reconnecting after " + (5 * i) + " seconds...{" + (RETRY_TIMES - i) + "}", MessageType.Info); await Task.Delay(i * 5000); ControlSocket = await createStreamAsync(Host, Port); if (ControlSocket != null) { break; } } } if (ControlSocket != null) { ControlStream = new NetworkStream(ControlSocket); if (secureConnection) { if (Encryption.Protocol == FTPSslProtocol.TLS) { ControlStream = await SslClient.ConnectAsync(ControlStream, Host, SslProtocols.Tls12); } else { ControlStream = await SslClient.ConnectAsync(ControlStream, Host, SslProtocols.Ssl3); } } IsConnected = (ControlStream != null); } IsConnected = ((ControlStream != null) && (await getResponseAsync() == 220)); if (IsConnected && (Encryption.Type == EncryptionType.ExplicitType)) { IsConnected = false; string protocol = "TLS"; if (Encryption.Protocol == FTPSslProtocol.SSL) { protocol = "SSL"; } if (await _commandAuthAsync(protocol)) { if (Encryption.Protocol == FTPSslProtocol.TLS) { ControlStream = await SslClient.ConnectAsync(ControlStream, Host, SslProtocols.Tls12); } else { ControlStream = await SslClient.ConnectAsync(ControlStream, Host, SslProtocols.Ssl3); } secureConnection = IsConnected = (ControlStream != null); } } if (!IsConnected || !(await _logInAsync())) { await _failedToConnectAsync(); return(false); } if (secureConnection) { InfoMessage("Secure Connection Established.", MessageType.Info); } else { InfoMessage("Connection Established.", MessageType.Info); } await _afterConnectAsync(); Connected(); return(true); }
private async Task <bool> _commandPassAsync(string password) { return(await executeCommandAsync("PASS", CryptoHashing.Decrypt(password)) == 230); }
public override async Task GrantResourceOwnerCredentials(OAuthGrantResourceOwnerCredentialsContext context) { //context.OwinContext.Response.Headers.Add("Access-Control-Allow-Origin", new[] { "*" }); string errmsg = ""; string sName = "", sUserId = "0"; try { int SocietyId = 0; var form = await context.Request.ReadFormAsync(); if (form["SocietyId"] != null) { SocietyId = Convert.ToInt32(form["SocietyId"]); } //Convert Password to PasswordHash string PasswordHash = CryptoHashing.Encrypt(context.Password); DbRepository db = new DbRepository(); Users u = db.LoginUser(context.UserName, PasswordHash, out errmsg); if (errmsg != "success") { context.SetError("invalid_grant", "Error-" + errmsg); return; } else { if (u == null) { context.SetError("invalid_grant", "Invalid username or password !"); return; } else { sUserId = u.Id.ToString(); sName = u.Name; var identity = new ClaimsIdentity(context.Options.AuthenticationType); identity.AddClaim(new Claim("MemberName", sName)); identity.AddClaim(new Claim("UserId", sUserId)); //roles example var rolesTechnicalNamesUser = new List <string>(); var principal = new GenericPrincipal(identity, rolesTechnicalNamesUser.ToArray()); Thread.CurrentPrincipal = principal; var props = new AuthenticationProperties(new Dictionary <string, string> { { "userid", sUserId }, { "name", sName } }); var ticket = new AuthenticationTicket(identity, props); context.Validated(ticket); } } } catch (Exception ex) { if (ex.Message.Contains("See the inner exception for details")) { errmsg = ex.InnerException.Message; } else { errmsg = ex.Message; } context.SetError("error_occured", errmsg); } }