using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Data.SqlClient;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class _Default : System.Web.UI.Page
...{
protected void Page_Load(object sender, EventArgs e)
...{
}
private bool SiteLevelCustomAuthenticationMethod(string UserName, string Password)
...{
bool boolReturnValue = false;
// Insert code that implements a site-specific custom
// authentication method here.
// This example implementation always returns false.
string strConnection = "server=.;database=liuwei;uid=sa;pwd=;";
SqlConnection Connection = new SqlConnection(strConnection);
String strSQL = "Select * From liuwei";
SqlCommand command = new SqlCommand(strSQL, Connection);
SqlDataReader Dr;
Connection.Open();
Dr = command.ExecuteReader();
while (Dr.Read())
...{
if ((UserName == Dr["name"].ToString()) & (Password == Dr["Password"].ToString()))
...{
boolReturnValue = true;
}
Dr.Close();
return boolReturnValue;
}
}
protected void Login1_Authenticate(object sender, AuthenticateEventArgs e)
...{
bool Authenticated = false;
Authenticated = SiteLevelCustomAuthenticationMethod(Login1.UserName, Login1.Password);
e.Authenticated = Authenticated;
if (Authenticated == true)
...{
Response.Redirect("Default2.aspx");
}
}
}



本文介绍了一个使用 ASP.NET 实现自定义网站级别的身份验证方法的例子。该方法通过连接到 SQL Server 数据库来验证用户名和密码的有效性。

8836

被折叠的 条评论
为什么被折叠?



