日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Asp.net读取AD域信息的方法(一)

發(fā)布時間:2025/6/15 编程问答 22 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Asp.net读取AD域信息的方法(一) 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

1、首先新建一個頁面(Login.aspx):

前臺代碼(Login.aspx):

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Login.aspx.cs" Inherits="Login" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
??? <title>無標題頁</title>
</head>
<body>
??? <form id="Login" method="post" runat="server">
????? <asp:Label ID="Label1" runat="server" >Domain:</asp:Label>
????? <asp:TextBox ID="txtDomain" runat="server" ></asp:TextBox><br>????
????? <asp:Label ID="Label2" runat="server" >Username:</asp:Label>
????? <asp:TextBox ID="txtUsername" runat="server" ></asp:TextBox><br>
????? <asp:Label ID="Label3" runat="server" >Password:</asp:Label>
????? <asp:TextBox ID="txtPassword" runat="server" TextMode=Password></asp:TextBox><br>
????? <asp:Button ID="btnLogin" runat="server" Text="Login" OnClick="Login_Click"></asp:Button><br>
????? <asp:Label ID="errorLabel" runat="server" ForeColor=#ff3300></asp:Label><br>
????? <asp:CheckBox ID="chkPersist" runat="server" Text="Persist Cookie" />
??? </form>

</body>
</html>

后臺代碼(Login.aspx.cs):

using System;
using System.Data;
using System.Configuration;
using System.Collections;
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;

public partial class Login : System.Web.UI.Page
{
??? protected void Page_Load(object sender, EventArgs e)
??? {

??? }
??? protected void Login_Click(object sender, EventArgs e)
??? {
??????? string adPath = "LDAP://" + txtDomain.Text;

??????? LdapAuthentication adAuth = new LdapAuthentication(adPath);
??????? try
??????? {
??????????? if (true == adAuth.IsAuthenticated(txtDomain.Text, txtUsername.Text, txtPassword.Text))
??????????? {
??????????????? string groups = adAuth.GetGroupByUser();
????????????????

????????????????//Create the ticket, and add the groups.
??????????????? bool isCookiePersistent = chkPersist.Checked;
??????????????? FormsAuthenticationTicket authTicket = new FormsAuthenticationTicket(1,
????????????????????????? txtUsername.Text, DateTime.Now, DateTime.Now.AddMinutes(60), isCookiePersistent, groups);

????????????????//Encrypt the ticket.
??????????????? string encryptedTicket = FormsAuthentication.Encrypt(authTicket);

??????????????? //Create a cookie, and then add the encrypted ticket to the cookie as data.
??????????????? HttpCookie authCookie = new HttpCookie(FormsAuthentication.FormsCookieName, encryptedTicket);

??????????????? if (true == isCookiePersistent)
??????????????????? authCookie.Expires = authTicket.Expiration;

????????????????//Add the cookie to the outgoing cookies collection.
??????????????? Response.Cookies.Add(authCookie);

????????????????//You can redirect now.
??????????????? Response.Redirect(FormsAuthentication.GetRedirectUrl(txtUsername.Text, false));
??????????? }
??????????? else
??????????? {
??????????????? errorLabel.Text = "Authentication did not succeed. Check user name and password.";
??????????? }
??????? }
??????? catch (Exception ex)
??????? {
??????????? errorLabel.Text = "Error authenticating. " + ex.Message;
??????? }
??? }
}
2、在Global.asax里寫:

?void Application_AuthenticateRequest(object sender, EventArgs e)
??? {
??????? string cookieName = FormsAuthentication.FormsCookieName;
??????? HttpCookie authCookie = Context.Request.Cookies[cookieName];

??????? if (null == authCookie)
??????? {
????????????//There is no authentication cookie.
??????????? return;
??????? }
??????? FormsAuthenticationTicket authTicket = null;
??????? try
??????? {
??????????? authTicket = FormsAuthentication.Decrypt(authCookie.Value);
??????? }
??????? 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 = authTicket.UserData.Split(new char[] { '|' });
????????//Create an Identity.
??????? System.Security.Principal.GenericIdentity id = new System.Security.Principal.GenericIdentity(authTicket.Name, "LdapAuthentication");
????????//This principal flows throughout the request.
??????? System.Security.Principal.GenericPrincipal principal = new System.Security.Principal.GenericPrincipal(id, groups);
??????? Context.User = principal;
??? }

3、在Web.Config里加上:

<authentication mode="Forms">
???<forms loginUrl="Login.aspx" name="adAuthCookie" timeout="10" path="/">
???</forms>
??</authentication>
??<authorization>
???<deny users="?"/>
???<allow users="*"/>
??</authorization>
<identity impersonate="true"/>

?

4、再新建一個頁面(WebForm1.aspx):

前臺代碼(WebForm1.aspx):

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="WebForm1.aspx.cs" Inherits="WebForm1" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
??? <title>無標題頁</title>
</head>
<body>
??? <form id="Form1" method="post" runat="server">
????? <asp:Label ID="lblName" runat="server" /><br/>
????? <asp:Label ID="lblAuthType" runat="server" />
??? </form>
</body>
</html>

后臺代碼(WebForm1.aspx.cs):

using System;
using System.Data;
using System.Configuration;
using System.Collections;
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;

public partial class WebForm1 : System.Web.UI.Page
{
??? protected void Page_Load(object sender, EventArgs e)
??? {
??????? lblName.Text = "Hello " + Context.User.Identity.Name + ".";
??????? lblAuthType.Text = "You were authenticated using " + Context.User.Identity.AuthenticationType + ".";
??? }
}


5、再新建一個類(對域的操作主要都要這個類里):

LdapAuthentication.cs:

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.DirectoryServices;
using System.Text;

/// <summary>
/// LdapAuthentication?的摘要說明
/// </summary>
public class?LdapAuthentication
{
??? public LdapAuthentication()
??? {
??? }
??? private string _path;
??? private string _filterAttribute;

??? public LdapAuthentication(string path)
??? {
??????? _path = path;
??? }

??? /// <summary>
??? ///?判斷是否域用戶
??? /// </summary>
??? /// <param name="domain">域名</param>
??? /// <param name="username">用戶名</param>
??? /// <param name="pwd">密碼</param>
??? /// <returns></returns>
??? 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 = entry.NativeObject;

??????????? DirectorySearcher search = new DirectorySearcher(entry);

??????????? search.Filter = "(SAMAccountName=" + username + ")";
??????????? search.PropertiesToLoad.Add("cn");
??????????? SearchResult result = search.FindOne();
??????????? if (null == result)
??????????? {
??????????????? return false;
??????????? }
??????????? //Update the new path to the user in the directory.
??????????? _path = result.Path;
??????????? _filterAttribute = (string)result.Properties["cn"][0];
??????? }
??????? catch (Exception ex)
??????? {
??????????? throw new Exception("Error authenticating user. " + ex.Message);
??????? }
??????? return true;
??? }

??? /// <summary>
??? ///?根據(jù)用戶名獲取所屬組名
??? /// </summary>
??? /// <returns></returns>
??? public string GetGroupByUser()
??? {
??????? DirectorySearcher search = new DirectorySearcher(_path);
??????? search.Filter = "(cn=" + _filterAttribute + ")";
??????? search.PropertiesToLoad.Add("memberOf");
??????? StringBuilder groupNames = new StringBuilder();

??????? try
??????? {
??????????? SearchResult result = search.FindOne();
??????????? int propertyCount = result.Properties["memberOf"].Count;
??????????? string dn;
??????????? int equalsIndex, commaIndex;

??????????? for (int propertyCounter = 0; propertyCounter < propertyCount; propertyCounter++)
??????????? {
??????????????? dn = (string)result.Properties["memberOf"][propertyCounter];
??????????????? equalsIndex = dn.IndexOf("=", 1);
??????????????? commaIndex = dn.IndexOf(",", 1);
??????????????? if (-1 == equalsIndex)
??????????????? {
??????????????????? return null;
??????????????? }
??????????????? groupNames.Append(dn.Substring((equalsIndex + 1), (commaIndex - equalsIndex) - 1));
??????????? }
??????? }
??????? catch (Exception ex)
??????? {
??????????? throw new Exception("Error obtaining group names. " + ex.Message);
??????? }
??????? return groupNames.ToString();
??? }

??? /// <summary>
??? ///?獲取組用戶
??? /// </summary>
??? /// <param name="Groupname">組名</param>
??? /// <returns></returns>
??? public string[] GetUsersForGroup(string Groupname)
??? {
??????? DirectorySearcher ds = new DirectorySearcher(_path);
??????? ds.Filter = "(&(objectClass=group)(cn=" + Groupname + "))";
??????? ds.PropertiesToLoad.Add("member");
??????? SearchResult r = ds.FindOne();

??????? if (r.Properties["member"] == null)
??????? {
??????????? return (null);
??????? }

??????? string[] results = new string[r.Properties["member"].Count];
??????? for (int i = 0; i < r.Properties["member"].Count; i++)
??????? {
??????????? string theGroupPath = r.Properties["member"][i].ToString();
??????????? results[i] = theGroupPath.Substring(3, theGroupPath.IndexOf(",") - 3);
??????? }
??????? return (results);
??? }

??? /// <summary>
??? ///?獲取用戶所屬組
??? /// </summary>
??? /// <param name="username">用戶名</param>
??? /// <returns></returns>
??? public string[] GetGroupsForUser(string username)
??? {
??????? DirectorySearcher ds = new DirectorySearcher(_path);
??????? ds.Filter = "(&(sAMAccountName=" + username + "))";
??????? ds.PropertiesToLoad.Add("memberof");
??????? SearchResult r = ds.FindOne();

??????? if (r.Properties["memberof"].Count == 0)
??????? {
??????????? return (null);
??????? }

??????? string[] results = new string[r.Properties["memberof"].Count];
??????? for (int i = 0; i < r.Properties["memberof"].Count; i++)
??????? {
??????????? string theGroupPath = r.Properties["memberof"][i].ToString();
??????????? results[i] = theGroupPath.Substring(3, theGroupPath.IndexOf(",") - 3);
??????? }
??????? return (results);
??? }

??? public string[] GetAllGroupsForUser(string username)
??? {
??????? DirectorySearcher ds = new DirectorySearcher(_path);
??????? ds.Filter = "(&(sAMAccountName=" + username + "))";
??????? ds.PropertiesToLoad.Add("memberof");
??????? SearchResult r = ds.FindOne();
??????? if (r.Properties["memberof"] == null)
??????? {
??????????? return (null);
??????? }
??????? string[] results = new string[r.Properties["memberof"].Count + 1];
??????? for (int i = 0; i < r.Properties["memberof"].Count; i++)
??????? {
??????????? string theGroupPath = r.Properties["memberof"][i].ToString();
??????????? results[i] = theGroupPath.Substring(3, theGroupPath.IndexOf(",") - 3);
??????? }
??????? results[r.Properties["memberof"].Count] = "All";//All組屬于任何人,在AD之外定義了一個組,以便分配用戶權限
??????? return (results);
??? }

??? /// <summary>
??? ///?獲取組用戶
??? /// </summary>
??? /// <param name="username">用戶名</param>
??? /// <returns></returns>

??? public string GetUserDisplayName(string username)
??? {
??????? string results;
??????? DirectorySearcher ds = new DirectorySearcher(_path);
??????? ds.Filter = "(&(objectClass=user)(sAMAccountName=" + username + "))";
??????? ds.PropertiesToLoad.Add("DisplayName");
??????? SearchResult r = ds.FindOne();
??????? results = r.GetDirectoryEntry().InvokeGet("DisplayName").ToString();
??????? return (results);

??? }

?

??? public string GetAdGroupDescription(string prefix)//根據(jù)CN獲取組description
??? {
??????? string results;
??????? DirectorySearcher groupsDS = new DirectorySearcher(_path);
??????? groupsDS.Filter = "(&(objectClass=group)(CN=" + prefix + "*))";
??????? groupsDS.PropertiesToLoad.Add("cn");
??????? SearchResult sr = groupsDS.FindOne();
??????? results = sr.GetDirectoryEntry().InvokeGet("description").ToString();
??????? return (results);
??? }

??? public DataTable GetAdGroupInfo()//根據(jù)CN獲取組信息
??? {
??????? DataTable dt = new DataTable();
??????? dt.Columns.Add("URL", typeof(System.String));
??????? dt.Columns.Add("cn", typeof(System.String));
??????? dt.Columns.Add("Description", typeof(System.String));

??????? DirectorySearcher searcher = new DirectorySearcher(_path);

??????? searcher.Filter = "(&(objectClass=group))";
??????? //searcher.SearchScope = SearchScope.Subtree;
??????? //searcher.Sort = new SortOption("description", System.DirectoryServices.SortDirection.Ascending);
??????? searcher.PropertiesToLoad.AddRange(new string[] { "cn", "description" });
??????? SearchResultCollection results = searcher.FindAll();
??????? if (results.Count == 0)
??????? {
??????????? return (null);
??????? }
??????? else
??????? {
??????????? foreach (SearchResult result in results)
??????????? {
??????????????? DataRow dr = dt.NewRow();
??????????????? dr[0] = result.Path.ToString();
??????????????? dr[1] = result.GetDirectoryEntry().InvokeGet("cn").ToString();
??????????????? if (result.GetDirectoryEntry().InvokeGet("Description") != null)
??????????????????? dr[2] = result.GetDirectoryEntry().InvokeGet("Description").ToString();
??????????????? else
??????????????????? dr[2] = result.GetDirectoryEntry().InvokeGet("cn").ToString();
??????????????? dt.Rows.Add(dr);
??????????? }
??????????? dt.DefaultView.Sort = "description ASC";
??????????? return dt;
??????? }

??? }

??? public string getAccountName(string cn) //根據(jù)CN獲取登陸名
??? {
??????? foreach (string path in _path.Split(','))
??????? {
??????????? DirectorySearcher ds = new DirectorySearcher(path);
??????????? ds.Filter = "(&(objectClass=user)(cn=*" + cn + "*))";
??????????? ds.PropertiesToLoad.Add("sAMAccountName");
??????????? SearchResult r = ds.FindOne();
??????????? if (r != null)
??????????????? return r.GetDirectoryEntry().InvokeGet("sAMAccountName").ToString();
??????? }
??????? return null;
??? }

?????? public DataTable adUserlist(string groupname)?? //生成用戶數(shù)據(jù)表
??? {
??????? DataTable dt = new DataTable();
??????? dt.Columns.Add("cn", typeof(System.String));
??????? dt.Columns.Add("sAMAccountName", typeof(System.String));
??????? string[] groupmember = GetUsersForGroup(groupname);
??????? if (groupmember.Length == 0)
??????? {
??????????? return null;
??????? }
??????? else
??????? {
??????????? foreach (string member in groupmember)
??????????? {
??????????????? if (IsAccountActive(getAccountControl(getAccountName(member))))
??????????????? {
??????????????????? DataRow dr = dt.NewRow();
??????????????????? dr[0] = member.ToString();
??????????????????? dr[1] = getAccountName(member);
??????????????????? dt.Rows.Add(dr);
??????????????? }
??????????? }
??????????? return dt;

??????? }
??? }

?

?public DataTable adUserlist()???//生成指定的用戶信息數(shù)據(jù)表
??? {
??????? DataTable dt = new DataTable();
??????? dt.Columns.Add("memberof", typeof(System.String));
??????? dt.Columns.Add("cn", typeof(System.String));
??????? dt.Columns.Add("Description", typeof(System.String));
??????? dt.Columns.Add("name", typeof(System.String));
??????? dt.Columns.Add("Mail", typeof(System.String));
??????? dt.Columns.Add("samaccountname", typeof(System.String));
??????? dt.Columns.Add("whencreated", typeof(System.String));
??????? dt.Columns.Add("title", typeof(System.String));
??????? dt.Columns.Add("department", typeof(System.String));
??????? DirectorySearcher searcher = new DirectorySearcher(_path);
??????? //searcher.Filter = "(description=ADPJ*)";
??????? searcher.Filter = "(description=ADPL*)";
??????? searcher.PropertiesToLoad.AddRange(new string[] { "memberof", "cn", "description", "name", "Mail", "samaccountname", "whencreated", "title", "department"});
??????? SearchResultCollection results = searcher.FindAll();

??????? if (results.Count == 0)
??????? {
??????????? return (null);
??????? }
??????? else
??????? {
??????????? foreach (SearchResult result in results)
??????????? {

??????????????? DataRow dr = dt.NewRow();
??????????????? //dr[0] = result.Path.ToString();
??????????????? if (result.GetDirectoryEntry().InvokeGet("memberof") != null)
??????????????????? dr[0] = result.GetDirectoryEntry().InvokeGet("memberof").ToString();
??????????????? else
??????????????????? dr[0] = "";
??????????????? if (result.GetDirectoryEntry().InvokeGet("cn") != null)
??????????????????? dr[1] = result.GetDirectoryEntry().InvokeGet("cn").ToString();
??????????????? else
??????????????????? dr[1] = "";

??????????????? if (result.GetDirectoryEntry().InvokeGet("Description") != null)
??????????????????? dr[2] = result.GetDirectoryEntry().InvokeGet("Description").ToString();
??????????????? else
??????????????????? dr[2] = result.GetDirectoryEntry().InvokeGet("cn").ToString();
??????????????? if (result.GetDirectoryEntry().InvokeGet("name") != null)
??????????????????? dr[3] = result.GetDirectoryEntry().InvokeGet("name").ToString();
??????????????? else
??????????????????? dr[3] = "";
??????????????? if (result.GetDirectoryEntry().InvokeGet("Mail") != null)
??????????????????? dr[4] = result.GetDirectoryEntry().InvokeGet("Mail").ToString();
??????????????? else
??????????????????? dr[4] = "";
??????????????? if (result.GetDirectoryEntry().InvokeGet("samaccountname") != null)
??????????????????? dr[5] = result.GetDirectoryEntry().Properties["samaccountname"].Value.ToString();
??????????????? else
??????????????????? dr[5] = "";
??????????????? if (result.GetDirectoryEntry().InvokeGet("whencreated") != null)
??????????????????? dr[6] = result.GetDirectoryEntry().Properties["whencreated"].Value.ToString();
??????????????? else
??????????????????? dr[6] = "";

????????????????if (result.GetDirectoryEntry().InvokeGet("title") != null)
??????????????????? dr[7] = result.GetDirectoryEntry().Properties["title"].Value.ToString();
??????????????? else
??????????????????? dr[7] = "";
??????????????? if (result.GetDirectoryEntry().InvokeGet("department") != null)
??????????????????? dr[8] = result.GetDirectoryEntry().Properties["department"].Value.ToString();
??????????????? else
??????????????????? dr[8] = "";

??????????????? dt.Rows.Add(dr);
??????????? }
??????????? dt.DefaultView.Sort = "description ASC";
??????????? return dt;
??????? }
??? }

??? public void adUserlistbox(ListBox results, string groupName)? //生成USER
??? {
??????? results.Items.Clear();
??????? DataTable dt = adUserlist(groupName);
??????? if (dt != null)
??????? {
??????????? results.DataSource = dt;
??????????? results.DataTextField = dt.Columns[0].Caption;
??????????? results.DataValueField = dt.Columns[1].Caption;
??????????? results.DataBind();
??????? }
??? }

??? public void adGrouplistbox(ListBox results)
??? {
??????? results.Items.Clear();
??????? DataTable dt = GetAdGroupInfo();
??????? DataRow dr = dt.NewRow();
??????? dr[1] = "All";
??????? dr[2] = "All";
??????? dt.Rows.Add(dr);
??????? results.DataSource = dt;
??????? results.DataTextField = dt.Columns[2].Caption;
??????? results.DataValueField = dt.Columns[1].Caption;
??????? results.DataBind();
??? }

??? public void aduserGrouplist(DropDownList results)
??? {
??????? results.Items.Clear();
??????? DataTable dt = GetAdGroupInfo();
??????? results.DataSource = dt;
??????? results.DataTextField = dt.Columns[2].Caption;
??????? results.DataValueField = dt.Columns[1].Caption;
??????? results.DataBind();
??? }

??? public int getAccountControl(string accountName)//獲取權限碼
??? {
??????? int results;
??????? DirectorySearcher ds = new DirectorySearcher(_path);
??????? ds.Filter = "(&(objectClass=user)(sAMAccountName=" + accountName + "))";
??????? ds.PropertiesToLoad.Add("userAccountControl");
??????? try
??????? {
??????????? SearchResult r = ds.FindOne();
??????????? results = Convert.ToInt32(r.GetDirectoryEntry().InvokeGet("userAccountControl"));
??????????? return results;
??????? }
??????? catch
??????? {
??????????? return 0;
??????? }

??? }

??? public bool IsAccountActive(int userAccountControl)//判斷是否有效
??? {
??????? int ADS_UF_ACCOUNTDISABLE = 0X0002;
??????? int userAccountControl_Disabled = Convert.ToInt32(ADS_UF_ACCOUNTDISABLE);
??????? int flagExists = userAccountControl & userAccountControl_Disabled;
??????? if (flagExists > 0)
??????????? return false;
??????? else
??????????? return true;
??? }

??? public DirectoryEntry GetDirectoryEntryByAccount(string sAMAccountName)
??? {
??????? DirectorySearcher deSearch = new DirectorySearcher(_path);
??????? deSearch.Filter = "(&(objectCategory=person)(objectClass=user)(sAMAccountName=" + sAMAccountName + "))";
??????? // deSearch.SearchScope = SearchScope.Subtree;

??????? try
??????? {
??????????? SearchResult result = deSearch.FindOne();
??????????? if (result == null)
??????????? { return null; }
??????????? DirectoryEntry de = new DirectoryEntry(_path);
??????????? return de;
??????? }
??????? catch
??????? {
??????????? //throw;
??????????? return null;
??????? }
??? }

?}

另外增加一個讀取用戶信息的方法:
??? ///?? <summary>?
??? ///???讀取AD用戶信息?
??? ///?? </summary>?
??? ///?? <param?? name= "ADUsername ">?用戶?</param>?
??? ///?? <param?? name= "ADPassword ">?密碼?</param>?
??? ///?? <param?? name= "domain ">?域名?</param>?
??? ///?? <returns> </returns>?
??? public System.Collections.SortedList AdUserInfo(string ADUsername, string ADPassword, string domain)
??? {
??????? System.DirectoryServices.DirectorySearcher src;
??????? string ADPath = "LDAP:// " + domain;//?? "ou=總公司,DC=abc,DC=com,DC=cn ";?? + ",ou=總公司?"?
??????? System.Collections.SortedList sl = new System.Collections.SortedList();
??????? string domainAndUsername = domain + @"\" + ADUsername;
??????? System.DirectoryServices.DirectoryEntry de = new System.DirectoryServices.DirectoryEntry(ADPath, domainAndUsername, ADPassword);

src = new System.DirectoryServices.DirectorySearcher(de);
??????? src.Filter = "(SAMAccountName=" + ADUsername + ")";
??????? src.PageSize = 10000;//???此參數(shù)可以任意設置,但不能不設置,如不設置讀取AD數(shù)據(jù)為0~999條數(shù)據(jù),設置后可以讀取大于1000條數(shù)據(jù)。?
??????? //?? src.SizeLimit?? =?? 2000;?
??????? src.SearchScope = System.DirectoryServices.SearchScope.Subtree;
??????? try
??????? {
??????????? foreach (System.DirectoryServices.SearchResult res in src.FindAll())??

??????????? {

????????????????? sl.Add(res.GetDirectoryEntry().Properties["Name"].Value, res.GetDirectoryEntry().InvokeGet("Description"));

?????????????? }
??????? }
??????? catch (Exception ex)
??????? {
??????????? throw new Exception("Get?? Ad?? Info ", ex);
??????? }
??????? return sl;
??? }

下面列舉一些域中存在的屬性:

distinguishedname;objectclass;pwdlastset;userprincipalname與mail一樣;memberof;objectguid;instancetype;codepage;whenchanged;samaccountname;cn;usncreated;sn(姓);accountexpires;usnchanged;displayname;description;useraccountcontrol(權限碼);whencreated;givenName(名);samaccounttype;objectcategory;countrycode;primarygroupid;objectsid;title(職務);department(部門)

?備注:

????????????????? 1. ??在ASP.NET中專用屬性:???
? ? ? ? ? ??  獲取服務器電腦名:Page.Server.ManchineName ??
? ? ? ? ? ??  獲取用戶信息:Page.User ??
? ? ? ? ? ??  獲取客戶端電腦名:Page.Request.UserHostName ??
? ? ? ? ? ??  獲取客戶端電腦IP:Page.Request.UserHostAddress ??
? ? ? ? ? ??2. ??在網(wǎng)絡編程中的通用方法:???
? ? ? ? ? ??  獲取當前電腦名:static ? System.Net.Dns.GetHostName() ??
? ? ? ? ? ??  根據(jù)電腦名取出全部IP地址:static ? System.Net.Dns.Resolve(電腦名).AddressList ??
? ? ? ? ? ??  也可根據(jù)IP地址取出電腦名:static ? System.Net.Dns.Resolve(IP地址).HostName ??
? ? ? ? ? ??3. ??系統(tǒng)環(huán)境類的通用屬性:???
? ? ? ? ? ??  當前電腦名:static ? System.Environment.MachineName ??
? ? ? ? ? ??  當前電腦所屬網(wǎng)域:static ? System.Environment.UserDomainName ??
? ? ? ? ? ??  當前電腦用戶:static ? System.Environment.UserName

?

轉載于:https://www.cnblogs.com/ydfq-home/p/5017364.html

總結

以上是生活随笔為你收集整理的Asp.net读取AD域信息的方法(一)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。

主站蜘蛛池模板: 日日干夜夜撸 | 久久激情久久 | 日日日网站 | 日韩在观看线 | 日本黄色www| 粉嫩av蜜桃av蜜臀av | 一级大片免费看 | 成人影| 天天摸天天舔天天操 | 午夜在线免费观看 | 欧美日韩综合一区二区三区 | 性视频在线播放 | 国产网站精品 | 蜜桃久久精品 | 午夜激情视频在线 | 亚洲av无码专区在线电影 | 黄色在线观看av | 国产精品美女久久久久久久久 | 欧美一区二区三区成人片在线 | 99爱国产 | 99国产免费 | 亚洲欧洲成人精品久久一码二码 | 熟女毛毛多熟妇人妻aⅴ在线毛片 | 51啪影院| 欧美伊人久久 | 日本韩国在线播放 | 久久国产热视频 | 亚洲精品国产suv一区 | 精品女厕偷拍一区二区 | 国产免费视频一区二区三区 | 亚洲av成人无码网天堂 | 天天操天天干天天插 | 视频在线观看91 | 亚洲区一区二区 | 在线国产视频 | 91精品欧美一区二区三区 | 美腿丝袜亚洲色图 | 99久久国产宗和精品1上映 | 嫩模被强到高潮呻吟不断 | 最好看的中文字幕国语电影mv | 欧美mv日韩mv国产网站 | 国产污视频在线观看 | 一区二区免费视频 | av看片资源| 精品福利在线观看 | 亚洲涩涩图 | www日本www| 丹丹的呻吟声1一7 | 91精彩视频在线观看 | 中文字幕在线观看精品 | 亚洲色图 欧美 | 91爱| 久久久亚洲欧洲 | 中文天堂在线播放 | 亚洲av人人澡人人爽人人夜夜 | 亚洲精华国产精华精华液网站 | 在线播放无码后入内射少妇 | 三级全黄做爰在线观看 | 美女18毛片 | 春草| 亚洲综合视频一区 | 激情av综合 | 91精品视频在线看 | 国产精品成人免费 | av午夜在线观看 | 热热99| 国产日本在线观看 | 日本免费网址 | 久久无码人妻一区二区三区 | 日韩av成人 | 天天草夜夜草 | 国模丫头1000人体 | 午夜性影院 | 国产1区二区 | 国产91福利 | 双性受孕h堵精大肚生子 | 一区二区日韩精品 | 93久久精品日日躁夜夜躁欧美 | av女星全部名单 | 看黄色大片| 久久九九热视频 | 亚洲一级免费毛片 | 性福利视频 | 97在线免费视频观看 | 久久久久久一区二区三区 | 高清欧美精品xxxxx在线看 | 99精品久久精品一区二区 | 国产黄色免费网站 | 欧美黑人欧美精品刺激 | 在线观看不卡的av | 777片理伦片在线观看 | 男男做爰猛烈叫床爽爽小说 | 成人福利在线免费观看 | 一区二区三区小视频 | 亚洲综合干| 亚洲激情视频在线观看 | 免费一级a毛片 | 久久午夜场 | 久久偷看各类女兵18女厕嘘嘘 |