2023年6月21日发(作者:)
域认证操作
一 、验证域类
编写一个域用户验证的类LdapAuthentication【类的名称自己可以定义,这里我们就用LdapAuthentication】
引用命名空间:using oryServices;
public class LdapAuthentication
{
private string _path;
private string _filterAttribute;
public LdapAuthentication(string path)
{
_path = path;
}
public bool IsAuthenticated(string domain, string username, string pwd)
{
string domainAndUsername = domain + @"" + username;
DirectoryEntry entry = new DirectoryEntry(_path, domainAndUsername, pwd);
try
{
//Bind to the native AdsObject to force authentication.
object obj = Object;
DirectorySearcher search = new DirectorySearcher(entry);
= "(SAMAccountName=" + username + ")";
("cn");
SearchResult result = e();
if (null == result)
{
return false;
}
//Update the new path to the user in the directory.
_path = ;
_filterAttribute = (string)ties["cn"][0];
} catch (Exception ex)
{
throw new Exception( e);
}
}
return true;
}
二、配置文件
在文件中找到相应的节修改如下【如果没有找到直接复制粘贴到><>之间】 :
备注:是登录界面,根据自己系统的页面而定
三、 配置域信息
在标签
四、 编写文件
给您的程序添加文件,在其protected void Application_AuthenticateRequest(object
sender, EventArgs e){}事件中添加如下的代码:
string cookieName = ookieName;
HttpCookie authCookie = s[cookieName];
if (null == authCookie)
{
//There is no authentication cookie.
return;
}
FormsAuthenticationTicket authTicket = null;
try
{
authTicket = t();
}
catch (Exception ex)
{
//Write the exception to the Event Log.
return;
}
if (null == authTicket)
{
//Cookie failed to decrypt.
return;
}
//When the ticket was created, the UserData property was assigned a
//pipe-delimited string of group names.
string[] groups = (new char[] { '|' });
//Create an Identity.
GenericIdentity id = new GenericIdentity(, "LdapAuthentication");
//This principal flows throughout the request.
GenericPrincipal principal = new GenericPrincipal(id, groups);
= principal;
备注: LdapAuthentication跟你编写的验证类一样的名称
五、 设置登录验证
在登录的Click事件中添加如下的代码就行 :
string adPath = tings["ADPath"].ToString();
string domain = tings["Domain"].ToString();
LdapAuthentication adAuth = new LdapAuthentication(adPath);
try
{
string LoginName = ;
string Password = ;
//AD认证
if (true == enticated(domain, LoginName,Password)) {
//string groups = ups();
//Create the ticket, and add the groups.
bool isCookiePersistent = d;
FormsAuthenticationTicket authTicket = new FormsAuthenticationTicket(1,
ame, , utes(60),
isCookiePersistent, "");
//Encrypt the ticket.
string encryptedTicket = t(authTicket);
//Create a cookie, and then add the encrypted ticket to the cookie as data.
HttpCookie authCookie = new HttpCookie(ookieName,
encryptedTicket);
if (true == isCookiePersistent)
{
s = tion;
}
(authCookie); //Add the cookie to the outgoing cookies
collection.
//权限认证
//权限认证根据自己系统的设定而定,通过后跳转到相关的页面即可
}
else
{
= "登录失败.";
}
2023年6月21日发(作者:)
域认证操作
一 、验证域类
编写一个域用户验证的类LdapAuthentication【类的名称自己可以定义,这里我们就用LdapAuthentication】
引用命名空间:using oryServices;
public class LdapAuthentication
{
private string _path;
private string _filterAttribute;
public LdapAuthentication(string path)
{
_path = path;
}
public bool IsAuthenticated(string domain, string username, string pwd)
{
string domainAndUsername = domain + @"" + username;
DirectoryEntry entry = new DirectoryEntry(_path, domainAndUsername, pwd);
try
{
//Bind to the native AdsObject to force authentication.
object obj = Object;
DirectorySearcher search = new DirectorySearcher(entry);
= "(SAMAccountName=" + username + ")";
("cn");
SearchResult result = e();
if (null == result)
{
return false;
}
//Update the new path to the user in the directory.
_path = ;
_filterAttribute = (string)ties["cn"][0];
} catch (Exception ex)
{
throw new Exception( e);
}
}
return true;
}
二、配置文件
在文件中找到相应的节修改如下【如果没有找到直接复制粘贴到><>之间】 :
备注:是登录界面,根据自己系统的页面而定
三、 配置域信息
在标签
四、 编写文件
给您的程序添加文件,在其protected void Application_AuthenticateRequest(object
sender, EventArgs e){}事件中添加如下的代码:
string cookieName = ookieName;
HttpCookie authCookie = s[cookieName];
if (null == authCookie)
{
//There is no authentication cookie.
return;
}
FormsAuthenticationTicket authTicket = null;
try
{
authTicket = t();
}
catch (Exception ex)
{
//Write the exception to the Event Log.
return;
}
if (null == authTicket)
{
//Cookie failed to decrypt.
return;
}
//When the ticket was created, the UserData property was assigned a
//pipe-delimited string of group names.
string[] groups = (new char[] { '|' });
//Create an Identity.
GenericIdentity id = new GenericIdentity(, "LdapAuthentication");
//This principal flows throughout the request.
GenericPrincipal principal = new GenericPrincipal(id, groups);
= principal;
备注: LdapAuthentication跟你编写的验证类一样的名称
五、 设置登录验证
在登录的Click事件中添加如下的代码就行 :
string adPath = tings["ADPath"].ToString();
string domain = tings["Domain"].ToString();
LdapAuthentication adAuth = new LdapAuthentication(adPath);
try
{
string LoginName = ;
string Password = ;
//AD认证
if (true == enticated(domain, LoginName,Password)) {
//string groups = ups();
//Create the ticket, and add the groups.
bool isCookiePersistent = d;
FormsAuthenticationTicket authTicket = new FormsAuthenticationTicket(1,
ame, , utes(60),
isCookiePersistent, "");
//Encrypt the ticket.
string encryptedTicket = t(authTicket);
//Create a cookie, and then add the encrypted ticket to the cookie as data.
HttpCookie authCookie = new HttpCookie(ookieName,
encryptedTicket);
if (true == isCookiePersistent)
{
s = tion;
}
(authCookie); //Add the cookie to the outgoing cookies
collection.
//权限认证
//权限认证根据自己系统的设定而定,通过后跳转到相关的页面即可
}
else
{
= "登录失败.";
}
发布评论