毕业论文写作

毕业设计需求

asp.net 实现用户登录和注册——基于webform模式

    环境:Windows10 + VS2015 + 自带LocalDB

    看一下效果:

    1、注册页面:

1.jpg

如果用户重名:

 

2、登录页:

 

3、注册或者登录好了会跳到Home页面并且显示当前的用户

 

下面看看关键代码:

①注册前台页面Register.aspx:

  1. <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Register.aspx.cs" Inherits="Album.OnlineAlbum.Register" %>

  2.  

  3. <!DOCTYPE html>

  4.  

  5. <html xmlns="http://www.w3.org/1999/xhtml">

  6. <head runat="server">

  7. <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>

  8. <title>注册</title>

  9. <style>

  10. form{

  11.  

  12.  

  13. color:#575454;

  14. width:500px;

  15. margin:20px auto;

  16. font-size:15px;

  17. }

  18. .label{

  19. color:red;

  20. font-size:12px;

  21. font-family:'Lucida Console';

  22. }

  23.  

  24. input.Tb{

  25. border-radius:5px;

  26.  

  27. }

  28.  

  29. .user_name{ width:240px; height:38px; line-height:38px; border:1px solid #000; background:url(login_img_03.png) no-repeat left center; padding-left:30px; }

  30. .user_name input{ width:230px; height:36px; border:1px solid #fff;color:#666;}

  31. .password{ width:240px; height:38px; line-height:38px; border:1px solid #dfe1e8; background:url(login_img_09.png) no-repeat left center; padding-left:30px; }

  32. .password input{ width:230px; height:36px; border:1px solid #000;color:#666;}

  33. .transButton {

  34. border:solid 1px;

  35. background-color:transparent;

  36. }

  37. #btnRegister{

  38.  

  39. font-size:14px;

  40. }

  41. #linkToLogin{

  42. text-decoration:none

  43. }

  44. #ckItem{

  45. text-decoration:none

  46. }

  47. body{

  48. background-image:url("rbg.jpg");

  49. }

  50. </style>

  51. </head>

  52. <body>

  53. <form id="form1" runat="server">

  54.  

  55. <h2>欢迎注册OA</h2>

  56.  

  57. <h3>每一天,记录美。</h3>

  58.  

  59.  

  60.  

  61. <br />

  62.  

  63. <asp:ScriptManager ID="ScriptManager1" runat="server">

  64. </asp:ScriptManager>

  65.  

  66.  

  67. <asp:TextBox runat="server" ID="rUserNameText" Height="40px" Width="490px" CssClass="Tb"></asp:TextBox>

  68.  

  69. <br />

  70. <asp:UpdatePanel ID="UpdatePanel1" runat="server">

  71. <ContentTemplate>

  72. <asp:CustomValidator ID="CustomValidator1" runat="server"

  73. ControlToValidate="rUserNameText" ErrorMessage="*"

  74. onservervalidate="CustomValidator1_ServerValidate">

  75. </asp:CustomValidator>

  76. </ContentTemplate>

  77.  

  78. </asp:UpdatePanel>

  79. <br />

  80. <asp:TextBox runat="server" ID="rPsdText" TextMode="Password" Height="40px" Width="490px" CssClass="Tb"></asp:TextBox>

  81. <br />

  82. <asp:UpdatePanel ID="UpdatePanel2" runat="server">

  83. <ContentTemplate>

  84. <asp:CustomValidator ID="CustomValidator2" runat="server"

  85. ControlToValidate="rPsdText" ErrorMessage="*"

  86. onservervalidate="CustomValidator2_ServerValidate">

  87. </asp:CustomValidator>

  88. </ContentTemplate>

  89.  

  90. </asp:UpdatePanel>

  91. <br />

  92. <asp:TextBox runat="server" ID="rrPsdText" TextMode="Password" Height="40px" Width="490px" CssClass="Tb" ></asp:TextBox>

  93. <br />

  94. <asp:UpdatePanel ID="UpdatePanel3" runat="server">

  95. <ContentTemplate>

  96. <asp:CustomValidator ID="CustomValidator3" runat="server"

  97. ControlToValidate="rrPsdText" ErrorMessage="*"

  98. onservervalidate="CustomValidator3_ServerValidate">

  99. </asp:CustomValidator>

  100. </ContentTemplate>

  101.  

  102. </asp:UpdatePanel>

  103.  

  104. <br />

  105. <table>

  106. <tr>

  107. <td>

  108. <asp:CheckBox ID="CheckBox1" runat="server" Checked="true" />

  109. </td>

  110. <td>

  111. <span>同意</span> <asp:LinkButton runat="server" Text="服务条款" ID="ckItem"></asp:LinkButton>

  112. </td>

  113. <td>

  114.                       

  115.                       

  116.       

  117. </td>

  118. <td>

  119.  

  120. <asp:LinkButton ID="linkToLogin" runat="server" Text="已有账号?登录" OnClick="linkToLogin_Click"></asp:LinkButton>

  121. </td>

  122. </tr>

  123. </table>

  124.  

  125.  

  126.  

  127.  

  128.  

  129.  

  130. <asp:Button ID="btnRegister" runat="server" CssClass="transButton" Height="49px" Text="注 册" Width="500px" OnClick="btnRegister_Click" />

  131.  

  132.  

  133. </form>

  134. </body>

  135. <script type="text/javascript">

  136. function watermark(id, value) {

  137. var obj = document.getElementById(id);

  138. var isPsdMode = false;

  139. if (obj.type == "password")

  140. {

  141. obj.type = "text";

  142. isPsdMode = true;

  143. }

  144. obj.value = value;

  145. obj.style.color = "Gray";

  146. //获取焦点事件

  147. obj.onfocus = function () {

  148.  

  149. obj.style.color = "Black";

  150. if (isPsdMode)

  151. {

  152. obj.type = "password";

  153. }

  154. if (this.value == value) {

  155. this.value = '';

  156. }

  157. };

  158. //失去焦点事件

  159. obj.onblur = function () {

  160. if (this.value == "") {

  161. if (isPsdMode) {

  162. obj.type = "text";

  163. }

  164. this.value = value;

  165. obj.style.color = "Gray";

  166. }

  167. else {

  168. obj.style.color = "Black";

  169. }

  170. };

  171. }

  172. window.onload = function () {

  173. var arr = [{ 'id': 'rUserNameText', 'desc': '用户名' }, { 'id': 'rPsdText', 'desc': '密码' },{ 'id': 'rrPsdText', 'desc': '确认密码' }];

  174. for (var i = 0; i < arr.length; i++) {

  175. watermark(arr[i].id, arr[i].desc);

  176. }

  177. };

  178. </script>

  179. </html>

 

  注册后台页面Register.aspx.cs:

 

  1. using System;

  2. using System.Data;

  3. using System.Data.SqlClient;

  4. using System.Drawing;

  5.  

  6. namespace Album.OnlineAlbum

  7. {

  8. public partial class Register : System.Web.UI.Page

  9. {

  10. private bool UserNameIselgal = false;

  11. private bool PsdIselgal = false;

  12. private bool CanRegister = false;

  13.  

  14. protected void Page_Load(object sender, EventArgs e)

  15. {

  16.  

  17. }

  18.  

  19.  

  20. protected void linkToLogin_Click(object sender, EventArgs e)

  21. {

  22. Response.Redirect("Login.aspx");

  23. }

  24.  

  25. protected void btnRegister_Click(object sender, EventArgs e)

  26. {

  27. Session["User"] = rUserNameText.Text;

  28. Session["Psd"] = rPsdText.Text;

  29.  

  30. string connStr = @"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\Database1.mdf;Integrated Security=True";

  31. SqlConnection conn = new SqlConnection(connStr);

  32.  

  33.  

  34. try

  35. {

  36. conn.Open();

  37. SqlCommand cmd = new SqlCommand("select count(*) from UserTable where UId=@UId", conn);

  38. cmd.Parameters.Add("@UId", SqlDbType.Char);

  39. cmd.Parameters[0].Value = Session["User"];

  40. int count = (int)cmd.ExecuteScalar();

  41.  

  42. if (count > 0)

  43. {

  44.  

  45. Response.Write("<script>alert('用户名已存在!')</script>");

  46. }

  47. else

  48. {

  49.  

  50. CanRegister = true;

  51. }

  52. }

  53. catch

  54. {

  55. Response.Write("检测重名异常");

  56. }

  57.  

  58. finally

  59. {

  60. conn.Close();

  61. }

  62.  

  63. if (CanRegister)

  64. {

  65. try

  66. {

  67. conn.Open();

  68. string strIns = "insert into UserTable(UId, Psd) values(@UId, @Psd)";

  69. SqlCommand cmd = new SqlCommand(strIns, conn);

  70. cmd.Parameters.Add("@UId", SqlDbType.NChar);

  71. cmd.Parameters.Add("@Psd", SqlDbType.NChar);

  72.  

  73. cmd.Parameters["@UId"].Value = Session["User"];

  74. cmd.Parameters["@Psd"].Value = Session["Psd"];

  75.  

  76. cmd.ExecuteNonQuery();

  77. }

  78. catch

  79. {

  80. Response.Write("注册异常");

  81. }

  82. finally

  83. {

  84. conn.Close();

  85.  

  86. }

  87. }

  88. CanRegister = CanRegister && UserNameIselgal && PsdIselgal;

  89. if (CanRegister)

  90. {

  91. Session["CurrentUser"] = rUserNameText.Text;

  92. Response.Redirect("Home.aspx");

  93. }

  94.  

  95. }

  96.  

  97. protected void CustomValidator1_ServerValidate(object source, System.Web.UI.WebControls.ServerValidateEventArgs args)

  98. {

  99. if (rUserNameText.Text.Equals("用户名"))

  100. {

  101. CustomValidator1.ErrorMessage = "*用户名为空";

  102. args.IsValid = false;

  103. } else if (System.Text.RegularExpressions.Regex.IsMatch(rUserNameText.Text, "^[0-9a-zA-Z]+$") &&

  104. rUserNameText.Text.Length > 5 && rUserNameText.Text.Length < 11)

  105. {

  106. args.IsValid = true;

  107. UserNameIselgal = true;

  108. }

  109. else

  110. {

  111. CustomValidator1.ErrorMessage = "*用户名由6~10位数字和字母构成";

  112. args.IsValid = false;

  113. }

  114.  

  115.  

  116. }

  117.  

  118. protected void CustomValidator2_ServerValidate(object source, System.Web.UI.WebControls.ServerValidateEventArgs args)

  119. {

  120. if (rPsdText.Text.Equals("密码"))

  121. {

  122. CustomValidator2.ErrorMessage = "*密码为空";

  123. args.IsValid = false;

  124. }

  125.  

  126. else if (System.Text.RegularExpressions.Regex.IsMatch(rPsdText.Text, "^[0-9a-zA-Z]+$") &&

  127. rPsdText.Text.Length > 4)

  128. {

  129. args.IsValid = true;

  130. }

  131. else

  132. {

  133. CustomValidator2.ErrorMessage = "*密码由全数字和字母构成且不少于5位";

  134. args.IsValid = false;

  135. }

  136. }

  137.  

  138. protected void CustomValidator3_ServerValidate(object source, System.Web.UI.WebControls.ServerValidateEventArgs args)

  139. {

  140. if (rrPsdText.Text.Equals("") ||rrPsdText.Text.Equals("确认密码"))

  141. {

  142. args.IsValid = false;

  143. CustomValidator3.ErrorMessage = "*确认密码为空";

  144. }

  145. else if (!rrPsdText.Text.Equals(rPsdText.Text))

  146. {

  147. args.IsValid = false;

  148. CustomValidator3.ErrorMessage = "*两次密码不一致";

  149. }

  150. else

  151. {

  152. PsdIselgal = true;

  153. args.IsValid = true;

  154. }

  155. }

  156. }

  157. }

    ②登录前台页面Login.aspx:

    

  1. <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Login.aspx.cs" Inherits="Album.OnlineAlbum.Login" %>

  2.  

  3. <!DOCTYPE html>

  4.  

  5. <html xmlns="http://www.w3.org/1999/xhtml">

  6. <head runat="server">

  7. <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>

  8. <title>登陆</title>

  9. <style>

  10. form{

  11.  

  12.  

  13. color:#575454;

  14. width:500px;

  15. margin: auto;

  16. font-size:15px;

  17. margin-top:260px;

  18. }

  19. #spanpsd{

  20. margin-left:125px;

  21. }

  22. #spanuser{

  23. margin-left:110px;

  24. }

  25. div{

  26. margin:30px auto;

  27. align-content:center;

  28. }

  29. .textbox{

  30. border:solid 1px;

  31. background:rgba(0, 0, 0, 0);

  32. }

  33. #LinkButton1{

  34. text-decoration:none;

  35. color:lightblue;

  36. margin-left:230px;

  37.  

  38. }

  39. #Button1{

  40. border-radius:2px;

  41. border:solid 1px;

  42. background-color:transparent;

  43. margin-left:150px;

  44. margin-top:10px;

  45. }

  46. body{

  47. background-image: url("lbg.jpg");

  48. }

  49.  

  50. </style>

  51. </head>

  52. <body>

  53. <form id="form1" runat="server">

  54. <div>

  55. <div>

  56. <span id="spanuser">用户名:</span>

  57. <asp:TextBox ID="TextBox1" runat="server" CssClass="textbox" Height="30px" Width="240px"></asp:TextBox>

  58. </div>

  59.  

  60. <div>

  61. <span id="spanpsd">密码:</span>

  62. <asp:TextBox ID="TextBox2" runat="server" CssClass="textbox" Height="30px" Width="240px" TextMode="Password"></asp:TextBox>

  63. </div>

  64.  

  65. <div>

  66. <asp:LinkButton ID="LinkButton1" runat="server" OnClick="LinkButton1_Click">没有账号?注册</asp:LinkButton>

  67. <br />

  68. <asp:Button ID="Button1" runat="server" Text="登 录" Width="270px" Height="40px" OnClick="Button1_Click" />

  69. </div>

  70. </div>

  71.  

  72. </form>

  73. </body>

  74. </html>

    登录后台页面Login.aspx.cs:

    

  1. using System;

  2. using System.Collections.Generic;

  3. using System.Data;

  4. using System.Data.SqlClient;

  5. using System.Linq;

  6. using System.Web;

  7. using System.Web.UI;

  8. using System.Web.UI.WebControls;

  9.  

  10. namespace Album.OnlineAlbum

  11. {

  12. public partial class Login : System.Web.UI.Page

  13. {

  14. protected void Page_Load(object sender, EventArgs e)

  15. {

  16.  

  17. }

  18.  

  19.  

  20.  

  21. protected void LinkButton1_Click(object sender, EventArgs e)

  22. {

  23. Response.Redirect("Register.aspx");

  24. }

  25.  

  26. protected void Button1_Click(object sender, EventArgs e)

  27. {

  28. string connStr = @"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\Database1.mdf;Integrated Security=True";

  29. SqlConnection conn = new SqlConnection(connStr);

  30.  

  31. try

  32. {

  33.  

  34.  

  35. conn.Open();

  36. SqlCommand cmd = new SqlCommand("select count(*) from UserTable where UId=@UId and Psd=@Psd", conn);

  37. cmd.Parameters.Add("@UId", SqlDbType.Char);

  38. cmd.Parameters.Add("@Psd", SqlDbType.Char);

  39. cmd.Parameters[0].Value = TextBox1.Text;

  40. cmd.Parameters[1].Value = TextBox2.Text;

  41. int count = (int)cmd.ExecuteScalar();

  42. if (count == 1)

  43. {

  44.  

  45. Session["CurrentUser"] = TextBox1.Text;

  46. Response.Redirect("./Home.aspx");

  47.  

  48.  

  49. }

  50. else

  51. {

  52. Response.Write("<script>alert('用户名或密码错误')</script>");

  53. }

  54. }

  55. catch

  56. {

  57. Response.Write("<script>alert('登录异常')</script>");

  58. }

  59.  

  60. finally

  61. {

  62. conn.Close();

  63. }

  64.  

  65. }

  66. }

  67. }

    ③Home.aspx:

    

  1. <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Home.aspx.cs" Inherits="Album.OnlineAlbum.Home" %>

  2.  

  3. <!DOCTYPE html>

  4.  

  5. <html xmlns="http://www.w3.org/1999/xhtml">

  6. <head runat="server">

  7. <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>

  8. <title>Online Ablum</title>

  9. <style>

  10. #page_header{

  11. height:20px;

  12. margin-left:10px;

  13. }

  14. </style>

  15. </head>

  16. <body>

  17.  

  18.  

  19.  

  20. <form runat="server">

  21.  

  22.  

  23. <asp:LinkButton runat="server" OnClick="Unnamed2_Click" ID="btnToReg">注册</asp:LinkButton>

  24.  

  25.  <asp:LinkButton runat="server" OnClick="Unnamed1_Click" ID="btnToLog">登录</asp:LinkButton>

  26.  

  27. </form>

  28.  

  29.  

  30. </body>

  31. </html>

    home.aspx.cs:

    

  1. using System;

  2. using System.Collections.Generic;

  3. using System.Linq;

  4. using System.Web;

  5. using System.Web.UI;

  6. using System.Web.UI.WebControls;

  7.  

  8. namespace Album.OnlineAlbum

  9. {

  10. public partial class Home : System.Web.UI.Page

  11. {

  12. protected void Page_Load(object sender, EventArgs e)

  13. {

  14. if (Session["CurrentUser"] != null)

  15. btnToReg.Text = Session["CurrentUser"].ToString();

  16. }

  17.  

  18. protected void Unnamed1_Click(object sender, EventArgs e)

  19. {

  20. Response.Redirect("Login.aspx");

  21. }

  22.  

  23. protected void Unnamed2_Click(object sender, EventArgs e)

  24. {

  25. Response.Redirect("Register.aspx");

  26. }

  27. }

  28. }    

   Web.config:

  1. <?xml version="1.0" encoding="utf-8"?>

  2. <!--

  3. 有关如何配置 ASP.NET 应用程序的详细信息,请访问

  4. http://go.microsoft.com/fwlink/?LinkId=169433

  5. -->

  6. <configuration>

  7.  

  8.  

  9. <connectionStrings>

  10. <add name="ConnectionString" connectionString="Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\Database1.mdf;Integrated Security=True"

  11. providerName="System.Data.SqlClient" />

  12. </connectionStrings>

  13. <appSettings>

  14. <add key="ValidationSettings:UnobtrusiveValidationMode" value="none"/>

  15. </appSettings>

  16. <system.web>

  17. <compilation debug="true" targetFramework="4.5.2"/>

  18. <httpRuntime targetFramework="4.5.2"/>

  19. <httpModules>

  20. <add name="ApplicationInsightsWebTracking" type="Microsoft.ApplicationInsights.Web.ApplicationInsightsHttpModule, Microsoft.AI.Web"/>

  21. </httpModules>

  22. </system.web>

  23. <system.codedom>

  24. <compilers>

  25. <compiler language="c#;cs;csharp" extension=".cs"

  26. type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.CSharpCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"

  27. warningLevel="4" compilerOptions="/langversion:6 /nowarn:1659;1699;1701"/>

  28. <compiler language="vb;vbs;visualbasic;vbscript" extension=".vb"

  29. type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.VBCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"

  30. warningLevel="4" compilerOptions="/langversion:14 /nowarn:41008 /define:_MYTYPE=\"Web\" /optionInfer+"/>

  31. </compilers>

  32. </system.codedom>

  33. <system.webServer>

  34. <validation validateIntegratedModeConfiguration="false"/>

  35. <modules>

  36. <remove name="ApplicationInsightsWebTracking"/>

  37. <add name="ApplicationInsightsWebTracking" type="Microsoft.ApplicationInsights.Web.ApplicationInsightsHttpModule, Microsoft.AI.Web"

  38. preCondition="managedHandler"/>

  39. </modules>

  40. </system.webServer>

  41. </configuration>

最新毕业设计成品

版权所有© 帮我毕业网 并保留所有权利

QQ 1370405256 微信 biyebang

QQ:629001810微信:biyebang

收缩