一区二区三区日韩精品-日韩经典一区二区三区-五月激情综合丁香婷婷-欧美精品中文字幕专区

分享

C# 域用戶操作(轉(zhuǎn)) - WEBBER - 博客園

 ZeroLuo 2011-04-19

C# 域用戶操作(轉(zhuǎn))

 轉(zhuǎn)自:http://www./u/92/20081406091447.html
using System;
using System.DirectoryServices;
 
namespace SystemFrameworks.Helper
{
     ///
     ///活動(dòng)目錄輔助類。封裝一系列活動(dòng)目錄操作相關(guān)的方法。
     ///
     public sealed class ADHelper
     {
         ///
         ///域名
         ///
         private static string DomainName = "MyDomain";
         ///
         /// LDAP 地址
         ///
         private static string LDAPDomain = "DC=MyDomain,DC=local";
         ///
         /// LDAP綁定路徑
         ///
         private static string ADPath = "LDAP://brooks.mydomain.local";
         ///
         ///登錄賬號(hào)
         ///
         private static string ADUser = "Administrator";
         ///
         ///登錄密碼
         ///
         private static string ADPassword = "password";
         ///
         ///扮演類實(shí)例
         ///
         private static IdentityImpersonation impersonate = new IdentityImpersonation(ADUser, ADPassword, DomainName);
 
         ///
         ///用戶登錄驗(yàn)證結(jié)果
         ///
         public enum LoginResult
         {
              ///
              ///正常登錄
              ///
              LOGIN_USER_OK = 0,
              ///
              ///用戶不存在
              ///
              LOGIN_USER_DOESNT_EXIST,
              ///
              ///用戶賬號(hào)被禁用
              ///
              LOGIN_USER_ACCOUNT_INACTIVE,
              ///
              ///用戶密碼不正確
              ///
              LOGIN_USER_PASSWORD_INCORRECT
         }
 
         ///
         ///用戶屬性定義標(biāo)志
         ///
         public enum ADS_USER_FLAG_ENUM
         {
              ///
              ///登錄腳本標(biāo)志。如果通過(guò) ADSI LDAP 進(jìn)行讀或?qū)懖僮鲿r(shí),該標(biāo)志失效。如果通過(guò) ADSI WINNT,該標(biāo)志為只讀。
              ///
              ADS_UF_SCRIPT = 0X0001,
              ///
              ///用戶賬號(hào)禁用標(biāo)志
              ///
              ADS_UF_ACCOUNTDISABLE = 0X0002,
              ///
              ///主文件夾標(biāo)志
              ///
              ADS_UF_HOMEDIR_REQUIRED = 0X0008,
              ///
              ///過(guò)期標(biāo)志
              ///
              ADS_UF_LOCKOUT = 0X0010,
              ///
              ///用戶密碼不是必須的
              ///
              ADS_UF_PASSWD_NOTREQD = 0X0020,
              ///
              ///密碼不能更改標(biāo)志
              ///
              ADS_UF_PASSWD_CANT_CHANGE = 0X0040,
              ///
              ///使用可逆的加密保存密碼
              ///
              ADS_UF_ENCRYPTED_TEXT_PASSWORD_ALLOWED = 0X0080,
              ///
              ///本地賬號(hào)標(biāo)志
              ///
              ADS_UF_TEMP_DUPLICATE_ACCOUNT = 0X0100,
              ///
              ///普通用戶的默認(rèn)賬號(hào)類型
              ///
              ADS_UF_NORMAL_ACCOUNT = 0X0200,
              ///
              ///跨域的信任賬號(hào)標(biāo)志
              ///
              ADS_UF_INTERDOMAIN_TRUST_ACCOUNT = 0X0800,
              ///
              ///工作站信任賬號(hào)標(biāo)志
              ///
              ADS_UF_WORKSTATION_TRUST_ACCOUNT = 0x1000,
              ///
              ///服務(wù)器信任賬號(hào)標(biāo)志
              ///
              ADS_UF_SERVER_TRUST_ACCOUNT = 0X2000,
              ///
              ///密碼永不過(guò)期標(biāo)志
              ///
              ADS_UF_DONT_EXPIRE_PASSWD = 0X10000,
              ///
              /// MNS 賬號(hào)標(biāo)志
              ///
              ADS_UF_MNS_LOGON_ACCOUNT = 0X20000,
              ///
              ///交互式登錄必須使用智能卡
              ///
              ADS_UF_SMARTCARD_REQUIRED = 0X40000,
              ///
              ///當(dāng)設(shè)置該標(biāo)志時(shí),服務(wù)賬號(hào)(用戶或計(jì)算機(jī)賬號(hào))將通過(guò) Kerberos 委托信任
              ///
              ADS_UF_TRUSTED_FOR_DELEGATION = 0X80000,
              ///
              ///當(dāng)設(shè)置該標(biāo)志時(shí),即使服務(wù)賬號(hào)是通過(guò) Kerberos 委托信任的,敏感賬號(hào)不能被委托
              ///
              ADS_UF_NOT_DELEGATED = 0X100000,
              ///
              ///此賬號(hào)需要 DES 加密類型
              ///
              ADS_UF_USE_DES_KEY_ONLY = 0X200000,
              ///
              ///不要進(jìn)行 Kerberos 預(yù)身份驗(yàn)證
              ///
              ADS_UF_DONT_REQUIRE_PREAUTH = 0X4000000,
              ///
              ///用戶密碼過(guò)期標(biāo)志
              ///
              ADS_UF_PASSWORD_EXPIRED = 0X800000,
              ///
              ///用戶賬號(hào)可委托標(biāo)志
              ///
              ADS_UF_TRUSTED_TO_AUTHENTICATE_FOR_DELEGATION = 0X1000000
         }
 
         public ADHelper()
         {
              //
         }
 
         #region GetDirectoryObject
 
         ///
         ///獲得DirectoryEntry對(duì)象實(shí)例,以管理員登陸AD
         ///
         ///
         private static DirectoryEntry GetDirectoryObject()
         {
              DirectoryEntry entry = new DirectoryEntry(ADPath, ADUser, ADPassword, AuthenticationTypes.Secure);
              return entry;
         }
 
         ///
         ///根據(jù)指定用戶名和密碼獲得相應(yīng)DirectoryEntry實(shí)體
         ///
         ///
         ///
         ///
         private static DirectoryEntry GetDirectoryObject(string userName, string password)
         {
              DirectoryEntry entry = new DirectoryEntry(ADPath, userName, password, AuthenticationTypes.None);
              return entry;
         }
 
         ///
         /// i.e. /CN=Users,DC=creditsights, DC=cyberelves, DC=Com
         ///
         ///
         ///
         private static DirectoryEntry GetDirectoryObject(string domainReference)
         {
              DirectoryEntry entry = new DirectoryEntry(ADPath + domainReference, ADUser, ADPassword, AuthenticationTypes.Secure);
              return entry;
         }
 
         ///
         ///獲得以UserName,Password創(chuàng)建的DirectoryEntry
         ///
         ///
         ///
         ///
         ///
         private static DirectoryEntry GetDirectoryObject(string domainReference, string userName, string password)
         {
              DirectoryEntry entry = new DirectoryEntry(ADPath + domainReference, userName, password, AuthenticationTypes.Secure);
              return entry;
         }
 
         #endregion
 
         #region GetDirectoryEntry
 
         ///
         ///根據(jù)用戶公共名稱取得用戶的 對(duì)象
         ///
         ///
用戶公共名稱
         ///如果找到該用戶,則返回用戶的 對(duì)象;否則返回 null
         public static DirectoryEntry GetDirectoryEntry(string commonName)
         {
              DirectoryEntry de = GetDirectoryObject();
              DirectorySearcher deSearch = new DirectorySearcher(de);
              deSearch.Filter = "(&(&(objectCategory=person)(objectClass=user))(cn=" + commonName + "))";
              deSearch.SearchScope = SearchScope.Subtree;
 
              try
              {
                   SearchResult result = deSearch.FindOne();
                   de = new DirectoryEntry(result.Path);
                   return de;
              }
              catch
              {
                   return null;
              }
         }
 
         ///
         ///根據(jù)用戶公共名稱和密碼取得用戶的 對(duì)象。
         ///
         ///
用戶公共名稱
         ///
用戶密碼
         ///如果找到該用戶,則返回用戶的 對(duì)象;否則返回 null
         public static DirectoryEntry GetDirectoryEntry(string commonName, string password)
         {
              DirectoryEntry de = GetDirectoryObject(commonName, password);
              DirectorySearcher deSearch = new DirectorySearcher(de);
              deSearch.Filter = "(&(&(objectCategory=person)(objectClass=user))(cn=" + commonName + "))";
              deSearch.SearchScope = SearchScope.Subtree;
 
              try
              {
                   SearchResult result = deSearch.FindOne();
                   de = new DirectoryEntry(result.Path);
                   return de;
              }
              catch
              {
                   return null;
              }
         }
 
         ///
         ///根據(jù)用戶賬號(hào)稱取得用戶的 對(duì)象
         ///
         ///
用戶賬號(hào)名
         ///如果找到該用戶,則返回用戶的 對(duì)象;否則返回 null
         public static DirectoryEntry GetDirectoryEntryByAccount(string sAMAccountName)
         {
              DirectoryEntry de = GetDirectoryObject();
              DirectorySearcher deSearch = new DirectorySearcher(de);
              deSearch.Filter = "(&(&(objectCategory=person)(objectClass=user))(sAMAccountName=" + sAMAccountName + "))";
              deSearch.SearchScope = SearchScope.Subtree;
 
              try
              {
                   SearchResult result = deSearch.FindOne();
                   de = new DirectoryEntry(result.Path);
                   return de;
              }
              catch
              {
                   return null;
              }
         }
 
         ///
         ///根據(jù)用戶賬號(hào)和密碼取得用戶的 對(duì)象
         ///
         ///
用戶賬號(hào)名
         ///
用戶密碼
         ///如果找到該用戶,則返回用戶的 對(duì)象;否則返回 null
         public static DirectoryEntry GetDirectoryEntryByAccount(string sAMAccountName, string password)
         {
              DirectoryEntry de = GetDirectoryEntryByAccount(sAMAccountName);
              if (de != null)
              {
                   string commonName = de.Properties["cn"][0].ToString();
 
                   if (GetDirectoryEntry(commonName, password) != null)
                       return GetDirectoryEntry(commonName, password);
                   else
                       return null;
              }
              else
              {
                   return null;
              }
         }
 
         ///
         ///根據(jù)組名取得用戶組的 對(duì)象
         ///
         ///
組名
         ///
         public static DirectoryEntry GetDirectoryEntryOfGroup(string groupName)
         {
              DirectoryEntry de = GetDirectoryObject();
              DirectorySearcher deSearch = new DirectorySearcher(de);
              deSearch.Filter = "(&(objectClass=group)(cn=" + groupName + "))";
              deSearch.SearchScope = SearchScope.Subtree;
 
              try
              {
                   SearchResult result = deSearch.FindOne();
                   de = new DirectoryEntry(result.Path);
                   return de;
              }
              catch
              {
                   return null;
              }
         }
 
         #endregion
 
         #region GetProperty
 
         ///
         ///獲得指定 指定屬性名對(duì)應(yīng)的值
         ///
         ///
         ///
屬性名稱
         ///屬性值
         public static string GetProperty(DirectoryEntry de, string propertyName)
         {
              if(de.Properties.Contains(propertyName))
              {
                   return de.Properties[propertyName][0].ToString() ;
              }
              else
              {
                   return string.Empty;
              }
         }
 
         ///
         ///獲得指定搜索結(jié)果 中指定屬性名對(duì)應(yīng)的值
         ///
         ///
         ///
屬性名稱
         ///屬性值
         public static string GetProperty(SearchResult searchResult, string propertyName)
         {
              if(searchResult.Properties.Contains(propertyName))
              {
                   return searchResult.Properties[propertyName][0].ToString() ;
              }
              else
              {
                   return string.Empty;
              }
         }
 
         #endregion
 
         ///
         ///設(shè)置指定 的屬性值
         ///
         ///
         ///
屬性名稱
         ///
屬性值
         public static void SetProperty(DirectoryEntry de, string propertyName, string propertyValue)
         {
              if(propertyValue != string.Empty || propertyValue != "" || propertyValue != null)
              {
                   if(de.Properties.Contains(propertyName))
                   {
                       de.Properties[propertyName][0] = propertyValue;
                   }
                   else
                   {
                       de.Properties[propertyName].Add(propertyValue);
                   }
              }
         }
 
         ///
         ///創(chuàng)建新的用戶
         ///
         ///
DN 位置。例如:OU=共享平臺(tái) 或 CN=Users
         ///
公共名稱
         ///
賬號(hào)
         ///
密碼
         ///
         public static DirectoryEntry CreateNewUser(string ldapDN, string commonName, string sAMAccountName, string password)
         {
              DirectoryEntry entry = GetDirectoryObject();
              DirectoryEntry subEntry = entry.Children.Find(ldapDN);
              DirectoryEntry deUser = subEntry.Children.Add("CN=" + commonName, "user");
              deUser.Properties["sAMAccountName"].Value = sAMAccountName;
              deUser.CommitChanges();
              ADHelper.EnableUser(commonName);
              ADHelper.SetPassword(commonName, password);
              deUser.Close();
              return deUser;
         }
 
         ///
         ///創(chuàng)建新的用戶。默認(rèn)創(chuàng)建在 Users 單元下。
         ///
         ///
公共名稱
         ///
賬號(hào)
         ///
密碼
         ///
         public static DirectoryEntry CreateNewUser(string commonName, string sAMAccountName, string password)
         {
              return CreateNewUser("CN=Users", commonName, sAMAccountName, password);
         }
 
         ///
         ///判斷指定公共名稱的用戶是否存在
         ///
         ///
用戶公共名稱
         ///如果存在,返回 true;否則返回 false
         public static bool IsUserExists(string commonName)
         {
              DirectoryEntry de = GetDirectoryObject();
              DirectorySearcher deSearch = new DirectorySearcher(de);
              deSearch.Filter = "(&(&(objectCategory=person)(objectClass=user))(cn=" + commonName + "))";       // LDAP 查詢串
              SearchResultCollection results = deSearch.FindAll();
 
              if (results.Count == 0)
                   return false;
              else
                   return true;
         }
 
         ///
         ///判斷用戶賬號(hào)是否激活
         ///
         ///
用戶賬號(hào)屬性控制器
         ///如果用戶賬號(hào)已經(jīng)激活,返回 true;否則返回 false
         public static bool IsAccountActive(int userAccountControl)
         {
              int userAccountControl_Disabled = Convert.ToInt32(ADS_USER_FLAG_ENUM.ADS_UF_ACCOUNTDISABLE);
              int flagExists = userAccountControl & userAccountControl_Disabled;
 
              if (flagExists > 0)
                   return false;
              else
                   return true;
         }
 
         ///
         ///判斷用戶與密碼是否足夠以滿足身份驗(yàn)證進(jìn)而登錄
         ///
         ///
用戶公共名稱
         ///
密碼
         ///如能可正常登錄,則返回 true;否則返回 false
         public static LoginResult Login(string commonName, string password)
         {
              DirectoryEntry de = GetDirectoryEntry(commonName);
 
              if (de != null)
              {
                   // 必須在判斷用戶密碼正確前,對(duì)賬號(hào)激活屬性進(jìn)行判斷;否則將出現(xiàn)異常。
                   int userAccountControl = Convert.ToInt32(de.Properties["userAccountControl"][0]);
                   de.Close();
 
                   if (!IsAccountActive(userAccountControl))
                       return LoginResult.LOGIN_USER_ACCOUNT_INACTIVE;
 
                   if (GetDirectoryEntry(commonName, password) != null)
                       return LoginResult.LOGIN_USER_OK;
                   else
                       return LoginResult.LOGIN_USER_PASSWORD_INCORRECT;
              }
              else
              {
                   return LoginResult.LOGIN_USER_DOESNT_EXIST;
              }
         }
 
         ///
         ///判斷用戶賬號(hào)與密碼是否足夠以滿足身份驗(yàn)證進(jìn)而登錄
         ///
         ///
用戶賬號(hào)
         ///
密碼
         ///如能可正常登錄,則返回 true;否則返回 false
         public static LoginResult LoginByAccount(string sAMAccountName, string password)
         {
              DirectoryEntry de = GetDirectoryEntryByAccount(sAMAccountName);
                  
              if (de != null)
              {
                   // 必須在判斷用戶密碼正確前,對(duì)賬號(hào)激活屬性進(jìn)行判斷;否則將出現(xiàn)異常。
                   int userAccountControl = Convert.ToInt32(de.Properties["userAccountControl"][0]);
                   de.Close();
 
                   if (!IsAccountActive(userAccountControl))
                       return LoginResult.LOGIN_USER_ACCOUNT_INACTIVE;
 
                   if (GetDirectoryEntryByAccount(sAMAccountName, password) != null)
                       return LoginResult.LOGIN_USER_OK;
                   else
                       return LoginResult.LOGIN_USER_PASSWORD_INCORRECT;
              }
              else
              {
                   return LoginResult.LOGIN_USER_DOESNT_EXIST;
              }
         }
 
         ///
         ///設(shè)置用戶密碼,管理員可以通過(guò)它來(lái)修改指定用戶的密碼。
         ///
         ///
用戶公共名稱
         ///
用戶新密碼
         public static void SetPassword(string commonName, string newPassword)
         {
              DirectoryEntry de = GetDirectoryEntry(commonName);
             
              // 模擬超級(jí)管理員,以達(dá)到有權(quán)限修改用戶密碼
              impersonate.BeginImpersonate();
              de.Invoke("SetPassword", new object[]{newPassword});
              impersonate.StopImpersonate();
 
              de.Close();
         }
 
         ///
         ///設(shè)置賬號(hào)密碼,管理員可以通過(guò)它來(lái)修改指定賬號(hào)的密碼。
         ///
         ///
用戶賬號(hào)
         ///
用戶新密碼
         public static void SetPasswordByAccount(string sAMAccountName, string newPassword)
         {
              DirectoryEntry de = GetDirectoryEntryByAccount(sAMAccountName);
 
              // 模擬超級(jí)管理員,以達(dá)到有權(quán)限修改用戶密碼
              IdentityImpersonation impersonate = new IdentityImpersonation(ADUser, ADPassword, DomainName);
              impersonate.BeginImpersonate();
              de.Invoke("SetPassword", new object[]{newPassword});
              impersonate.StopImpersonate();
 
              de.Close();
         }
 
         ///
         ///修改用戶密碼
         ///
         ///
用戶公共名稱
         ///
舊密碼
         ///
新密碼
         public static void ChangeUserPassword (string commonName, string oldPassword, string newPassword)
         {
              // to-do: 需要解決密碼策略問(wèn)題
              DirectoryEntry oUser = GetDirectoryEntry(commonName);
              oUser.Invoke("ChangePassword", new Object[]{oldPassword, newPassword});
              oUser.Close();
         }
 
         ///
         ///啟用指定公共名稱的用戶
         ///
         ///
用戶公共名稱
         public static void EnableUser(string commonName)
         {
              EnableUser(GetDirectoryEntry(commonName));
         }
 
         ///
         ///啟用指定 的用戶
         ///
         ///
         public static void EnableUser(DirectoryEntry de)
         {
              impersonate.BeginImpersonate();
              de.Properties["userAccountControl"][0] = ADHelper.ADS_USER_FLAG_ENUM.ADS_UF_NORMAL_ACCOUNT | ADHelper.ADS_USER_FLAG_ENUM.ADS_UF_DONT_EXPIRE_PASSWD;
              de.CommitChanges();
              impersonate.StopImpersonate();
              de.Close();
         }
 
         ///
         ///禁用指定公共名稱的用戶
         ///
         ///
用戶公共名稱
         public static void DisableUser(string commonName)
         {
              DisableUser(GetDirectoryEntry(commonName));
         }
 
         ///
         ///禁用指定 的用戶
         ///
         ///
         public static void DisableUser(DirectoryEntry de)
         {
              impersonate.BeginImpersonate();
              de.Properties["userAccountControl"][0]=ADHelper.ADS_USER_FLAG_ENUM.ADS_UF_NORMAL_ACCOUNT | ADHelper.ADS_USER_FLAG_ENUM.ADS_UF_DONT_EXPIRE_PASSWD | ADHelper.ADS_USER_FLAG_ENUM.ADS_UF_ACCOUNTDISABLE;
              de.CommitChanges();
              impersonate.StopImpersonate();
              de.Close();
         }
 
         ///
         ///將指定的用戶添加到指定的組中。默認(rèn)為 Users 下的組和用戶。
         ///
         ///
用戶公共名稱
         ///
組名
         public static void AddUserToGroup(string userCommonName, string groupName)
          {
              DirectoryEntry oGroup = GetDirectoryEntryOfGroup(groupName);
              DirectoryEntry oUser = GetDirectoryEntry(userCommonName);
             
              impersonate.BeginImpersonate();
              oGroup.Properties["member"].Add(oUser.Properties["distinguishedName"].Value);
              oGroup.CommitChanges();
              impersonate.StopImpersonate();
 
              oGroup.Close();
              oUser.Close();
         }
 
         ///
         ///將用戶從指定組中移除。默認(rèn)為 Users 下的組和用戶。
         ///
         ///
用戶公共名稱
         ///
組名
         public static void RemoveUserFromGroup(string userCommonName, string groupName)
         {
              DirectoryEntry oGroup = GetDirectoryEntryOfGroup(groupName);
              DirectoryEntry oUser = GetDirectoryEntry(userCommonName);
             
              impersonate.BeginImpersonate();
              oGroup.Properties["member"].Remove(oUser.Properties["distinguishedName"].Value);
              oGroup.CommitChanges();
              impersonate.StopImpersonate();
 
              oGroup.Close();
              oUser.Close();
         }
 
     }
 
     ///
     ///用戶模擬角色類。實(shí)現(xiàn)在程序段內(nèi)進(jìn)行用戶角色模擬。
     ///
     public class IdentityImpersonation
     {
         [DllImport("advapi32.dll", SetLastError=true)]
         public static extern bool LogonUser(String lpszUsername, String lpszDomain, String lpszPassword, int dwLogonType, int dwLogonProvider, ref IntPtr phToken);
 
         [DllImport("advapi32.dll", CharSet=CharSet.Auto, SetLastError=true)]
         public extern static bool DuplicateToken(IntPtr ExistingTokenHandle, int SECURITY_IMPERSONATION_LEVEL, ref IntPtr DuplicateTokenHandle);
 
         [DllImport("kernel32.dll", CharSet=CharSet.Auto)]
         public extern static bool CloseHandle(IntPtr handle);
 
         // 要模擬的用戶的用戶名、密碼、域(機(jī)器名)
         private String _sImperUsername;
         private String _sImperPassword;
         private String _sImperDomain;
         // 記錄模擬上下文
         private WindowsImpersonationContext _imperContext;
         private IntPtr _adminToken;
         private IntPtr _dupeToken;
         // 是否已停止模擬
         private Boolean _bClosed;
 
         ///
         ///構(gòu)造函數(shù)
         ///
         ///
所要模擬的用戶的用戶名
         ///
所要模擬的用戶的密碼
         ///
所要模擬的用戶所在的域
         public IdentityImpersonation(String impersonationUsername, String impersonationPassword, String impersonationDomain)
         {
              _sImperUsername = impersonationUsername;
              _sImperPassword = impersonationPassword;
              _sImperDomain = impersonationDomain;
 
              _adminToken = IntPtr.Zero;
              _dupeToken = IntPtr.Zero;
              _bClosed = true;
         }
 
         ///
         ///析構(gòu)函數(shù)
         ///
         ~IdentityImpersonation()
         {
              if(!_bClosed)
              {
                   StopImpersonate();
              }
         }
 
         ///
         ///開(kāi)始身份角色模擬。
         ///
         ///
         public Boolean BeginImpersonate()
         {
              Boolean bLogined = LogonUser(_sImperUsername, _sImperDomain, _sImperPassword, 2, 0, ref _adminToken);
                       
              if(!bLogined)
              {
                   return false;
              }
 
              Boolean bDuped = DuplicateToken(_adminToken, 2, ref _dupeToken);
 
              if(!bDuped)
              {
                   return false;
              }
 
              WindowsIdentity fakeId = new WindowsIdentity(_dupeToken);
              _imperContext = fakeId.Impersonate();
 
              _bClosed = false;
 
              return true;
         }
 
         ///
         ///停止身分角色模擬。
         ///
         public void StopImpersonate()
         {
              _imperContext.Undo();
              CloseHandle(_dupeToken);
              CloseHandle(_adminToken);
              _bClosed = true;
         }
     }
}

=====================================================

簡(jiǎn)單的應(yīng)用

[WebMethod]
  public string IsAuthenticated(string UserID,string Password)
  {
            string _path = "LDAP://" + adm + "/DC=lamda,DC=com,DC=cn";//"LDAP://172.75.200.1/DC=名字,DC=com,DC=cn";
   string _filterAttribute=null;
  
   DirectoryEntry entry = new DirectoryEntry(_path,UserID,Password);
   
   try
   {
    //Bind to the native AdsObject to force authentication.
    DirectorySearcher search = new DirectorySearcher(entry);
    search.Filter = "(SAMAccountName=" + UserID + ")";
    SearchResult result = search.FindOne();
    
    if(null == result)
    {
     _filterAttribute="登錄失敗: 未知的用戶名或錯(cuò)誤密碼.";
    }
    else
    {
     _filterAttribute="true";
    }
   
   }
   catch (Exception ex)
   {
//    if(ex.Message.StartsWith("該服務(wù)器不可操作"))
//    {
//     string mail = ADO.GetConnString("mail");
//     entry.Path = "LDAP://"+mail+"/OU=名字,DC=it2004,DC=gree,DC=com,DC=cn";
//     try
//     { 
//      DirectorySearcher search = new DirectorySearcher(entry);
//      search.Filter = "(SAMAccountName=" + UserID + ")";
//      SearchResult result = search.FindOne();
//
//      if(null == result)
//      {
//       _filterAttribute="登錄失敗: 未知的用戶名或錯(cuò)誤密碼.";
//      }
//      else
//      {
//       _filterAttribute="true";
//      }
//      return _filterAttribute;
//   
//     }
//     catch (Exception ex1)
//     {
//      return ex1.Message;
//     }
//     
//    }
//    else
     return ex.Message;
   }
   return _filterAttribute;
  }
  [WebMethod]
  public string[] LDAPMessage(string UserID)
  {
   string _path = "LDAP://"+adm+"/DC=it2004,DC=名字,DC=com,DC=cn";
   string[] _filterAttribute=new string[5];
   string[] msg = {"samaccountname","displayname","department","company"};

   DirectoryEntry entry = new DirectoryEntry(_path,"180037","790813");

   
   try
   { 


    Object obj = entry.NativeObject;
    
    DirectorySearcher search = new DirectorySearcher(entry);
    search.Filter = "(SAMAccountName=" + UserID + ")";
    SearchResult result = search.FindOne();

    
    if(null == result)
    {
     _filterAttribute[0]="登錄失敗: 未知的用戶名或錯(cuò)誤密碼.";
    }
    else
    {
     _filterAttribute[0]="true";  
     for(int propertyCounter = 1; propertyCounter < 5; propertyCounter++)
     {
      
      if(propertyCounter==4 &&  result.Properties[msg[propertyCounter-1]][0]==null)
       break;
      _filterAttribute[propertyCounter]=result.Properties[msg[propertyCounter-1]][0].ToString();
      
     }
    }
   
   }
   catch (Exception ex)
   {
    //_filterAttribute[0]=ex.Message;
   }
   return _filterAttribute;
  }
  [WebMethod]
  public string[] AllMembers()
  {
   
   string[] msg;
   string _path = "LDAP://名字";

   DirectoryEntry entry = new DirectoryEntry(_path,"180037","790813");
   
 
   //Bind to the native AdsObject to force authentication.
   Object obj = entry.NativeObject;

   System.DirectoryServices.DirectorySearcher mySearcher = new System.DirectoryServices.DirectorySearcher(entry);
   mySearcher.Filter = "(SAMAccountName=180037)";
   msg=new string[mySearcher.FindAll().Count];
   int i=0;
   foreach(System.DirectoryServices.SearchResult result in mySearcher.FindAll())
   {
    msg[i++]=result.Path;
   }
   return msg;
  }
 
 }

    本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請(qǐng)注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購(gòu)買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊一鍵舉報(bào)。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評(píng)論

    發(fā)表

    請(qǐng)遵守用戶 評(píng)論公約

    類似文章 更多

    国产麻豆精品福利在线| 丰满少妇被猛烈插入在线观看| 国产精品久久男人的天堂| 中文字幕禁断介一区二区| 尤物天堂av一区二区| 人妻人妻人人妻人人澡| 国产午夜福利一区二区| 国产在线观看不卡一区二区 | 极品少妇嫩草视频在线观看| 欧美午夜不卡在线观看| 在线视频免费看你懂的| 欧美国产日韩在线综合| 亚洲人午夜精品射精日韩| 美日韩一区二区精品系列| 91麻豆精品欧美一区| 亚洲淫片一区二区三区| 亚洲中文字幕乱码亚洲| 超碰在线播放国产精品| 亚洲综合天堂一二三区| 日本高清视频在线观看不卡| 人妻一区二区三区多毛女| 丰满少妇被粗大猛烈进出视频| 国产欧美高清精品一区| 成人午夜在线视频观看| 东京热加勒比一区二区三区| 大香蕉伊人一区二区三区| 亚洲乱码av中文一区二区三区| 国产毛片av一区二区三区小说| 99久久人妻精品免费一区| 精品视频一区二区三区不卡| 中文字幕日韩精品人一妻| 日本深夜福利在线播放| 殴美女美女大码性淫生活在线播放| 东北女人的逼操的舒服吗| 欧洲精品一区二区三区四区 | 欧美国产日韩变态另类在线看| 亚洲视频在线观看你懂的| 男人操女人下面国产剧情| 欧美三级大黄片免费看| 亚洲综合精品天堂夜夜| 日本少妇aa特黄大片|