-
Notifications
You must be signed in to change notification settings - Fork 0
/
DangNhap.cs
141 lines (127 loc) · 5.3 KB
/
DangNhap.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
using Newtonsoft.Json;
using System;
using System.Net.Http;
using System.Text;
using System.Windows.Forms;
using System.Security.Cryptography;
using MinigameOlympia.Models;
using MinigameOlympia;
namespace MinigameOlympia {
public partial class DangNhap : Form {
private RootForm _rootForm { get; set; }
private bool isOKUsername = false;
private bool isOKPassword = false;
private bool showPass = true;
private Player player;
public DangNhap(RootForm rootForm) {
InitializeComponent();
_rootForm = rootForm;
}
// Quay về Root Form
private void BackToRootForm(object sender, EventArgs args) {
Close();
_rootForm.Visible = true;
}
private async void tbUsername_TextChanged(object sender, EventArgs e) {
if (tbUsername.Text == "") {
lblAlertUsername.Text = "ⓘ Username không được để trống";
lblAlertUsername.Visible = true;
isOKUsername = false;
} else {
using (HttpClient client = new HttpClient()) {
try {
string url = "https://olympiawebservice.azurewebsites.net/api/Player/username?lookup=" + tbUsername.Text;
var response = await client.GetAsync(url);
if (!response.IsSuccessStatusCode) {
lblAlertUsername.Text = "ⓘ Username không tồn tại";
lblAlertUsername.Visible = true;
isOKUsername = false;
} else {
lblAlertUsername.Visible = false;
isOKUsername = true;
string jsonContent = await response.Content.ReadAsStringAsync();
player = JsonConvert.DeserializeObject<Player>(jsonContent);
}
} catch (Exception ex) {
MessageBox.Show(ex.Message);
}
}
}
}
private void PasswordTextbox(object sender, EventArgs e) {
tbPassword.MaxLength = 15;
if (showPass)
tbPassword.PasswordChar = '●';
else
tbPassword.PasswordChar = '\0';
if (tbPassword.Text == "") {
lblAlertPassword.Text = "ⓘ Mật khẩu không được để trống";
lblAlertPassword.Visible = true;
isOKPassword = false;
} else {
foreach (char c in tbPassword.Text) {
if (c == ' ') {
lblAlertPassword.Text = "ⓘ Mật khẩu không được chứa dấu cách";
lblAlertPassword.Visible = true;
isOKPassword = false;
break;
} else
lblAlertPassword.Visible = false;
}
isOKPassword = true;
}
}
private void ptbPassword_Click(object sender, EventArgs e) {
if (showPass) {
tbPassword.PasswordChar = '\0';
ptbPassword.Image = Properties.Resources.HidePass;
showPass = false;
} else {
tbPassword.PasswordChar = '●';
ptbPassword.Image = Properties.Resources.ShowPass;
showPass = true;
}
}
// Phương thức băm mật khẩu bằng SHA-256
private string HashPassword(string password) {
password += "group17";
using (SHA256 sha256 = SHA256.Create()) {
byte[] bytes = sha256.ComputeHash(Encoding.UTF8.GetBytes(password));
StringBuilder builder = new StringBuilder();
foreach (byte b in bytes) {
builder.Append(b.ToString("x2"));
}
return builder.ToString();
}
}
// Phương thức xác thực mật khẩu đã băm
private bool VerifyHashedPassword(string hashedPassword, string password) {
string hashOfInput = HashPassword(password);
return StringComparer.OrdinalIgnoreCase.Compare(hashedPassword, hashOfInput) == 0;
}
private void btnSignIn_Click(object sender, EventArgs e) {
if (isOKUsername && isOKPassword) {
if (VerifyHashedPassword(player.Password, tbPassword.Text)) {
//if (player.Password == tbPassword.Text) {
GiaoDienChinh mainScreen = new GiaoDienChinh();
mainScreen.username = tbUsername.Text;
mainScreen.Show();
Close();
} else {
lblAlertPassword.Text = "ⓘ Sai mật khẩu";
lblAlertPassword.Visible = true;
}
}
}
private void btnSignUp_Click(object sender, EventArgs e) {
DangKy signUp = new DangKy(_rootForm);
signUp.Show();
Close();
}
private void ForgetPassword(object sender, EventArgs e) {
Visible = false;
QuenMK forgetPassword = new QuenMK();
forgetPassword.Show();
}
}
}