C#用户登录判断+session对象

天学习了C#关于用户登录判断和session使用、、、、一下是个成品。。。有需要的亲们学学哇~~~共有两个页面。。log.aspx和test.aspx

步骤一:log.aspx界面设计 来个难看的。。C#用户登录判断+session对象

代码如下:

--------------------------------------------------------------------------------------------------

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="log.aspx.cs" Inherits="Hp_hr.log" %>

<!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: 500px;
        }
        .style2
        {
            font-size: large;
        }
        .style5
        {
            width: 530px;
            border: 1px solid #008080;
        }
    </style>
</head>
<body>
    <form id="form1" runat="server">
    <div>
   
        <table class="style5" align="center">
            <tr>
                <td class="style2" colspan="5">
                    用户登录:</td>
            </tr>
            <tr>
                <td>
                    用户名:</td>
                <td>
                    <asp:TextBox ID="Tusername" runat="server"></asp:TextBox>
                </td>
                <td>
                    &nbsp;</td>
                <td>
                    密码:</td>
                <td>
                    <asp:TextBox ID="Tpassword" runat="server"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td>
                    &nbsp;</td>
                <td>
                    &nbsp;</td>
                <td>
                    <asp:Button ID="Blog" runat="server" Text="登录" onclick="Blog_Click" />
                </td>
                <td>
                    &nbsp;</td>
                <td>
                    &nbsp;</td>
            </tr>
        </table>
   
    </div>
    </form>
</body>
</html>

--------------------------------------------------------------------------------------------------

步骤二:数据库设计

 C#用户登录判断+session对象

 步骤三:登录代码实现 log.aspx.cs页面 代码如下

-------------------------------------------------------------------------------------------------

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.Sql;
using System.Data.SqlClient;

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

        }

        protected void Blog_Click(object sender, EventArgs e)
        {
            string con = @"server=.;initial catalog=HP_hr_db;integrated security=True";
            string com = "SELECT uname,pwd FROM [user] WHERE uname='" + Tusername.Text.Trim() + "' and pwd='" + Tpassword.Text.Trim() + "'";
            SqlConnection conn = new SqlConnection(con);
            SqlCommand comm = new SqlCommand(com,conn);
            conn.Open();
            SqlDataReader read = comm.ExecuteReader();
            if (read.Read())
            {
                Session.Remove("susername");
                Session["susername"] = Tusername.Text;

                Response.Redirect("test.aspx");
            }
            else
            {
                Response.Write("<script language='javascript'>alert('用户名或密码错误!')</script>");
            }
            conn.Close();
        }
    }
}

--------------------------------------------------------------------------------------------------
步骤四:session对象的使用→test.aspx只用在Page_Load做判断,只有登录才能访问test.aspx页面、

test.aspx.cs代码如下:

--------------------------------------------------------------------------------------------------

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Hp_hr
{
    public partial class test : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["susername"] == null)
            {
                Response.Redirect("log.aspx");
            }
            else {
            //其他操作
            }
        }
    }
}

--------------------------------------------------------------------------------------------------

over、、、、、

posted on 2013-03-12 09:45  AI_JJ  阅读(1688)  评论(0)    收藏  举报

导航