ASP.NET 用户验证模块

ASP.NET用户登陆验证

1.1登录界面前端代码展示(用户控件)

%@ Control Language="C#" AutoEventWireup="true" CodeFile="landing.ascx.cs" Inherits="controls_WebUserControl" %>

<div>

<center>

<asp:Label ID="tishi" Text="欢迎登陆本系统" runat="server" ForeColor="Red"></asp:Label>

<table style="height: 86px; width: 319px">

<tr>

<td>用户名</td>

<td><asp:TextBox ID="txt_user" runat="server" Text="" MaxLength="11"></asp:TextBox>

    </td>

</tr>

<tr>

<td>密码</td>

<td><asp:TextBox ID="txt_password" runat="server" Text="" MaxLength="11" TextMode="password"></asp:TextBox></td>

</tr>

</table>

    <asp:Button ID="btn_landing" Text="登录" runat="server" Width="95px" 

        Height="26px" onclick="btn_landing_Click"/>

</center>

</div>

1.2后端.cs文件代码展示

using System;

using System.Text;

using System.Configuration;

using System.Collections;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Web.Security;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Web.UI.WebControls.WebParts;

using System.Web.UI.HtmlControls;

using System.Data.SqlClient;

using System.Data;



public partial class controls_WebUserControl : System.Web.UI.UserControl

{

    static string strCon = "Server=localhost;Initial Catalog=se;Trusted_Connection=yes;"; // 连接windows身份验证模式

    SqlConnection con = new SqlConnection(strCon);

    SqlCommand cmd = new SqlCommand();

    protected void Page_Load(object sender, EventArgs e)

    {

        cmd.CommandType = CommandType.Text;

        cmd.Connection = con;

    }



    protected void btn_landing_Click(object sender, EventArgs e)

    {

       con.Open();

        string userName= txt_user.Text.Trim();

        string userPassword = txt_password.Text.Trim();

        cmd.CommandText = "Select * from logo where User_id='" + userName + "' and  User_password='" + userPassword + "'";

        SqlDataReader dr = cmd.ExecuteReader();

        if (txt_user.Text == "system")    //验证用户输入用户名是否为管理员

        {

            Response.Redirect("~/Default1.aspx");

        }

        if (txt_user.Text == "jhq")

        {

            Response.Redirect("~/Default1.aspx");

        }

       if  (dr.Read())	   //验证用户输入用户名是否为访客

       {    

           //验证成功

            Response.Redirect("~/Default2.aspx");

       }

        else	    

        {   

           //验证失败

            Literal txtMsg = new Literal();

            txtMsg.Text = "

";



            Page.Controls.Add(txtMsg);

       }

       dr.Close();

       con.Close();



    } 

    }

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值