-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDataAccess.cs
55 lines (48 loc) · 1.57 KB
/
DataAccess.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
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
namespace CodeQLAlertTrigger
{
public class DataAccess
{
private Dictionary<string, string> _users = new Dictionary<string, string>();
private const string SqlConnectionString = "Server=localhost;Database=SecurityDb;User Id=sa;Password=MyP@ssw0rd!;";
public DataAccess()
{
}
public bool IsValidUser(string username, string password)
{
PopulateUsers();
// Should access the database but we wont
if (!_users.ContainsKey(username))
{
return false;
}
return _users[username] == password;
}
private void PopulateUsers()
{
try
{
using (var conn = new SqlConnection(SqlConnectionString))
using (var cmd = conn.CreateCommand())
{
conn.Open();
cmd.CommandText = "select username,password from users";
var rdr = cmd.ExecuteReader();
while (rdr.Read())
{
_users.Add(rdr.GetString(0), rdr.GetString(1));
}
}
} catch
{
// Accept DB failure, populate pre-filled
_users.Add("user1", "password1");
_users.Add("user2", "password2");
_users.Add("user3", "password3");
_users.Add("user4", "password4");
}
}
}
}