using System;
using System.Data;
using System.Configuration;
using System.Collections;
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;
public partial class login : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        //从Web.config文件中获取数据库连接字符串    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        string path = System.Web.HttpContext.Current.Server.MapPath(ConfigurationManager.AppSettings["DateBaseConnectionString"]);
       // string constr = "server=localhost;uid=sa;pwd=2531;database=datebase;";
        SqlConnection con = new SqlConnection(path);
        string sql = "select * from manager where name='" + TextBox1.Text + "' and pwd='" + TextBox2.Text + "'";
        SqlCommand cmd = new SqlCommand(sql, con);
        try
        {
            con.Open();
        }
        catch
        {
            Response.Write("<script>alert('数据库连接异常,为此向您说声抱歉,请联系站长。');</script>");
        }
        SqlDataReader dr = cmd.ExecuteReader();
        if (dr.Read())
        {
            FormsAuthentication.RedirectFromLoginPage(TextBox1.Text, false );
        }
        else
        {
            Label1.Text = "用户名或密码错误,请重新输入。";
        }
        dr.Close();
        con.Close();
    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        TextBox1.Text = "";
        TextBox2.Text = "";
        Label1.Text = "";
    }
    protected void LinkButton1_Click(object sender, EventArgs e)
    {
        Response.Redirect("~/index.aspx");
    }
    protected void TextBox1_TextChanged(object sender, EventArgs e)
    {    }
    protected void SqlDataSource1_Selecting(object sender, SqlDataSourceSelectingEventArgs e)
    {    }
}
帮忙看看吧 在这先谢谢了

解决方案 »

  1.   

    使用FormsAuthentication验证机制,必须在wei.config中配置authentication
    例:
    <authentication mode="Forms">
    <forms cookieless="AutoDetect" loginUrl="~/User/Default.htm" name="SUMINGER" domain="jj-it.com"></forms>
    </authentication>
      

  2.   

    除此之外,还要设置目录权限
    <location path="Master">
    <system.web>
    <authorization>
    <deny users="?"></deny>
    </authorization>
    </system.web>
    </location>
      

  3.   

    手工的自己写的<authentication mode="Forms"> 
    <forms  loginUrl="~/User/Default.htm" name="SUMINGER" DefaultURL="登陆成功后页面"></forms> 
    </authentication>
      

  4.   

     Response.Redirect("~/index.aspx"); 
    路径对吗