/
MyDbContext.cs
148 lines (125 loc) · 5.66 KB
/
MyDbContext.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
142
143
144
145
146
147
148
using System;
using System.Data;
using EFCore.Core.Models;
using EFCore.Core.Utils;
using EFCore.Dal.Models;
using EFCore.Dal.Utils;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Options;
namespace EFCore.Dal
{
public class MyDbContext : DbContext
{
private readonly DbContextOptions<MyDbContext> options = null;
private readonly AppSettings appSettings = null;
public MyDbContext(
DbContextOptions<MyDbContext> options) : base(options)
{
this.options = options;
}
public MyDbContext(
DbContextOptions<MyDbContext> options,
IOptions<AppSettings> configuration) : base(options)
{
this.options = options;
this.appSettings = configuration.Value;
}
public DbSet<User> Users { get; set; }
#region Functions
[DbFunction(Name = "my_hash", Schema = "public")]
public static string DbHash(string t) => throw new NotImplementedException();
[DbFunction(Name = "my_hash_match", Schema = "public")]
public static bool DbHashMatch(string t, string hashed) => throw new NotImplementedException();
[DbFunction(Name = "my_sym_encrypt", Schema = "public")]
public static byte[] DbSymEncrypt(string t) => throw new NotImplementedException();
[DbFunction(Name = "my_sym_decrypt", Schema = "public")]
public static string DbSymDecrypt(byte[] t) => throw new NotImplementedException();
#endregion
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
// Hash Password
modelBuilder.Entity<User>().Property(p => p.Password).HasConversion(
val => this.HashIt(val),
val => val);
// Encrypt/Decrypt Phone
modelBuilder.Entity<User>().Property(p => p.Phone).HasConversion(
val => this.EncryptMe(val),
val => this.DecryptMe(val));
// Encrypt/Decrypt CardNo
// Notice that we cannot use the static method like "DbSymEncrypt" here
modelBuilder.Entity<User>().Property(p => p.CardNo).HasConversion(
val => this.EncryptMe(val),
val => this.DecryptMe(val));
// This code wont work!
////modelBuilder.Entity<Receipt>().Property(p => p.CardNo).HasConversion(
//// val => MiniTisContext.DbSymEncrypt(val),
//// val => MiniTisContext.DbSymDecrypt(val));
// Seed
modelBuilder.Seed();
}
private string HashIt(string text)
{
// You can use DbContextFactory to create the new DbContext as well
// var dbContext = DbContextFactory.Create(ConstFactory.DbConetextName);
using (var dbContext = new MyDbContext(this.getOptionBuilder().Options))
using (var command = dbContext.Database.GetDbConnection().CreateCommand())
{
command.CommandType = CommandType.StoredProcedure;
command.CommandText = "my_hash";
command.Parameters.Add(new Npgsql.NpgsqlParameter("t", NpgsqlTypes.NpgsqlDbType.Text)
{ Value = text });
if (command.Connection.State == ConnectionState.Closed)
{
command.Connection.Open();
}
var hashed = (string)command.ExecuteScalar();
return hashed;
}
}
private byte[] EncryptMe(string text)
{
// You can use DbContextFactory to create the new DbContext as well
// var dbContext = DbContextFactory.Create(ConstFactory.DbConetextName);
using (var dbContext = new MyDbContext(this.getOptionBuilder().Options))
using (var command = dbContext.Database.GetDbConnection().CreateCommand())
{
command.CommandType = CommandType.StoredProcedure;
command.CommandText = "my_sym_encrypt";
command.Parameters.Add(
new Npgsql.NpgsqlParameter("t", NpgsqlTypes.NpgsqlDbType.Text) { Value = text });
if (command.Connection.State == ConnectionState.Closed)
{
command.Connection.Open();
}
var encrypted = (byte[])command.ExecuteScalar();
return encrypted;
}
}
private string DecryptMe(byte[] cipher)
{
// You can use DbContextFactory to create the new DbContext as well
// var dbContext = DbContextFactory.Create(ConstFactory.DbConetextName);
using (var dbContext = new MyDbContext(this.getOptionBuilder().Options))
using (var command = dbContext.Database.GetDbConnection().CreateCommand())
{
command.CommandType = CommandType.StoredProcedure;
command.CommandText = "my_sym_decrypt";
command.Parameters.Add(
new Npgsql.NpgsqlParameter("t", NpgsqlTypes.NpgsqlDbType.Bytea) { Value = cipher });
if (command.Connection.State == ConnectionState.Closed)
{
command.Connection.Open();
}
var decrypted = (string)command.ExecuteScalar();
return decrypted;
}
}
private DbContextOptionsBuilder<MyDbContext> getOptionBuilder()
{
var connectionString = this.appSettings.ConnectionStrings.DB;
var optionsBuilder = new DbContextOptionsBuilder<MyDbContext>();
optionsBuilder.UseNpgsql(connectionString);
return optionsBuilder;
}
}
}