private void lstUsers_SelectedIndexChanged(object sender, EventArgs e) { var user = new DataContext().XUsers.Single(u => u.Username == (string)lstUsers.SelectedItem); txtDescription.Text = user.Description; txtUsername.Text = user.Username; txtLastLogin.Text = user.LastLogin.ToString("dd-MM-yyyy HH:mm:ss"); btnDelete.Enabled = btnReset.Enabled = true; }
private async void btnSave_Click(object sender, EventArgs e) { if (txtDescription.Text == string.Empty && txtDescription.Text.Length < 50) { MessageBox.Show( "The description is a required property, it has to be between 1 and 50 characters long.", "Description invalid", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); txtDescription.Focus(); return; } if (txtUsername.Text == string.Empty && txtUsername.Text.Length < 50 && txtUsername.Text.Length > 4) { MessageBox.Show( "The username is a required property, it has to be between 4 and 50 characters long.", "Username invalid", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); txtUsername.Focus(); return; } if (txtPassword.Text == string.Empty && txtPassword.Text.Length < 50 && txtPassword.Text.Length > 5) { MessageBox.Show( "The password is a required property, it has to be between 5 and 50 characters long.", "Password invalid", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); txtPassword.Focus(); return; } using (var ctx = new DataContext()) { var hash = Convert.ToBase64String(new HMACSHA256(Encoding.Unicode.GetBytes(txtPassword.Text)) .ComputeHash(Encoding.Unicode.GetBytes(txtUsername.Text))); ctx.XUsers.Add(new XUser() { Description = txtDescription.Text, Username = txtUsername.Text.ToLower(), Password = hash, LastLogin = DateTime.Now }); await ctx.SaveChangesAsync(); } hasSaved = true; this.Close(); }
private async void btnDelete_Click(object sender, EventArgs e) { if(MessageBox.Show( "Are you sure you want to delete this user?", "Delete user", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes) { using(var ctx = new DataContext()) { ctx.XUsers.Remove(ctx.XUsers.Single(u => u.Username == (string)lstUsers.SelectedItem)); await ctx.SaveChangesAsync(); } frmUsers_Load(null, null); } }
private void frmUsers_Load(object sender, EventArgs e) { using(var ctx = new DataContext()) { lstUsers.Items.Clear(); lstUsers.Items.AddRange( ctx.XUsers.Select(u => u.Username).ToArray()); if (lstUsers.Items.Count > 0) lstUsers.SelectedIndex = 0; else { txtDescription.Text = txtUsername.Text = txtLastLogin.Text = string.Empty; btnDelete.Enabled = btnReset.Enabled = false; } } }
private async void btnReset_Click(object sender, EventArgs e) { if(txtPassword.Text != txtPasswordRepeat.Text) { MessageBox.Show( "The passwords specified don't match. Please try again.", "Passwords don't match", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); txtPassword.Clear(); txtPasswordRepeat.Clear(); txtPassword.Focus(); return; } if(txtPassword.Text.Length < 5 && txtPassword.Text.Length > 50) { MessageBox.Show( "The password size is invalid, please use a password between 5 and 50 characters long.", "Password size invalid", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); txtPassword.Clear(); txtPasswordRepeat.Clear(); txtPassword.Focus(); return; } using(var ctx = new DataContext()) { var user = ctx.XUsers.Single(u => u.Username == this.username); user.Password = Convert.ToBase64String(new HMACSHA256(Encoding.Unicode.GetBytes(txtPassword.Text)) .ComputeHash(Encoding.Unicode.GetBytes(this.username))); ctx.XUsers.Attach(user); ctx.Entry<XUser>(user).Property(p => p.Password).IsModified = true; await ctx.SaveChangesAsync(); } this.Close(); }
public async Task<Models.TokenResponse> Authenticate([FromBody] Models.TokenRequest request) { using (var ctx = new Data.DataContext()) { var user = ctx.XUsers.SingleOrDefault(u => u.Username == request.Username); if (user != null) { if (request.Signature == user.Password) { var token = ctx.XTokens.SingleOrDefault(t => t.Username == user.Username); if (token != null) { return new Models.TokenResponse() { Token = token.Token }; } else { token = new Data.XToken() { Username = user.Username, Token = (Guid.NewGuid().ToString() + Guid.NewGuid().ToString()).Replace("-", "").ToLower(), Created = DateTime.Now }; ctx.XTokens.Add(token); await ctx.SaveChangesAsync(); return new Models.TokenResponse() { Token = token.Token }; } } else { Program.cfgForm.Log(string.Format("Failed authorization attempt on {0}", Request.RequestUri.ToString())); throw new Exception("The authorization failed. Check the signature, it should consist out of the username signed with the pasword."); } } else { Program.cfgForm.Log(string.Format("Failed authorization attempt on {0}", Request.RequestUri.ToString())); throw new Exception("The authorization failed. This user does not exist."); } } }
private void CommitSetting(DataContext ctx, string key, string value) { var setting = ctx.Settings.SingleOrDefault(s => s.Key == key); if (setting != null) { setting.Value = value; ctx.Settings.Attach(setting); ctx.Entry<Setting>(setting).Property(p => p.Value).IsModified = true; } else ctx.Settings.Add(new Setting() { Key = key, Value = value }); }
private async void SaveSettings() { using (var ctx = new DataContext()) { CommitSetting(ctx, "txtFolder", txtFolder.Text); CommitSetting(ctx, "txtIP", txtIP.Text); CommitSetting(ctx, "txtPort", txtPort.Text); CommitSetting(ctx, "txtSteam", txtSteam.Text); CommitSetting(ctx, "txtHttpPort", txtHttpPort.Value.ToString()); CommitSetting(ctx, "SelectedHttp", rbLocalhost.Checked ? "local" : rbIPAddress.Checked ? "IP" : "DNS"); if (rbIPAddress.Checked) CommitSetting(ctx, "cbIP", (string)cbIPAddress.SelectedItem); if (rbDNS.Checked) CommitSetting(ctx, "txtDNS", txtDNS.Text); CommitSetting(ctx, "SelectedAuth", rbAnon.Checked ? "anonymous" : "XAuth"); await ctx.SaveChangesAsync(); } }
private void frmConfig_Load(object sender, System.EventArgs e) { cbIPAddress.Items.Clear(); NetworkInterface[] adapters = NetworkInterface.GetAllNetworkInterfaces(); foreach (NetworkInterface adapter in adapters) foreach (IPAddressInformation uniCast in adapter.GetIPProperties().UnicastAddresses) if (!IPAddress.IsLoopback(uniCast.Address) && uniCast.Address.AddressFamily != AddressFamily.InterNetworkV6) cbIPAddress.Items.Add(uniCast.Address.ToString()); cbIPAddress.SelectedIndex = 0; using (var ctx = new DataContext()) { var settings = ctx.Settings.ToList(); var setting = settings.SingleOrDefault(s => s.Key == "txtFolder"); if (setting != null) txtFolder.Text = setting.Value; setting = settings.SingleOrDefault(s => s.Key == "txtIP"); if (setting != null) txtIP.Text = setting.Value; setting = settings.SingleOrDefault(s => s.Key == "txtPort"); if (setting != null) txtPort.Text = setting.Value; setting = settings.SingleOrDefault(s => s.Key == "txtSteam"); if (setting != null) txtSteam.Text = setting.Value; setting = settings.SingleOrDefault(s => s.Key == "txtHttpPort"); if (setting != null) txtHttpPort.Value = System.Convert.ToInt32(setting.Value); setting = settings.SingleOrDefault(s => s.Key == "SelectedHttp"); if (setting != null) { if (setting.Value == "local") { rbLocalhost.Checked = true; rbIPAddress.Checked = rbDNS.Checked = false; } if (setting.Value == "IP") { rbIPAddress.Checked = true; rbLocalhost.Checked = rbDNS.Checked = false; setting = settings.SingleOrDefault(s => s.Key == "cbIP"); if (setting != null) for (var i = 0; i < cbIPAddress.Items.Count; i++) if ((string)cbIPAddress.Items[i] == setting.Value) { cbIPAddress.SelectedIndex = i; break; } } if (setting.Value == "DNS") { rbDNS.Checked = true; rbLocalhost.Checked = rbIPAddress.Checked = false; setting = settings.SingleOrDefault(s => s.Key == "txtDNS"); if (setting != null) txtDNS.Text = setting.Value; } } setting = settings.SingleOrDefault(s => s.Key == "SelectedAuth"); if (setting != null) { if (setting.Value == "anonymous") { rbAnon.Checked = true; rbXAuth.Checked = false; } if (setting.Value == "XAuth") { rbXAuth.Checked = true; rbAnon.Checked = false; } } } }