231071cf5a26c5b63030a7987c1085115561fad0
Tutorials/CodeSamples/Login-Sample.md
| ... | ... | @@ -4,27 +4,32 @@ |
| 4 | 4 | |
| 5 | 5 | ##About |
| 6 | 6 | |
| 7 | -This is a simple authentication function you can place in your Login page (Login.aspx in the default starter kit). |
|
| 7 | +This is a simple authentication function you can place in your Login page (Login.aspx in the default starter kit). We will review adding a hard-coded login to the page and enabling that login in the Global.asax file. |
|
| 8 | + |
|
| 9 | +##Editing Login.aspx |
|
| 8 | 10 | |
| 9 | 11 | Normally, logins should be pulled from the database, active directory, or whatever system your organization uses for storing user info. We also support using your company's existing login page in place of our example login page. If nothing is available, here is a simple method to provide basic authentication. Also included is the callback function used by **btnLogin**'s "OnClick" property. |
| 10 | 12 | |
| 11 | 13 | ```csharp |
| 12 | 14 | Bool AuthenticateUser(string userName, string password, out bool isAdmin) |
| 13 | 15 | { |
| 16 | + //We can control logins through hashtables, which enable a key (the username) to be associated with a value (the password). We can also have a separate list of admins |
|
| 17 | + //that can contain duplicate usernames since checking the person's admin status is a separate operation |
|
| 14 | 18 | System.Collections.Hashtable Passwords = new System.Collections.Hashtable(); |
| 15 | 19 | System.Collections.ArrayList Admins = new System.Collections.ArrayList(); |
| 16 | 20 | |
| 17 | - Passwords["Bob"]="password"; |
|
| 18 | - Passwords["Steve"]="password"; |
|
| 19 | - Passwords["Stacey"]="password"; |
|
| 20 | - Passwords["Kim"]="password"; |
|
| 21 | + Passwords["Bob"]="password"; //Adds a key called "Bob" if it doesn't exist and sets the value to "password" |
|
| 22 | + Passwords["Steve"]="$tr0ng_pA$$w0rd"; //Adds "Steve" |
|
| 23 | + Passwords["Stacey"]="password"; //Adds "Stacey" |
|
| 24 | + Passwords["Kim"]="password"; //Adds "Kim" |
|
| 21 | 25 | |
| 22 | - Admins.Add("Bob"); |
|
| 23 | - Admins.Add("Steve"); |
|
| 26 | + Admins.Add("Bob"); //Adds "Bob" as an admin |
|
| 27 | + Admins.Add("Steve"); //Adds "Steve" as an admin |
|
| 24 | 28 | |
| 25 | - if(Passwords[userName].Equals(password)) |
|
| 29 | + isAdmin = false; //Ensure false is returned in the "out" direction variable "isAdmin" if authentication fails |
|
| 30 | + if(Passwords.ContainsKey(userName) && Passwords[userName].Equals(password)) //Authenticate the username with the password |
|
| 26 | 31 | { |
| 27 | - isAdmin = Admins.Contains(userName); |
|
| 32 | + isAdmin = Admins.Contains(userName); //Check if the user is an admin |
|
| 28 | 33 | return true; |
| 29 | 34 | } |
| 30 | 35 | |
| ... | ... | @@ -42,12 +47,18 @@ void btnLogin_Click(object sender, EventArgs args) |
| 42 | 47 | else |
| 43 | 48 | HttpContext.Current.Session["Role"] = "RegularUser"; |
| 44 | 49 | global_asax.CustomAdHocConfig.InitializeReporting(); |
| 45 | - Response.Redirect("ReportList.aspx"); |
|
| 46 | 50 | Page.Title = "Izenda - " + AdHocSettings.CurrentUserName; |
| 51 | + Response.Redirect("ReportList.aspx"); |
|
| 47 | 52 | //FormsAuthentication.RedirectFromLoginPage(userNameTextbox.Text, false); |
| 48 | 53 | return; |
| 49 | 54 | } |
| 50 | 55 | else |
| 51 | 56 | loginValidator.IsValid = false; |
| 52 | 57 | } |
| 53 | -``` |
|
| ... | ... | \ No newline at end of file |
| 0 | +``` |
|
| 1 | + |
|
| 2 | +If you are using a database to hold usernames and passwords, your ``AuthenticateUser()`` method will look slightly different. For instance, you could have a method to load all usernames from the database into an array that you can call from a utility class. Notice that we set the isAdmin property to false before checking if the user is an admin. This will help prevent false positives. You will also notice that we comment out the ``FormsAuthentication.RedirectFromLoginPage`` line. This is because we are using a different redirection technique with ``Response.Redirect`` that will ensure the Report List page is the next page the user sees. We also call ``InitializeReporting()`` at this time. Find out more about the [[InitializeReporting|/FAQ/InitializeReporting]] method. |
|
| 3 | + |
|
| 4 | +##Conclusion |
|
| 5 | + |
|
| 6 | +After you have set up your global.asax to use ``InitializeReporing()``, you now have basic authentication that integrates with Izenda. You may now use your login page. |
|
| ... | ... | \ No newline at end of file |