鉴于此,要使用Forms认证方式
1。web.config:
<authentication mode="Forms" >
<forms name="PlatForm" defaultUrl="default.aspx" path="/"></forms>
</authentication>
<forms name="PlatForm" defaultUrl="default.aspx" path="/"></forms>
</authentication>
<appSettings>
<add key="LDAP" value="LDAP://server:389/OU=*,DC=*,DC=*"/>
</appSettings>
<add key="LDAP" value="LDAP://server:389/OU=*,DC=*,DC=*"/>
</appSettings>
2。login.aspx:
<form id="form1" runat="server">
<div>
<p> <asp:TextBox ID="txtName" runat="server"></asp:TextBox></p>
<asp:TextBox ID="txtPwd" runat="server" TextMode="Password"></asp:TextBox>
<p><asp:DropDownList ID="drpDomain" runat="server">
<asp:ListItem Selected="True">aa</asp:ListItem>
<asp:ListItem></asp:ListItem>
<asp:ListItem Value="bb">bb</asp:ListItem>
</asp:DropDownList></p>
<asp:Button ID="btnLogin" runat="server" Text="登录" OnClick="btnLogin_Click" />
</div>
</form>
<div>
<p> <asp:TextBox ID="txtName" runat="server"></asp:TextBox></p>
<asp:TextBox ID="txtPwd" runat="server" TextMode="Password"></asp:TextBox>
<p><asp:DropDownList ID="drpDomain" runat="server">
<asp:ListItem Selected="True">aa</asp:ListItem>
<asp:ListItem></asp:ListItem>
<asp:ListItem Value="bb">bb</asp:ListItem>
</asp:DropDownList></p>
<asp:Button ID="btnLogin" runat="server" Text="登录" OnClick="btnLogin_Click" />
</div>
</form>
3.login.aspx.cs
DBAccess db = new DBAccess();
protected void Page_Load(object sender, EventArgs e)
{
//如果default.aspx没有做退出动作
if (Request.QueryString["Flag"] != "Y")
{
//初始化,抓取NT帐号
if (Request.ServerVariables["LOGON_USER"] != "")
{
FormsAuthentication.RedirectFromLoginPage(Request.ServerVariables["LOGON_USER"], true);
}
}
//否则,进行Normal or AD验证
else
{
}
}
protected void Page_Load(object sender, EventArgs e)
{
//如果default.aspx没有做退出动作
if (Request.QueryString["Flag"] != "Y")
{
//初始化,抓取NT帐号
if (Request.ServerVariables["LOGON_USER"] != "")
{
FormsAuthentication.RedirectFromLoginPage(Request.ServerVariables["LOGON_USER"], true);
}
}
//否则,进行Normal or AD验证
else
{
}
}
protected void btnLogin_Click(object sender, EventArgs e)
{
//判断非AD账号
string strSql="select * from users where job=1 and account_ID='"+txtName.Text.Trim()+"' and psWrd='"+txtPwd.Text.Trim()+"'";
DataSet ds=db.GetDataSet(strSql);
//AD验证
if (objData.CheckNTAccount(txtName.Text, txtPwd.Text, drpDomain.SelectedValue))
{
FormsAuthentication.RedirectFromLoginPage(txtName.Text, true);
}
//其他非AD账号
else if(ds.Tables[0].Rows.Count!=0)
{
FormsAuthentication.RedirectFromLoginPage(txtName.Text, true);
}
//如果不能成功登录,显示出错信息
else
{
Response.Write("<script type=text/javascript>alert(\'请输入正确的密码,账号~\');</script>");
}
}
{
//判断非AD账号
string strSql="select * from users where job=1 and account_ID='"+txtName.Text.Trim()+"' and psWrd='"+txtPwd.Text.Trim()+"'";
DataSet ds=db.GetDataSet(strSql);
clsData objData
= new clsData();//AD验证
if (objData.CheckNTAccount(txtName.Text, txtPwd.Text, drpDomain.SelectedValue))
{
FormsAuthentication.RedirectFromLoginPage(txtName.Text, true);
}
//其他非AD账号
else if(ds.Tables[0].Rows.Count!=0)
{
FormsAuthentication.RedirectFromLoginPage(txtName.Text, true);
}
//如果不能成功登录,显示出错信息
else
{
Response.Write("<script type=text/javascript>alert(\'请输入正确的密码,账号~\');</script>");
}
}
4.default.aspx.cs
protected void lbtnLogout_Click(object sender, EventArgs e)
{
string strFlag = "flag=Y";
FormsAuthentication.RedirectToLoginPage(strFlag);
}
{
string strFlag = "flag=Y";
FormsAuthentication.RedirectToLoginPage(strFlag);
}
5.clsData.cs(AD验证)
using System.DirectoryServices; //此为AD验证的控件,一定要加载!!!
/// <summary>
/// clsData 的摘要说明
/// </summary>
public class clsData
{
public clsData()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
public bool CheckNTAccount(string UserName, string Password, string Domain)
{
string strLDAP = System.Configuration.ConfigurationSettings.AppSettings["LDAP"];
{
object obj = Ad.NativeObject; //检查是否可正常登入
return true;
}
{
return false;
}
{
Ad.Dispose();
}
/// <summary>
/// clsData 的摘要说明
/// </summary>
public class clsData
{
public clsData()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
public bool CheckNTAccount(string UserName, string Password, string Domain)
{
DirectoryEntry Ad;
string strLDAP = System.Configuration.ConfigurationSettings.AppSettings["LDAP"];
Ad
= new DirectoryEntry(strLDAP, Domain + "\\" + UserName, Password, AuthenticationTypes.ServerBind);try
{
object obj = Ad.NativeObject; //检查是否可正常登入
return true;
}
catch (Exception)
{
return false;
}
finally
{
Ad.Dispose();
}
}
}
6,DBAccess.cs
详见:
http://www.cnblogs.com/ReadAuthen.aspx?EntryID=922281&ReturnUrl=/vegaslee/archive/2007/10/12/922281.html
相关文章
暂无评论...