using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data.Common;
using System.Data.OleDb;
using System.Configuration;
namespace Web教学系统
{
/// <summary>
/// WebForm1 的摘要说明。
/// </summary>
public class WebForm1 : System.Web.UI.Page
{
protected System.Web.UI.WebControls.TextBox TextBox1;
protected System.Web.UI.WebControls.TextBox TextBox2;
protected System.Web.UI.WebControls.LinkButton LinkButton1;
protected System.Web.UI.WebControls.Button Button1;
protected System.Web.UI.WebControls.Button Button2;
protected System.Web.UI.WebControls.Label Label2;
protected System.Web.UI.WebControls.Label Label3;
protected System.Web.UI.WebControls.Label Label1;
protected System.Web.UI.WebControls.LinkButton LinkButton2;
OleDbConnection Con=new OleDbConnection();
   

private void Page_Load(object sender, System.EventArgs e)
{
Con.ConnectionString="Provider=Microsoft.Jet.OLEDB.4.0; Data Source="+Server.MapPath(@".\DB\webst.mdb"); // 在此处放置用户代码以初始化页面
} #region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN:该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}

/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{    
this.Button1.Click += new System.EventHandler(this.Button1_Click);
this.Button2.Click += new System.EventHandler(this.Button2_Click);
this.LinkButton1.Click += new System.EventHandler(this.LinkButton1_Click);
this.LinkButton2.Click += new System.EventHandler(this.LinkButton2_Click);
this.Load += new System.EventHandler(this.Page_Load); }
#endregion private void Button1_Click(object sender, System.EventArgs e)
{
string strCmd="SELECT count(*) FROM information WHERE (UserName LIKE '"+TextBox1.Text+"')AND(pwd LIKE '"+TextBox2.Text+"')";

OleDbCommand myCommand=new OleDbCommand(strCmd,Con);
myCommand.Connection.Open();
int flag=(int)myCommand.ExecuteScalar();
myCommand.Connection.Close();
Con.Close();
if(flag>0)
{
Session["UserName"]=this.TextBox1.Text;
this.Response.Redirect("main.aspx");
}
else
{
this.Label2.Text="对不起!你的用户名与密码不符,请重新输入";
this.TextBox1.Text="";
this.TextBox2.Text="";
}
} private void Button2_Click(object sender, System.EventArgs e)
{
this.Response.Redirect("index.aspx");
} private void LinkButton1_Click(object sender, System.EventArgs e)
{
this.Response.Redirect("register.aspx");
} private void LinkButton2_Click(object sender, System.EventArgs e)
{
this.Response.Redirect("administer/default.aspx");
}
}
}

解决方案 »

  1.   

    ASP.Net实现验证码! 
    http://www.cnblogs.com/hover/articles/1283.aspx
      

  2.   

    login.aspx:<%@ Page Language="C#" Inherits="plane.login" src="login.cs"%>
    <HTML>
    <HEAD>
    <TITLE>管理员登陆</TITLE>
    <META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=gb2312">
    <link href="../main.css" rel="stylesheet" type="text/css">
    <style type="text/css">
    <!--
    .样式2 {color: #FF9900}
    .样式3 {color: #FF0000}
    -->
    </style>
    </HEAD>
    <BODY BGCOLOR=#FFFFFF LEFTMARGIN=0 TOPMARGIN=0 MARGINWIDTH=0 MARGINHEIGHT=0>
    <form runat="server">
    <!-- ../imageReady Slices (login.psd) -->
    <TABLE WIDTH=477 BORDER=0 align="center" CELLPADDING=0 CELLSPACING=0>
    <TR>
    <TD COLSPAN=11>&nbsp;
      </TD>
    <TD ROWSPAN=10>&nbsp;
      </TD>
    </TR>
    <TR>
    <TD ROWSPAN=9>&nbsp;
      </TD>
    <TD ROWSPAN=8>
    <IMG SRC="../image/login_04.gif" WIDTH=16 HEIGHT=255 ALT=""></TD>
    <TD COLSPAN=8>
    <IMG SRC="../image/alogin_05.gif" WIDTH=389 HEIGHT=22 ALT=""></TD>
    <TD ROWSPAN=8>
    <IMG SRC="../image/login_06.gif" WIDTH=23 HEIGHT=255 ALT=""></TD>
    </TR>
    <TR>
      <TD height="70" COLSPAN=8 background="../image/alogin_07.gif"> <span class="样式2">
    <asp:Label id="lblShowe">管理员登陆!</asp:Label>
    </span><br>
    <span class="样式3">
    <asp:Label id="lblShow" runat="Server" EnableViewState="false">如忘记密码,请输入用户名过,点密码重发!</asp:Label>
    </span> <Br>
          <span class="样式3"> </span> </TD>
    </TR>
    <TR>
    <TD height="27" COLSPAN=2 background="../image/login_08.gif">
    <div align="center">  用户名:</div></TD>
    <TD height="27" COLSPAN=3 background="../image/login_09.gif"><input name="user_name" type="text" class="input" id="user_name" size="20" runat="server">
      </TD>
    <TD COLSPAN=3>
    <IMG SRC="../image/login_10.gif" WIDTH=137 HEIGHT=27 ALT=""></TD>
    </TR>
    <TR>
    <TD height="29" COLSPAN=2 background="../image/login_11.gif">
    <div align="center">  密 码:</div></TD>
    <TD height="29" COLSPAN=3 background="../image/login_12.gif"><input name="user_password" type="password" class="input" id="user_password" size="20" runat="server">
      </TD>
    <TD COLSPAN=2><asp:imageButton ID="login" runat="server" OnClick="btnOk_Click" imageUrl="../image/login_13.gif" WIDTH=83 HEIGHT=29 ALT="登陆"></asp:imageButton></TD>
    <TD>
    <IMG SRC="../image/login_14.gif" WIDTH=54 HEIGHT=29 ALT=""></TD>
    </TR>
    <TR>
      <TD height="26" COLSPAN=2 background="../image/login_15.gif"><div align="center">  验 证 码:</div></TD>
      <TD height="26" COLSPAN=3 background="../image/login_15.gif"><asp:textbox ID="txtValidateCode" CssClass="input" size="20" runat="server" TextMode="SingleLine" /></TD>
      <TD COLSPAN=3 background="../image/login_15.gif"><asp:Image id="Image1" runat="server" ImageUrl="gif.aspx"></asp:Image></TD>
        </TR>
    <TR>
    <TD>
    <IMG SRC="../image/login_16.gif" WIDTH=57 HEIGHT=28 ALT=""></TD>
    <TD COLSPAN=2>
    <asp:imageButton ID="forgetpass" enabled="false" OnClick="btnpass_Click" runat="server" imageUrl="../image/login_17.gif"  ALT="请填入用户名后点击密码重发!"></asp:imageButton></TD>
    <TD>
    <IMG SRC="../image/login_18.gif" WIDTH=93 HEIGHT=28 ALT=""></TD>
    <TD COLSPAN=2>   <IMG SRC="../image/login_19.gif" ALT="还没注册!):还不赶快行动啊!" WIDTH=82 HEIGHT=28 border="0"></TD>
    <TD COLSPAN=2>
    <IMG SRC="../image/login_20.gif" WIDTH=74 HEIGHT=28 ALT=""></TD>
    </TR>
    <TR>
    <TD COLSPAN=8>
    <IMG SRC="../image/login_21.gif" WIDTH=389 HEIGHT=29 ALT=""></TD>
    </TR>
    <TR>
    <TD COLSPAN=8>
    <IMG SRC="../image/login_22.gif" WIDTH=389 HEIGHT=24 ALT=""></TD>
    </TR>
    <TR>
    <TD height="19" COLSPAN=10>&nbsp;
      </TD>
    </TR>
    <TR>
    <TD>
    <IMG SRC="../image/spacer.gif" WIDTH=28 HEIGHT=1 ALT=""></TD>
    <TD>
    <IMG SRC="../image/spacer.gif" WIDTH=16 HEIGHT=1 ALT=""></TD>
    <TD>
    <IMG SRC="../image/spacer.gif" WIDTH=57 HEIGHT=1 ALT=""></TD>
    <TD>
    <IMG SRC="../image/spacer.gif" WIDTH=42 HEIGHT=1 ALT=""></TD>
    <TD>
    <IMG SRC="../image/spacer.gif" WIDTH=41 HEIGHT=1 ALT=""></TD>
    <TD>
    <IMG SRC="../image/spacer.gif" WIDTH=93 HEIGHT=1 ALT=""></TD>
    <TD>
    <IMG SRC="../image/spacer.gif" WIDTH=19 HEIGHT=1 ALT=""></TD>
    <TD>
    <IMG SRC="../image/spacer.gif" WIDTH=63 HEIGHT=1 ALT=""></TD>
    <TD>
    <IMG SRC="../image/spacer.gif" WIDTH=20 HEIGHT=1 ALT=""></TD>
    <TD>
    <IMG SRC="../image/spacer.gif" WIDTH=54 HEIGHT=1 ALT=""></TD>
    <TD>
    <IMG SRC="../image/spacer.gif" WIDTH=23 HEIGHT=1 ALT=""></TD>
    <TD>
    <IMG SRC="../image/spacer.gif" WIDTH=21 HEIGHT=1 ALT=""></TD>
    </TR>
    </TABLE>
    </form>
    </body>
    </html>
      

  3.   

    login.cs
    using System;
    using System.Collections;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Web;
    using System.Web.SessionState;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.HtmlControls;
    using System.Web.Mail;
    using System.Web.Security;namespace plane
    {
    /// <summary>
    /// tree_Index 的摘要说明。
    /// </summary>
    public class login : System.Web.UI.Page
    {    
            protected config conn=new config();
    protected System.Web.UI.HtmlControls.HtmlInputText user_name;
    protected System.Web.UI.HtmlControls.HtmlInputText user_password;
    protected System.Web.UI.WebControls.TextBox txtValidateCode;
    protected System.Web.UI.WebControls.Label lblShow;
    protected System.Web.UI.WebControls.Image Image1; string str_Sql; private void Page_Load(object sender, System.EventArgs e)
    {
    // 在此处放置用户代码以初始化页面

     if (Session["VNum"]!=null) { 
             string VNum  =Session["VNum"].ToString();
                             ViewState["VNum"]=VNum; }
    }
           
                    public void btnOk_Click(object sender, ImageClickEventArgs e)
      { 
      if (txtValidateCode.Text!=ViewState["VNum"].ToString() ){
                          lblShow.Text="<font color='red'>提示:验证码错误!</font>";
       }
                          else{
          string username=user_name.Value;
          string userpassword=user_password.Value;
          userpassword=FormsAuthentication.HashPasswordForStoringInConfigFile(userpassword, "MD5");
                          string str_Sel="select admin.id,admin_purview.purview_name from admin_purview,admin where admin.purview_id=admin_purview.id and admin_name='"+username+"' and password='"+userpassword+"'";
          conn.Fill(str_Sel);
          int count= conn.ds.Tables[0].Rows.Count;
          if (count>0) {
              Session["login"]="true";
      Session["id"]=conn.ds.Tables[0].Rows[0]["id"].ToString().Trim();
      Session["purview_name"]=conn.ds.Tables[0].Rows[0]["purview_name"].ToString().Trim();
      Response.Redirect("index.aspx");
      
         }
         else 
           lblShow.Text="<font color='red'>用户名或密码错误!</font>";
         }    
      }
      public void btnpass_Click(object sender, ImageClickEventArgs e)
      { 
      
    conn.Open();
                   string user_name=Request.Form["user_name"].Trim();
           
           str_Sql="select password,msn from agent where  agent_name='"+user_name+"'";
           
    int count =conn.GetRowCount(str_Sql);
    if (count>0)
      {
        conn.GetRowRecord(str_Sql);
        string MailBody=user_name+"您好!,你的密码是:"+conn.dr["password"];
                            SmtpMail.Send(    "*****@163.com",  conn.dr["msn"].ToString(),    "密码信息",    MailBody );
    Response.Write("<script>alert('您的密码已发送到你的邮箱,快去看看吧!')</"+"script>");
    //Response.Redirect("index.aspx");
      }
    else{
     Response.Write("<script>alert('用户名输入有误!,请重新输入!')</script>");
     return;
      }    
      }

    #region Web Form Designer generated code
    override protected void OnInit(EventArgs e)
    {
    //
    // CODEGEN:该调用是 ASP.NET Web 窗体设计器所必需的。
    //
    InitializeComponent();
    base.OnInit(e);
    }

    /// <summary>
    /// 设计器支持所需的方法 - 不要使用代码编辑器修改
    /// 此方法的内容。
    /// </summary>
    private void InitializeComponent()
    {    
    this.Load += new System.EventHandler(this.Page_Load);
    }
    #endregion
    }
    }
      

  4.   

    gif.aspx:<%@ import namespace="System"%>
    <%@ import namespace="System.IO"%>
    <%@ import namespace="System.Drawing"%>
    <%@ import namespace="System.Drawing.Imaging"%>
    <%@ import namespace="System.Drawing.Drawing2D"%>
    <script language="C#" runat="server">
    private Bitmap validateimage;
    private Graphics g;
     public void  Page_Load(object Sender   ,EventArgs e  ){
        if (!IsPostBack){ 
        string  VNum  =MakeValidateCode( );
    Session["VNum"]=VNum;
        ValidateCode(VNum);}
     }
    public void  ValidateCode(string VNum)
    {
    validateimage = new Bitmap(70, 20, PixelFormat.Format24bppRgb);
    g = Graphics.FromImage(validateimage);g.DrawString(VNum, new Font("宋体",15),new SolidBrush(Color.White),new PointF(8,0));
    g.FillRectangle(new LinearGradientBrush(new Point(0,0), new Point(110,20), Color.FromArgb(0,0,0,0),Color.FromArgb(255,255,60,40)),0,0,120,30); 
    g.Save();
    MemoryStream ms=new MemoryStream();
    validateimage.Save(ms,System.Drawing.Imaging.ImageFormat.Gif);
    Response.ClearContent();
    Response.ContentType="image/gif";
    Response.BinaryWrite(ms.ToArray());
    Response.End();
    }  string MakeValidateCode()
    {
    /*char[] s = new char[]{'0','1', '2','3','4','5','6','7','8','9','a'
    ,'b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q'
    ,'r','s','t','u','v','w','x','y','z','A','B','C','D','E','F','G'
    ,'H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W'
    ,'X','Y','Z'};*/
    char[] s = new char[]{'0','1', '2','3','4','5','6','7','8','9'};
    string num = "";
    Random r = new Random();
    for(int i = 0; i < 5; i++)
    {
    num += s[r.Next(0, s.Length)].ToString(); 
    }
     return num;
    }
    </script>
      

  5.   

    想请问一下protected config conn=new config();中的CONFIG是哪里的呢?
      

  6.   

    想请问一下protected config conn=new config();中的CONFIG是哪里的呢?
    config.cs 文件是包括操作数据库的函数的。
    在这用的是:conn.Fill(str_Sel);int count =conn.GetRowCount(str_Sql);
    下面是它们的函数,你可以自已组织一下,直接使用,可以把protected config conn=new config();去掉/// <summary>
    /// 连接数据库
    /// </summary>
    public void Open()
    {   
    myConnection=new SqlConnection(System.Configuration.ConfigurationSettings.AppSettings["data"]);
    if (myConnection!=null)
    {
    myConnection.Close(); // 关闭数据库
    }
    myConnection.Open();
    }
    /// <summary>
    /// 建立DataSet对象,用记录填充或构架(如果必要)DataSet对
    象,DataSet即是数据在内存的缓存
    /// </summary>
    /// <param name="str_Sql">打开表Sql语句</param>
    public void Fill(string str_Sql)
    {  
    Open();
    myAdapter = new SqlDataAdapter(str_Sql,myConnection);
    ds = new DataSet();
    myAdapter.Fill(ds);
    }public int GetRowCount(string str_Sql)
    {
    Fill(str_Sql);
    try
    {
    int count=ds.Tables[0].Rows.Count;
    ds.Clear();
    myConnection.Close();
    return count;
    }
    catch
    {
    ds.Clear();
    myConnection.Close();
    return 0;
    }

    }
      

  7.   

    请问一下如果在程序中写SELECT count(*) FROM information WHERE (UserName LIKE '"+TextBox1.Text+"')AND(pwd LIKE '"+TextBox2.Text+"')";这样的语句会不会有不安全的因素呢??