CREATE TABLE users( email [varchar](250) NOT NULL, password [varchar](250) NOT NULL, CONSTRAINT [PK_users] PRIMARY KEY CLUSTERED ( [email] ASC ) )
<configuration> <connectionStrings> <add name="Main" connectionString="uid=myuser; pwd=mypassword; Initial Catalog=mydatabase; Server=mydbserver" /> </connectionStrings> <system.web> <authentication mode="Forms" /> <authorization> <deny users="?" /> </authorization> </system.web> <location path="Assets"> <system.web> <authorization> <allow users="*" /> </authorization> </system.web> </location> </configuration>
<p> <strong>Email:</strong> <br /> <asp:TextBox ID="txtUserName" runat="server" Columns=50></asp:TextBox> </p> <p> <strong>Password:</strong> <br /> <asp:TextBox ID="txtUserPass" runat="server" Columns=50 TextMode="Password"></asp:TextBox> </p> <p> <asp:Button ID="bnLogon" runat="server" Text="Logon" /> <br /> <asp:Label id="lblMsg" ForeColor="red" runat="server" /> </p>
using System; using System.Configuration; using System.Collections.Generic; using System.Web; using System.Text; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.Security; using System.Data.SqlClient; using System.Security.Cryptography; public partial class Login : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { if (!String.IsNullOrEmpty(Request.Params["logout"])) { FormsAuthentication.SignOut(); Response.Redirect("./"); } } protected void bnLogon_Click(object sender, EventArgs e) { if (ValidateUser(txtUserName.Text, txtUserPass.Text)) FormsAuthentication.RedirectFromLoginPage(txtUserName.Text, true); else lblMsg.Text = "Incorrect"; } /// <summary> /// Filter out the fat fingers who get their passwords wrong /// </summary> bool ValidateUser(string user, string pass) { string connStr = ConfigurationManager.ConnectionStrings["Main"].ConnectionString; using (SqlConnection conn = new SqlConnection(connStr)) { conn.Open(); string sql = "select email from users where email = @email and password = @password"; SqlCommand cmd = new SqlCommand(sql, conn); cmd.Parameters.AddWithValue("@email", user); cmd.Parameters.AddWithValue("@password", Sha1(Salt(pass))); return cmd.ExecuteScalar() is string; } } /// <summary> /// Salt the hell out of a string before hashing it /// </summary> public string Salt(string text) { return "zu5QnKrH4NJfOgV2WWqV5Oc1l" + text + "1DMuByokGSDyFPQ0DbXd9rAgW"; } /// <summary> /// One-way hash the password, so the DBA can't see the inevitable swear words in the password column /// </summary> public string Sha1(string text) { byte[] clear = Encoding.UTF8.GetBytes(text); byte[] hash = new SHA1CryptoServiceProvider().ComputeHash(clear); return BitConverter.ToString(hash).Replace("-", "").ToLower(); } }
<% if (Context.User.Identity.IsAuthenticated) { %> <%= Context.User.Identity.Name%> Log out <% } %>
a="zu5QnKrH4NJfOgV2WWqV5Oc1l" b="1DMuByokGSDyFPQ0DbXd9rAgW" c=a + "my new password here" + b require 'digest/sha1' Digest::SHA1.hexdigest c => "e7f0df4d064a7d2cdc653447e752cf4d736e114b"
Thanks for reading! And if you want to get in touch, I'd love to hear from you: chris.hulbert at gmail.
(Comp Sci, Hons - UTS)
Software Developer (Freelancer / Contractor) in Australia.
I have worked at places such as Google, Cochlear, Assembly Payments, News Corp, Fox Sports, NineMSN, FetchTV, Coles, Woolworths, Trust Bank, and Westpac, among others. If you're looking for help developing an iOS app, drop me a line!
Get in touch:
[email protected]
github.com/chrishulbert
linkedin