最近在写asp课程设计,网站登录注册的功能怎么能少,捣鼓了两天终于弄出点东西来了。
环境:Windows10 + VS2015 + 自带LocalDB
看一下效果:
1、注册页面:
如果用户重名:
2、登录页:
3、注册或者登录好了会跳到Home页面并且显示当前的用户
下面看看关键代码:
①注册前台页面Register.aspx:
注册后台页面Register.aspx.cs:
- using System;
- using System.Data;
- using System.Data.SqlClient;
- using System.Drawing;
- namespace Album.OnlineAlbum
- {
- public partial class Register : System.Web.UI.Page
- {
- private bool UserNameIselgal = false;
- private bool PsdIselgal = false;
- private bool CanRegister = false;
- protected void Page_Load(object sender, EventArgs e)
- {
-
- }
- protected void linkToLogin_Click(object sender, EventArgs e)
- {
- Response.Redirect("Login.aspx");
- }
- protected void btnRegister_Click(object sender, EventArgs e)
- {
- Session["User"] = rUserNameText.Text;
- Session["Psd"] = rPsdText.Text;
- string connStr = @"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\Database1.mdf;Integrated Security=True";
- SqlConnection conn = new SqlConnection(connStr);
-
- try
- {
- conn.Open();
- SqlCommand cmd = new SqlCommand("select count(*) from UserTable where UId=@UId", conn);
- cmd.Parameters.Add("@UId", SqlDbType.Char);
- cmd.Parameters[0].Value = Session["User"];
- int count = (int)cmd.ExecuteScalar();
- if (count > 0)
- {
- Response.Write("<script>alert('用户名已存在!')</script>");
- }
- else
- {
-
- CanRegister = true;
- }
- }
- catch
- {
- Response.Write("检测重名异常");
- }
- finally
- {
- conn.Close();
- }
- if (CanRegister)
- {
- try
- {
- conn.Open();
- string strIns = "insert into UserTable(UId, Psd) values(@UId, @Psd)";
- SqlCommand cmd = new SqlCommand(strIns, conn);
- cmd.Parameters.Add("@UId", SqlDbType.NChar);
- cmd.Parameters.Add("@Psd", SqlDbType.NChar);
- cmd.Parameters["@UId"].Value = Session["User"];
- cmd.Parameters["@Psd"].Value = Session["Psd"];
- cmd.ExecuteNonQuery();
- }
- catch
- {
- Response.Write("注册异常");
- }
- finally
- {
- conn.Close();
- }
- }
- CanRegister = CanRegister && UserNameIselgal && PsdIselgal;
- if (CanRegister)
- {
- Session["CurrentUser"] = rUserNameText.Text;
- Response.Redirect("Home.aspx");
- }
-
- }
- protected void CustomValidator1_ServerValidate(object source, System.Web.UI.WebControls.ServerValidateEventArgs args)
- {
- if (rUserNameText.Text.Equals("用户名"))
- {
- CustomValidator1.ErrorMessage = "*用户名为空";
- args.IsValid = false;
- } else if (System.Text.RegularExpressions.Regex.IsMatch(rUserNameText.Text, "^[0-9a-zA-Z]+$") &&
- rUserNameText.Text.Length > 5 && rUserNameText.Text.Length < 11)
- {
- args.IsValid = true;
- UserNameIselgal = true;
- }
- else
- {
- CustomValidator1.ErrorMessage = "*用户名由6~10位数字和字母构成";
- args.IsValid = false;
- }
-
- }
- protected void CustomValidator2_ServerValidate(object source, System.Web.UI.WebControls.ServerValidateEventArgs args)
- {
- if (rPsdText.Text.Equals("密码"))
- {
- CustomValidator2.ErrorMessage = "*密码为空";
- args.IsValid = false;
- }
- else if (System.Text.RegularExpressions.Regex.IsMatch(rPsdText.Text, "^[0-9a-zA-Z]+$") &&
- rPsdText.Text.Length > 4)
- {
- args.IsValid = true;
- }
- else
- {
- CustomValidator2.ErrorMessage = "*密码由全数字和字母构成且不少于5位";
- args.IsValid = false;
- }
- }
- protected void CustomValidator3_ServerValidate(object source, System.Web.UI.WebControls.ServerValidateEventArgs args)
- {
- if (rrPsdText.Text.Equals("") ||rrPsdText.Text.Equals("确认密码"))
- {
- args.IsValid = false;
- CustomValidator3.ErrorMessage = "*确认密码为空";
- }
- else if (!rrPsdText.Text.Equals(rPsdText.Text))
- {
- args.IsValid = false;
- CustomValidator3.ErrorMessage = "*两次密码不一致";
- }
- else
- {
- PsdIselgal = true;
- args.IsValid = true;
- }
- }
- }
- }
复制代码
②登录前台页面Login.aspx:
- <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Login.aspx.cs" Inherits="Album.OnlineAlbum.Login" %>
- <!DOCTYPE html>
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head runat="server">
- <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
- <title>登陆</title>
- <style>
- form{
-
-
- color:#575454;
- width:500px;
- margin: auto;
- font-size:15px;
- margin-top:260px;
- }
- #spanpsd{
- margin-left:125px;
- }
- #spanuser{
- margin-left:110px;
- }
- div{
- margin:30px auto;
- align-content:center;
- }
- .textbox{
- border:solid 1px;
- background:rgba(0, 0, 0, 0);
- }
- #LinkButton1{
- text-decoration:none;
- color:lightblue;
- margin-left:230px;
-
- }
- #Button1{
- border-radius:2px;
- border:solid 1px;
- background-color:transparent;
- margin-left:150px;
- margin-top:10px;
- }
- body{
- background-image: url("lbg.jpg");
- }
-
- </style>
- </head>
- <body>
- <form id="form1" runat="server">
- <div>
- <div>
- <span id="spanuser">用户名:</span>
- <asp:TextBox ID="TextBox1" runat="server" CssClass="textbox" Height="30px" Width="240px"></asp:TextBox>
- </div>
- <div>
- <span id="spanpsd">密码:</span>
- <asp:TextBox ID="TextBox2" runat="server" CssClass="textbox" Height="30px" Width="240px" TextMode="Password"></asp:TextBox>
- </div>
- <div>
- <asp:LinkButton ID="LinkButton1" runat="server" OnClick="LinkButton1_Click">没有账号?注册</asp:LinkButton>
- <br />
- <asp:Button ID="Button1" runat="server" Text="登 录" Width="270px" Height="40px" OnClick="Button1_Click" />
- </div>
- </div>
-
- </form>
- </body>
- </html>
复制代码
登录后台页面Login.aspx.cs:
- using System;
- using System.Collections.Generic;
- using System.Data;
- using System.Data.SqlClient;
- using System.Linq;
- using System.Web;
- using System.Web.UI;
- using System.Web.UI.WebControls;
- namespace Album.OnlineAlbum
- {
- public partial class Login : System.Web.UI.Page
- {
- protected void Page_Load(object sender, EventArgs e)
- {
-
- }
-
- protected void LinkButton1_Click(object sender, EventArgs e)
- {
- Response.Redirect("Register.aspx");
- }
- protected void Button1_Click(object sender, EventArgs e)
- {
- string connStr = @"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\Database1.mdf;Integrated Security=True";
- SqlConnection conn = new SqlConnection(connStr);
- try
- {
- conn.Open();
- SqlCommand cmd = new SqlCommand("select count(*) from UserTable where UId=@UId and Psd=@Psd", conn);
- cmd.Parameters.Add("@UId", SqlDbType.Char);
- cmd.Parameters.Add("@Psd", SqlDbType.Char);
- cmd.Parameters[0].Value = TextBox1.Text;
- cmd.Parameters[1].Value = TextBox2.Text;
- int count = (int)cmd.ExecuteScalar();
- if (count == 1)
- {
- Session["CurrentUser"] = TextBox1.Text;
- Response.Redirect("./Home.aspx");
- }
- else
- {
- Response.Write("<script>alert('用户名或密码错误')</script>");
- }
- }
- catch
- {
- Response.Write("<script>alert('登录异常')</script>");
- }
- finally
- {
- conn.Close();
- }
- }
- }
- }
复制代码
③Home.aspx:
- <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Home.aspx.cs" Inherits="Album.OnlineAlbum.Home" %>
- <!DOCTYPE html>
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head runat="server">
- <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
- <title>Online Ablum</title>
- <style>
- #page_header{
- height:20px;
- margin-left:10px;
- }
- </style>
- </head>
- <body>
-
-
- <form runat="server">
-
-
- <asp:LinkButton runat="server" OnClick="Unnamed2_Click" ID="btnToReg">注册</asp:LinkButton>
-
- <asp:LinkButton runat="server" OnClick="Unnamed1_Click" ID="btnToLog">登录</asp:LinkButton>
-
- </form>
-
-
- </body>
- </html>
复制代码
home.aspx.cs:
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- using System.Web.UI;
- using System.Web.UI.WebControls;
- namespace Album.OnlineAlbum
- {
- public partial class Home : System.Web.UI.Page
- {
- protected void Page_Load(object sender, EventArgs e)
- {
- if (Session["CurrentUser"] != null)
- btnToReg.Text = Session["CurrentUser"].ToString();
- }
- protected void Unnamed1_Click(object sender, EventArgs e)
- {
- Response.Redirect("Login.aspx");
- }
- protected void Unnamed2_Click(object sender, EventArgs e)
- {
- Response.Redirect("Register.aspx");
- }
- }
- }
复制代码
Web.config: - <?xml version="1.0" encoding="utf-8"?>
- <!--
- 有关如何配置 ASP.NET 应用程序的详细信息,请访问
- http://go.microsoft.com/fwlink/?LinkId=169433
- -->
- <configuration>
-
- <connectionStrings>
- <add name="ConnectionString" connectionString="Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\Database1.mdf;Integrated Security=True"
- providerName="System.Data.SqlClient" />
- </connectionStrings>
- <appSettings>
- <add key="ValidationSettings:UnobtrusiveValidationMode" value="none"/>
- </appSettings>
- <system.web>
- <compilation debug="true" targetFramework="4.5.2"/>
- <httpRuntime targetFramework="4.5.2"/>
- <httpModules>
- <add name="ApplicationInsightsWebTracking" type="Microsoft.ApplicationInsights.Web.ApplicationInsightsHttpModule, Microsoft.AI.Web"/>
- </httpModules>
- </system.web>
- <system.codedom>
- <compilers>
- <compiler language="c#;cs;csharp" extension=".cs"
- type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.CSharpCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
- warningLevel="4" compilerOptions="/langversion:6 /nowarn:1659;1699;1701"/>
- <compiler language="vb;vbs;visualbasic;vbscript" extension=".vb"
- type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.VBCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
- warningLevel="4" compilerOptions="/langversion:14 /nowarn:41008 /define:_MYTYPE="Web" /optionInfer+"/>
- </compilers>
- </system.codedom>
- <system.webServer>
- <validation validateIntegratedModeConfiguration="false"/>
- <modules>
- <remove name="ApplicationInsightsWebTracking"/>
- <add name="ApplicationInsightsWebTracking" type="Microsoft.ApplicationInsights.Web.ApplicationInsightsHttpModule, Microsoft.AI.Web"
- preCondition="managedHandler"/>
- </modules>
- </system.webServer>
- </configuration>
复制代码
附上项目代码,需要积分下载。
ps:这是我一个在线相册系统的一部分,完整项目源码见github,可以不用积分下载,不过你得理清我写的东西。 来源:https://blog.csdn.net/Richard1997/article/details/79354090 免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作! |