FAQ/Questions/Authenticating-via-hash.md
... ...
@@ -0,0 +1,68 @@
1
+#Authenticating Via Hash
2
+
3
+[[_TOC_]]
4
+
5
+##Question
6
+
7
+
8
+
9
+##Answer
10
+
11
+You can use the following code sample in order to create basic authentication against a hash value sent via the query string parameter "UserHash".
12
+
13
+###C♯ Example
14
+
15
+```csharp
16
+void Application_PreRequestHandlerExecute(object sender, EventArgs e) {
17
+ //check for query string values
18
+ try {
19
+ string username = HttpContext.Current.Session["UserName"].ToString(); //greg
20
+ string hash = HttpContext.Current.Request.QueryString["UserHash"]; //"EE5BD620F9418FC5CDEE8BC28DD7659E";
21
+ string redir = HttpContext.Current.Request.QueryString["Redirect"]; //null;
22
+ if (username == null || hash == null) {
23
+ //redirect to login
24
+ Response.Redirect("Login.aspx");
25
+ }
26
+ if (CheckMd5Sum(username, hash)) {
27
+ //set global database connection string
28
+ SetUserSession(username, "server=(local);database=Northwind;Trusted_Connection=True", redir);
29
+ }
30
+ else {
31
+ //redirect to login
32
+ Response.Redirect("Login.aspx");
33
+ }
34
+ }
35
+ catch {
36
+ //redirect to login
37
+ Response.Redirect("Login.aspx");
38
+ }
39
+ }
40
+
41
+ void SetUserSession(string sUser, string sConnection, string sRedir) {
42
+ //query database and set user information and populate variables
43
+ Izenda.AdHoc.AdHocSettings.SqlServerConnectionString = sConnection;
44
+ Izenda.AdHoc.AdHocSettings.CurrentUserIsAdmin = (bool)(HttpContext.Current.Session["IsAdmin"]); //false
45
+ Izenda.AdHoc.AdHocSettings.CurrentUserName = sUser;
46
+
47
+ if (sRedir != null) {
48
+ HttpContext.Current.Response.Redirect(sRedir);
49
+ }
50
+ }
51
+
52
+ bool CheckMd5Sum(string key, string hash) {
53
+ string sFullKey = key + "Izend$$";
54
+ Encoder enc = System.Text.Encoding.Unicode.GetEncoder();
55
+
56
+ byte[] unicodeText = new byte[sFullKey.Length * 2];
57
+ enc.GetBytes(sFullKey.ToCharArray(), 0, sFullKey.Length, unicodeText, 0, true);
58
+
59
+ MD5 md5 = new MD5CryptoServiceProvider();
60
+ byte[] result = md5.ComputeHash(unicodeText);
61
+
62
+ StringBuilder sb = new StringBuilder();
63
+ for (int i = 0; i < result.Length; i++) {
64
+ sb.Append(result[i].ToString("X2")); //produces a hexadecimal equivalent of the result padded to 2 characters minimum (1 would become 01)
65
+ }
66
+ return (hash.CompareTo(sb.ToString()) == 0);
67
+ }
68
+```
... ...
\ No newline at end of file