本篇内容介绍了“C#怎么实现AD+NT+Normal认证”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!
代码如下:
鉴于此,要使用Forms认证方式
1。web.config: <authentication mode="Forms" >
<forms name="PlatForm" defaultUrl="default.aspx" path="/"></forms>
</authentication>
<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>
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 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);
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);
}
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)
{
DirectoryEntry Ad;
// DirectorySearcher ds;
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
“C#怎么实现AD+NT+Normal认证”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注亿速云网站,小编将为大家输出更多高质量的实用文章!
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。
原文链接:https://blog.51cto.com/amadeus/5741021