注册 | 登录 忘记密码? 51cto首页 | 博客 | 论坛 | 招聘
热点文章 LCS2005客户端配置详解:L..
 帮助

结合数据库的用户验证。


2005-06-22 16:54:00
 标签:数据库 验证 结合   [推送到技术圈]

版权声明:原创作品,如需转载,请与作者联系。否则将追究法律责任。
1.WEB.CONFIG文件
 <appSettings>
   <add key="DataSource" value="zj.mdb" />
  <add key="Provider" value="Provider = Microsoft.Jet.OLEDB.4.0" />
  <add key="Cookies" value="zj" />
 </appSettings>

2.login.aspx文件
<%@ Page language="c#" Codebehind="Login.aspx.cs" AutoEventWireup="false" Inherits="QA.Login" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
    
<HEAD>
        
<title>Login</title>
        
<meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
        
<meta name="CODE_LANGUAGE" Content="C#">
        
<meta name="vs_defaultClientScript" content="JavaScript">
        
<meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
    
</HEAD>
    
<body MS_POSITIONING="GridLayout">
        
<form id="Form1" method="post" runat="server">
            用户名:
<input id="UserId" type="text" runat="server" style="width:150;height:19">
            密码:
<input id="UserPass" type="password" runat="server" style="width:150;height:19">
            
<asp:button Text="登录" OnClick="Login_Click" Runat="server" id="Button1" />
            
<asp:Label ID="Msg" ForeColor="red" Runat="server" />
        
</form>
    
</body>
</HTML>



3.login.aspx.cs文件

# region 导入命名空间
using System;
using System.Collections;
using System.ComponentModel;
using System.Configuration;
using System.Data;
using System.Drawing;
using System.Data.OleDb;
using System.Web.SessionState;
using System.Web.Security;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
# endregion
namespace QA
{
    
/// <summary>
    
/// Login 的摘要说明。
    
/// </summary>

    public class Login : System.Web.UI.Page
    
{

        
public static string strConn
        
{
            
get
            
{
                StringBuilder strResult 
= new StringBuilder(ConfigurationSettings.AppSettings["Provider"]);
                strResult.Append(
"");
                strResult.Append(
"Data Source = ");
                strResult.Append(HttpContext.Current.Server.MapPath(
"."));
                strResult.Append(
"\\");
                strResult.Append(ConfigurationSettings.AppSettings[
"DataSource"]);
                
return strResult.ToString();
            }

        }

        
protected string strSQL;
        
protected OleDbDataAdapter Adpt = new OleDbDataAdapter();
        
protected DataSet Ds = new DataSet();
        
protected OleDbConnection Conn = new OleDbConnection(strConn);
        
protected OleDbCommand Cmd = new OleDbCommand();
        
protected System.Web.UI.WebControls.Button Button1;
        
protected System.Web.UI.WebControls.Label Msg;
        
protected System.Web.UI.HtmlControls.HtmlInputText UserId;
        
protected System.Web.UI.HtmlControls.HtmlInputText UserPass;
        
protected string strUserID;
        
protected string strUserPass;
        
        
protected void Login_Click(object sender, System.EventArgs e)
        
{
        
            
string strName = UserId.Value.ToString().Trim();
            
string strPass = UserPass.Value.ToString().Trim();

            strSQL 
= "SELECT * FROM Qadmin WHERE UserId = @name AND UserPass = @pass";
            Conn.Open();
            OleDbCommand Cmd 
= new OleDbCommand(strSQL, Conn);
            Cmd.Parameters.Add(
new OleDbParameter("@name", OleDbType.Char, 20)).Value = strName;
            Cmd.Parameters.Add(
new OleDbParameter("@pass", OleDbType.Char, 32)).Value = strPass;
            OleDbDataReader Rd 
= Cmd.ExecuteReader();
            
if (Rd.Read())
            
{
                HttpCookie xCookies 
= new HttpCookie("zj");
                xCookies[
"UserId"= UserId.Value.ToString();
                xCookies.Expires 
= DateTime.Now.AddDays(1);
                Response.Cookies.Add(xCookies);
                Rd.Close();
                Dispose();
                Cmd.Dispose();
                Conn.Close();
                Conn.Dispose();
                Response.Redirect(
"admin.aspx?Type=Index");
            }

            
else
            
{
                Msg.Text 
= "验证失败";
            }


        }


    }

}

本文出自 “共享经验” 博客,转载请与作者联系!





    文章评论
 
 

发表评论

昵   称:
验证码:  点击图片可刷新验证码  博客过2级,无需填写验证码
内   容: