vs2010asp.net网站制作用户注册登录界面源代码(包含验证码)并把用户信息保存到数据库sql2008

数据库名是Member

login.aspx代码:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="login.aspx.cs" Inherits="login" %>


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">


<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <style type="text/css">
        .a
        {
            color: #FF0000;
        }
    </style>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <table id="tb_login" style="text-align: center"><tr><td>用户名:</td><td>
        <asp:TextBox ID="txtusername" runat="server"></asp:TextBox></td><td>
            <asp:RequiredFieldValidator ID="ReqFV_username" runat="server" 
                ErrorMessage="*" ControlToValidate="txtusername"></asp:RequiredFieldValidator>
        </td></tr>
        <tr><td>密码:</td><td>
            <asp:TextBox ID="txtpass" runat="server" TextMode="Password"></asp:TextBox></td><td><a href="passchange.aspx" class="a">忘记密码</a></td></tr>
            <tr><td>验证码:</td><td>
                <asp:TextBox ID="txtcode" runat="server"></asp:TextBox></td><td>
                    <asp:RequiredFieldValidator ID="ReqFV_code" runat="server" 
                        ErrorMessage="*" ControlToValidate="txtcode"></asp:RequiredFieldValidator>
                <img style=" cursor:hand;width: 76px; height: 21px"  id="imgcode" src="checkcode.aspx" alt="看不清,请点击我!" οnclick="this.src=this.src+'?'" /></td></tr>
            <tr><td>
                <asp:Button ID="btnlogin" runat="server" Text="登录" οnclick="btnlogin_Click" /></td><td></td><td>
                    <asp:Label ID="lablogin" runat="server" Text="lablogin"></asp:Label></td></tr>
    </table>
    
    </div>
    </form>
</body>
</html>

aspx.cs代码:

using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Security;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;


public partial class login : System.Web.UI.Page
{


    protected void Page_Load(object sender, EventArgs e)
    {


    }
    protected void btnlogin_Click(object sender, EventArgs e)
    {
    //    //try
    //    //{
    //        SqlConnection con = new SqlConnection("Data Source=(local);Initial Catalog=Member;Integrated Security=True");
    //        con.Open();
    //        string pass = FormsAuthentication.HashPasswordForStoringInConfigFile(txtpass.Text, "MD5");
    //        string sqlSel = "select count(*) from MemberInfo where userName=@name and userPass=@pass";
    //        SqlCommand com = new SqlCommand(sqlSel, con);
    //        com.Parameters.Add(new SqlParameter("name", SqlDbType.NVarChar, 20));
    //        com.Parameters["name"].Value = txtusername;
    //        com.Parameters.Add(new SqlParameter("pass", SqlDbType.NVarChar, 50));
    //        com.Parameters["pass"].Value = txtpass;
    //        if ( Convert.ToInt32( com.ExecuteScalar())>0)
    //        {
    //            lablogin.Text = "登陆成功";
    //            txtpass.Text = "";


    //        }
    //        else
    //        {
    //            lablogin.Text = "登录失败";
    //        }
    //    }
    //    //catch(Exception ex)
    //    //{
    //    //    lablogin.Text = "" + ex.Message;
    //    //}
        if (Request.Cookies["checkcode"].Value == txtcode.Text)
        {
            string sqlconnstr = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
            SqlConnection con = new SqlConnection(sqlconnstr);
            SqlCommand com = new SqlCommand();
            com.Connection = con;
            con.Open();
            com.CommandText = "select * from MemberInfo where userName=@name and userPass=@pass";
            com.Parameters.AddWithValue("@name", txtusername.Text);
            com.Parameters.AddWithValue("@pass", txtpass.Text);
            SqlDataReader sqldatareader = com.ExecuteReader();
            if (sqldatareader.Read())
            {
                txtpass.Text = "";
                txtcode.Text = "";
                lablogin.Text = "登陆成功";
                com = null;
                con.Close();
                con = null;
            }


            else
            {
                txtpass.Text = "";
                txtcode.Text = "";
                lablogin.Text = "用户名或密码错误";
                com = null;
                con.Close();
                con = null;
            }
        }
        else
        {
            txtpass.Text = "";
            txtcode.Text = "";
            lablogin.Text = "验证码错误";
        }
    }
}

zhuce.aspx代码:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="zhuce.aspx.cs" Inherits="zhuce" %>


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">


<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <style type="text/css">
        .style1
        {
            width: 40px;
        }
    </style>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    
    <table>
    <tr><td>用户名:</td>
    <td>
        <asp:TextBox ID="txtusername" runat="server"></asp:TextBox>


    </td><td class="style1">
            <asp:RequiredFieldValidator ID="ReqFV_username" runat="server" 
                ControlToValidate="txtusername" ErrorMessage="*"></asp:RequiredFieldValidator>
        </td>
    </tr>
    <tr><td>密码:</td>
    <td>
        <asp:TextBox ID="txtuserpass" runat="server" TextMode="Password"></asp:TextBox>
    </td><td class="style1">
            <asp:RequiredFieldValidator ID="ReqFV_userpass" runat="server" 
                ControlToValidate="txtuserpass" ErrorMessage="*"></asp:RequiredFieldValidator>
            
        </td></tr>
    <tr><td>性别:</td><td>
        <asp:DropDownList ID="DropDownList_sex" runat="server">
            <asp:ListItem>男</asp:ListItem>
            <asp:ListItem>女</asp:ListItem>
        </asp:DropDownList>
    </td><td class="style1"></td></tr>
    <tr><td>年龄:</td><td>
        <asp:TextBox ID="txtage" runat="server"></asp:TextBox></td><td class="style1">
            &nbsp;</td></tr>
        <tr><td>联系方式:</td><td>
            <asp:TextBox ID="txttelnum" runat="server"></asp:TextBox></td>
            <td class="style1">
                &nbsp;</td></tr>
            <tr><td>照片:</td><td>
                <asp:FileUpload ID="FileUpload_photo" runat="server" /></td><td class="style1"></td></tr>
                <tr><td>所在城市:</td><td>
                    <asp:DropDownList ID="DropDownList_city" runat="server">
                        <asp:ListItem>请选择所在城市</asp:ListItem>
                        <asp:ListItem>北京</asp:ListItem>
                        <asp:ListItem>上海</asp:ListItem>
                        <asp:ListItem>广州</asp:ListItem>
                        <asp:ListItem>深圳</asp:ListItem>
                    </asp:DropDownList>
                </td><td class="style1"></td></tr>
                
                    
    </table>
        <br />
    </div>
        <div><table>
    <tr><td>
                    <asp:Button ID="btn_zhuce" runat="server" Text="注册" οnclick="btn_zhuce_Click" /></td><td>
                        <asp:Button ID="btn_login" runat="server" Text="返回登录" 
                            οnclick="btn_login_Click" CausesValidation="False" /></td><td class="style1">        <asp:Label ID="labMes" runat="server" Text="Label"></asp:Label></td></tr>
    </table></div>
    </form>
</body>
</html>

zhuce.aspx.cs代码:

using System;
using System.Data.SqlClient;
using System.Configuration;
using System.Security.Cryptography;
using System.Web.Security;


public partial class zhuce : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {


    }
    private string Encrypt(string strPwd)
    {
        MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();//创建MD5对象
        byte[] data = System.Text.Encoding.Default.GetBytes(strPwd);//将字符编码为一个字节序列
        byte[] md5data = md5.ComputeHash(data);//计算data字节数组的哈希值
        md5.Clear();//清空MD5对象
        string str = "";//定义一个变量,用来记录加密后的密码
        for (int i = 0; i < md5data.Length - 1; i++)//遍历字节数组
        {
            str += md5data[i].ToString("x").PadLeft(2, '0');//对遍历到的字节进行加密
        }
        return str;//返回得到的加密字符串
      
    }
    //private string GetMD5(string sDataIn) 
    //{ 
    //    MD5CryptoServiceProvider md5=new MD5CryptoServiceProvider(); 
    //    byte[]bytValue,bytHash; 
    //    bytValue = System.Text.Encoding.UTF8.GetBytes(sDataIn); 
    //    bytHash = md5.ComputeHash(bytValue); 
    //    md5.Clear(); 
    //    string sTemp= " "; 
    //    for(int i=0;i <bytHash.Length;i++) 
    //    { 
    //    sTemp+=bytHash[i].ToString( "X ").PadLeft(2, '0'); 
    //    } 
    //    return sTemp.ToLower(); 
    //    } 
    protected void btn_zhuce_Click(object sender, EventArgs e)
    {
        string pass = FormsAuthentication.HashPasswordForStoringInConfigFile(txtuserpass.Text, "MD5");


                string sqlconnstr = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
                SqlConnection con = new SqlConnection(sqlconnstr);
                SqlCommand com = new SqlCommand();
                com.Connection = con;
                com.CommandText = @"insert into MemberInfo(userName,userPass,sex,age,telnum,photo,city)
                                    values(@userName,@userPass,@sex,@age,@telnum,@photo,@city)";
                com.Parameters.AddWithValue("@userName",txtusername.Text);
                com.Parameters.AddWithValue("@userPass", txtuserpass.Text);
                com.Parameters.AddWithValue("@sex",DropDownList_sex.Text);
                com.Parameters.AddWithValue("@age",txtage.Text);
                com.Parameters.AddWithValue("@telnum", txttelnum.Text);
                com.Parameters.AddWithValue("@photo",FileUpload_photo.FileName);
                com.Parameters.AddWithValue("@city", DropDownList_city.Text);
                try
                {
                    con.Open();
                    com.ExecuteNonQuery();
                    if (FileUpload_photo.HasFile == true)
                    {
                        FileUpload_photo.SaveAs(Server.MapPath(("./MPhotos/") + FileUpload_photo.FileName));
                    }
                    labMes.Text = "注册成功!";


                }
                catch (Exception ex)
                {
                    labMes.Text="" + ex.Message;
                }
    }
    protected void btn_login_Click(object sender, EventArgs e)
    {
        Response.Redirect("login.aspx");
    }
}
  • 13
    点赞
  • 74
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
好的,我可以为您提供一个简单的教程,帮助您使用 ASP.NET Core 6.0 框架和 SQL Server 数据库生成注册登录界面。 1. 安装 SQL Server 数据库 如果您还没有安装 SQL Server 数据库,请使用此链接下载并安装 SQL Server 数据库:https://www.microsoft.com/en-us/sql-server/sql-server-downloads 2. 创建 ASP.NET Core 6.0 项目 打开 Visual Studio 2022 或更高版本,创建一个新的 ASP.NET Core 6.0 Web 应用程序项目。在创建项目时,选择 Web 应用程序模板。 3. 添加 Entity Framework Core 在解决方案资源管理器,右键单击项目文件夹,选择“管理 NuGet 包”。在 NuGet 包管理器,搜索并安装 Microsoft.EntityFrameworkCore.SqlServer 和 Microsoft.EntityFrameworkCore.Tools 包。 4. 创建数据库模型 在项目根目录,创建一个名为 Models 的文件夹。在该文件夹,创建一个名为 ApplicationUser.cs 的新类。在该类,定义用户模型: ``` using Microsoft.AspNetCore.Identity; namespace YourProjectName.Models { public class ApplicationUser : IdentityUser { // 可以在此定义其他用户属性 } } ``` 5. 创建数据库上下文 在 Models 文件夹创建一个名为 ApplicationDbContext.cs 的新类。在该类,继承 DbContext 并将 ApplicationUser 添加到 DbSet: ``` using Microsoft.AspNetCore.Identity.EntityFrameworkCore; using Microsoft.EntityFrameworkCore; namespace YourProjectName.Models { public class ApplicationDbContext : IdentityDbContext<ApplicationUser> { public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options) : base(options) { } } } ``` 6. 配置应用程序 在 Startup.cs 文件,配置应用程序以使用 Entity Framework Core 和 SQL Server 数据库。在 ConfigureServices 方法,添加以下代码: ``` services.AddDbContext<ApplicationDbContext>(options => options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection"))); services.AddDefaultIdentity<ApplicationUser>(options => options.SignIn.RequireConfirmedAccount = true) .AddEntityFrameworkStores<ApplicationDbContext>(); ``` 在 appsettings.json 文件,添加以下代码: ``` "ConnectionStrings": { "DefaultConnection": "Server=(localdb)\\mssqllocaldb;Database=YourDatabaseName;Trusted_Connection=True;MultipleActiveResultSets=true" }, ``` 7. 生成注册和登录功能 在 Visual Studio ,右键单击“Controllers”文件夹,选择“添加” > “新建项目项”。在“添加新项目项”对话框,选择“Identity”模板,并选择“注册”和“登录”选项。单击“添加”按钮,Visual Studio 将为您生成注册和登录功能。 8. 运行应用程序 按 F5 键或单击“调试” > “启动调试”按钮以运行应用程序。您现在可以访问注册和登录页面。 希望这个简单的教程可以帮助到您!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值