-
Notifications
You must be signed in to change notification settings - Fork 0
/
FormAddUser.cs
86 lines (72 loc) · 2.56 KB
/
FormAddUser.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
/***************************************************************************************
* Copyright (C) 2021 Fran Vojković, Martina Gaćina, Matea Čotić, Mirna Keser *
* *
* This file is part of the RP3_Projekt project. *
* *
***************************************************************************************/
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Text.RegularExpressions;
using System.Data.SqlClient;
namespace CaffeBar
{
/// <summary>
/// Display add user form.
/// </summary>
public partial class FormAddUser : Form
{
public FormAddUser()
{
InitializeComponent();
}
private void FormAddUser_Load(object sender, EventArgs e)
{ }
private void buttonSubmit_Click(object sender, EventArgs e)
{
//MessageBox.Show("e");
// validate
bool flag = true;
SqlConnection connection = DB.getConnection();
SqlCommand command = new SqlCommand("INSERT INTO [User](Username, Password) VALUES(@Username, @Password)", connection);
command.Parameters.AddWithValue("@Username", textBoxUsername.Text.ToString());
command.Parameters.AddWithValue("@Password", Hash(textBoxPassword.Text.ToString()));
try
{
command.ExecuteNonQuery();
}
catch (Exception ex)
{
MessageBox.Show("ERROR: Database error! " + ex.Message);
flag = false;
}
finally
{
DB.closeConnection();
}
if (flag)
{
MessageBox.Show("User added!");
this.Close();
}
}
private string Hash(string password)
{
var bytes = new UTF8Encoding().GetBytes(password);
byte[] hashBytes;
using (var algorithm = new System.Security.Cryptography.SHA512Managed())
{
hashBytes = algorithm.ComputeHash(bytes);
}
return Convert.ToBase64String(hashBytes);
}
#region Input validation
#endregion
}
}