-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDataAccess.cs
More file actions
66 lines (58 loc) · 2.01 KB
/
DataAccess.cs
File metadata and controls
66 lines (58 loc) · 2.01 KB
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
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 = "";
private const string SqlConnectionString2 = "Server=localhost;Database=SecurityDb;User Id=sa;Password=MyP@ssw0rd2!;";
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;
}
string GeneratePassword()
{
// https://codeql.github.com/codeql-query-help/csharp/cs-insecure-randomness/
// BAD: Password is generated using a cryptographically insecure RNG
Random gen = new Random();
string password = "mypassword" + gen.Next();
return password;
}
private void PopulateUsers()
{
try
{
using (var conn = new SqlConnection())
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");
_users.Add("user5", "password5");
}
}
}
}