• 大小: 52KB
    文件类型: .rar
    金币: 1
    下载: 0 次
    发布日期: 2021-05-14
  • 语言: C#
  • 标签: AD  

资源简介

C#读取AD域组织架构及用户例子。值得初学者一看。

资源截图

代码片段和文件信息

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.DirectoryServices;
using System.Collections;
using System.Text.Regularexpressions;

namespace ActiveDirectoryDome
{
    class Program
    {
        static void Main(string[] args)
        {
            if (TryAuthenticate(“whjh.com“ “lity“ “admins“))
            {
                Console.WriteLine(“登陆成功!“);
            }
            else
            {
                Console.WriteLine(“登陆失败!“);
            }
            Console.WriteLine(“*******************************************************“);
            List group = new List();
            group = GetADGroups(“lity“ “whjh.com“ “lity“ “admins“);
            foreach (string temp in group)
            {
                Console.WriteLine(“组名称:“+temp.ToString());
            }

            Console.ReadLine();
        }

        /// 
        /// 验证AD用户是否登陆成功
        /// 

        /// 域名称
        /// 用户名
        /// 密码
        /// 返回登陆状态
        public static bool TryAuthenticate(string domain string username string password)
        {
            bool isLogin = false;
            try
            {
                DirectoryEntry entry = new DirectoryEntry(string.Format(“LDAP://{0}“ domain) username password);
                entry.RefreshCache();
                isLogin = true;
            }
            catch
            {
                isLogin = false;
            }
            return isLogin;
        }

        /// 
        /// 取用户所对应的用户组
        /// 

        /// 用户名称
        /// 
        /// 登陆用户
        /// 登陆密码
        /// 
        public static List GetADGroups(string userNamestring domainstring adusernamestring adpassword)
        {
            List groups = new List();
            try
            {
                var entry = new DirectoryEntry(string.Format(“LDAP://{0}“ domain) adusername adpassword);
                entry.RefreshCache();

                DirectorySearcher search = new DirectorySearcher(entry);
                //search.PropertiesToLoad.Add(“memberof“);
                //search.Filter = string.Format(“sAMAccountName={0}“ userName);
                //SearchResult result = search.FindOne();

                search.Filter = “(&(objectCategory=person)(objectClass=user))“;
                search.Sort = new SortOption(“samaccountname“ SortDirection.Ascending);
                search.SearchScope = SearchScope.Subtree;

                SearchResultCollection resultColl = search.FindAll();

                foreach (SearchResult result in resultColl)
       

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----

     文件       2565  2010-08-17 11:06  C#读取AD域里用户名或组\dome\ActiveDirectoryDome\ActiveDirectoryDome\ActiveDirectoryDome.csproj

     文件       7168  2011-11-24 13:45  C#读取AD域里用户名或组\dome\ActiveDirectoryDome\ActiveDirectoryDome\bin\Debug\ActiveDirectoryDome.exe

     文件      15872  2011-11-24 13:45  C#读取AD域里用户名或组\dome\ActiveDirectoryDome\ActiveDirectoryDome\bin\Debug\ActiveDirectoryDome.pdb

     文件      11592  2011-11-24 13:45  C#读取AD域里用户名或组\dome\ActiveDirectoryDome\ActiveDirectoryDome\bin\Debug\ActiveDirectoryDome.vshost.exe

     文件        490  2009-06-11 05:14  C#读取AD域里用户名或组\dome\ActiveDirectoryDome\ActiveDirectoryDome\bin\Debug\ActiveDirectoryDome.vshost.exe.manifest

     文件       1736  2011-11-24 13:45  C#读取AD域里用户名或组\dome\ActiveDirectoryDome\ActiveDirectoryDome\obj\Debug\ActiveDirectoryDome.csproj.FileListAbsolute.txt

     文件       7168  2011-11-24 13:45  C#读取AD域里用户名或组\dome\ActiveDirectoryDome\ActiveDirectoryDome\obj\Debug\ActiveDirectoryDome.exe

     文件      15872  2011-11-24 13:45  C#读取AD域里用户名或组\dome\ActiveDirectoryDome\ActiveDirectoryDome\obj\Debug\ActiveDirectoryDome.pdb

     文件       3818  2011-11-24 13:30  C#读取AD域里用户名或组\dome\ActiveDirectoryDome\ActiveDirectoryDome\Program.cs

     文件       1370  2010-08-17 10:37  C#读取AD域里用户名或组\dome\ActiveDirectoryDome\ActiveDirectoryDome\Properties\AssemblyInfo.cs

     文件       1434  2011-11-24 13:44  C#读取AD域里用户名或组\dome\ActiveDirectoryDome\ActiveDirectoryDome.sln

    ..A..H.     29184  2011-11-24 13:48  C#读取AD域里用户名或组\dome\ActiveDirectoryDome\ActiveDirectoryDome.suo

     文件       4269  2011-11-24 13:44  C#读取AD域里用户名或组\dome\ActiveDirectoryDome\ADYUAccount\ADYUAccount.csproj

     文件       1297  2011-11-24 13:44  C#读取AD域里用户名或组\dome\ActiveDirectoryDome\ADYUAccount\ADYUAccount.csproj.user

     文件        389  2011-11-24 14:05  C#读取AD域里用户名或组\dome\ActiveDirectoryDome\ADYUAccount\ADYUAccount.Publish.xml

     文件       8192  2011-11-24 14:14  C#读取AD域里用户名或组\dome\ActiveDirectoryDome\ADYUAccount\bin\ADYUAccount.dll

     文件      17920  2011-11-24 14:14  C#读取AD域里用户名或组\dome\ActiveDirectoryDome\ADYUAccount\bin\ADYUAccount.pdb

     文件        813  2011-11-24 13:54  C#读取AD域里用户名或组\dome\ActiveDirectoryDome\ADYUAccount\Default.aspx

     文件       8720  2011-11-24 14:14  C#读取AD域里用户名或组\dome\ActiveDirectoryDome\ADYUAccount\Default.aspx.cs

     文件       2560  2011-11-24 13:47  C#读取AD域里用户名或组\dome\ActiveDirectoryDome\ADYUAccount\Default.aspx.designer.cs

     文件        498  2011-11-24 13:48  C#读取AD域里用户名或组\dome\ActiveDirectoryDome\ADYUAccount\obj\Debug\ADYUAccount.csproj.FileListAbsolute.txt

     文件       8192  2011-11-24 14:14  C#读取AD域里用户名或组\dome\ActiveDirectoryDome\ADYUAccount\obj\Debug\ADYUAccount.dll

     文件      17920  2011-11-24 14:14  C#读取AD域里用户名或组\dome\ActiveDirectoryDome\ADYUAccount\obj\Debug\ADYUAccount.pdb

     文件       1309  2011-11-24 13:35  C#读取AD域里用户名或组\dome\ActiveDirectoryDome\ADYUAccount\Properties\AssemblyInfo.cs

     文件       7305  2011-11-24 13:45  C#读取AD域里用户名或组\dome\ActiveDirectoryDome\ADYUAccount\Web.config

     目录          0  2010-08-17 11:41  C#读取AD域里用户名或组\dome\ActiveDirectoryDome\ActiveDirectoryDome\obj\Debug\TempPE

     目录          0  2011-11-24 13:35  C#读取AD域里用户名或组\dome\ActiveDirectoryDome\ADYUAccount\obj\Debug\TempPE

     目录          0  2011-11-23 17:57  C#读取AD域里用户名或组\dome\ActiveDirectoryDome\ActiveDirectoryDome\bin\Debug

     目录          0  2011-11-24 13:45  C#读取AD域里用户名或组\dome\ActiveDirectoryDome\ActiveDirectoryDome\obj\Debug

     目录          0  2011-11-24 14:14  C#读取AD域里用户名或组\dome\ActiveDirectoryDome\ADYUAccount\obj\Debug

............此处省略15个文件信息

评论

共有 条评论