Advertisement
Guest User

Untitled

a guest
Nov 6th, 2016
150
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.97 KB | None | 0 0
  1. <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="LogIn.aspx.cs" Inherits="MembershipSite.LogIn" %>
  2.  
  3. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  4.  
  5. <html xmlns="http://www.w3.org/1999/xhtml" >
  6. <head runat="server">
  7. <title></title>
  8. </head>
  9. <body>
  10. <form id="form1" runat="server">
  11. <div>
  12. <h2>LogIn Page</h2>
  13. <asp:Label ID="Label1" runat="server" Text="Please log in below to access the membership area."></asp:Label>
  14. <br />
  15. <br />
  16. <asp:Login ID="LoginControl" runat="server"
  17. onauthenticate="LoginControl_Authenticate">
  18. </asp:Login>
  19. </div>
  20. </form>
  21. </body>
  22. </html>
  23.  
  24. using System;
  25. using System.Collections.Generic;
  26. using System.Linq;
  27. using System.Web;
  28. using System.Web.UI;
  29. using System.Web.UI.WebControls;
  30.  
  31. using System.Web.Security;
  32. using System.Data.SqlClient;
  33. using HashLibrary;
  34. using System.Configuration;
  35. using System.Text.RegularExpressions;
  36.  
  37.  
  38. namespace MembershipSite
  39. {
  40. public partial class LogIn : System.Web.UI.Page
  41. {
  42. protected void Page_Load(object sender, EventArgs e)
  43. {
  44.  
  45. }
  46.  
  47. protected void LoginControl_Authenticate(object sender, AuthenticateEventArgs e)
  48. {
  49. bool authenticated = this.ValidateCredentials(LoginControl.UserName, LoginControl.Password);
  50.  
  51. if (authenticated)
  52. {
  53. FormsAuthentication.RedirectFromLoginPage(LoginControl.UserName, LoginControl.RememberMeSet);
  54. }
  55. }
  56.  
  57.  
  58.  
  59.  
  60. public bool IsAlphaNumeric(string text)
  61. {
  62. return Regex.IsMatch(text, "^[a-zA-Z0-9]+$");
  63. }
  64.  
  65. private bool ValidateCredentials(string userName, string password)
  66. {
  67. bool returnValue = false;
  68.  
  69. if (this.IsAlphaNumeric(userName) && userName.Length <= 50 && password.Length <= 50)
  70. {
  71. SqlConnection conn = null;
  72.  
  73. try
  74. {
  75. string sql = "select count(*) from UsersMemb where username = @username and password = @password";
  76.  
  77. conn = new SqlConnection(ConfigurationManager.ConnectionStrings["MembershipSiteConStr"].ConnectionString);
  78. SqlCommand cmd = new SqlCommand(sql, conn);
  79.  
  80. SqlParameter user = new SqlParameter();
  81. user.ParameterName = "@username";
  82. user.Value = userName.Trim();
  83. cmd.Parameters.Add(user);
  84.  
  85. SqlParameter pass = new SqlParameter();
  86. pass.ParameterName = "@password";
  87. pass.Value = Hasher.HashString(password.Trim());
  88. cmd.Parameters.Add(pass);
  89.  
  90. conn.Open();
  91.  
  92. int count = (int)cmd.ExecuteScalar();
  93.  
  94. if (count > 0) returnValue = true;
  95. }
  96. catch (Exception ex)
  97. {
  98. // Log your error
  99. }
  100. finally
  101. {
  102. if (conn != null) conn.Close();
  103. }
  104. }
  105. else
  106. {
  107. // Log error - user name not alpha-numeric or
  108. // username or password exceed the length limit!
  109. }
  110.  
  111. return returnValue;
  112. }
  113.  
  114. }
  115. }
  116.  
  117. <?xml version="1.0" encoding="utf-8"?>
  118. <!--
  119. For more information on how to configure your ASP.NET application, please visit
  120. http://go.microsoft.com/fwlink/?LinkId=169433
  121. -->
  122. <configuration>
  123.  
  124.  
  125. <appSettings>
  126. <add key="ValidationSettings:UnobtrusiveValidationMode" value="None" />
  127. </appSettings>
  128.  
  129. <system.web>
  130. <compilation debug="true" targetFramework="4.6.1"/>
  131. <httpRuntime targetFramework="4.6.1"/>
  132.  
  133. <authentication mode="Forms">
  134. <forms defaultUrl="~/members/member.aspx" loginUrl="~/login.aspx" slidingExpiration="true" timeout="20"></forms>
  135. </authentication>
  136.  
  137. </system.web>
  138.  
  139.  
  140. <system.codedom>
  141. <compilers>
  142. <compiler language="c#;cs;csharp" extension=".cs"
  143. type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.CSharpCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
  144. warningLevel="4" compilerOptions="/langversion:6 /nowarn:1659;1699;1701"/>
  145. <compiler language="vb;vbs;visualbasic;vbscript" extension=".vb"
  146. type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.VBCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
  147. warningLevel="4" compilerOptions="/langversion:14 /nowarn:41008 /define:_MYTYPE="Web" /optionInfer+"/>
  148. </compilers>
  149. </system.codedom>
  150.  
  151.  
  152. <connectionStrings>
  153. <add name="MembershipSiteConStr" connectionString="Data Source=TIMLAWLOR-HPSQLEXPRESS; database=DmiVideoApp; Persist Security Info=True; integrated security=SSPI" providerName="System.Data.SqlClient" />
  154. </connectionStrings>
  155.  
  156.  
  157. </configuration>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement