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 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 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(); } }