我目前正在ASP.NET中制作自定义登录。我已修改了登录控件的代码,以使用我的数据库而不是Aspnet表。以下是我的代码示例:
using System;
using System.Data;
using System.Configuration;
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)
{
}
// Custom login control
protected void Login1_Authenticate(object sender, AuthenticateEventArgs e)
{
try
{
string uname = Login1.UserName.Trim();
string password = Login1.Password.Trim();
bool flag = AuthenticateUser(uname, password);
if (flag == true)
{
e.Authenticated = true;
Login1.DestinationPageUrl = "Default.aspx";
}
else
e.Authenticated = false;
}
catch (Exception)
{
e.Authenticated = false;
}
}
private bool AuthenticateUser(string uname, string password)
{
bool bflag = false;
string connString = "Server=DEVSERVER;User ID=sa;Password=whatpassword;Database=CommonUser";
string connstring2 = "Server=DEVSERVER;User ID=sa;Password=whatpassword;Database=Admins";
string strSQL = "Select * from dbo.Users where Username ='" + uname + "' and Password ='" + password + "'";
DataSet userDS = new DataSet();
SqlConnection m_conn;
SqlDataAdapter m_dataAdapter;
SqlCommand m_Command;
try
{
m_conn = new SqlConnection(connString);
m_conn.Open();
m_dataAdapter = new SqlDataAdapter(strSQL, m_conn);
m_dataAdapter.Fill(userDS);
m_conn.Close();
}
catch (Exception)
{
userDS = null;
}
if (userDS != null)
{
if (userDS.Tables[0].Rows.Count > 0)
bflag = true;
}
return bflag;
}
}
我有一个用于管理员用户的另一个数据库。那么我的问题是如何使其检查管理员用户的数据库。此外,如何限制普通用户访问某些页面,例如~Admin/AdminPages.aspx?我目前正在尝试弄清楚这个。
非常感谢任何帮助 ;)
提前致谢