缩短响应时间:目录服务帐户管理

时间:2015-06-10 21:28:51

标签: c# performance

我正在创建一个工具来协助管理帐户权限。最初我实现了添加用户功能,但输出的响应时间/性能很慢。是否可以快速检索输出或改善响应时间?

代码示例:

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

namespace DirectoryServicesPlay
{
class Program
{
    static void Main(string[] args)
    {
        Original();
        Console.ReadLine();
    }

    private static void Original()
    {
        Console.WriteLine("getting domain context");
        var userContext = new PrincipalContext(ContextType.Domain);

        Console.WriteLine("getting user");
        var user = UserPrincipal.FindByIdentity(userContext, "c123456");

        Console.WriteLine("getting machine context");
        var machineContext = new PrincipalContext(ContextType.Machine, "ABCDEFGHI1", null, ContextOptions.Negotiate, "c123789", "test123");

        Console.WriteLine("getting group");
        var group = GroupPrincipal.FindByIdentity(machineContext, "Administrators"); //Response time is slow

        Console.WriteLine("adding member");
        group.Members.Add(user); //Response time is slow

        Console.WriteLine("saving group");
        group.Save();

        Console.WriteLine("getting members");
        var members = group.GetMembers();

        Console.WriteLine("iterating members");
        foreach (var member in members) // Response time is slow to retrieve and display each member information.
        {
            Console.WriteLine("{0} {1}", member.Name,member.SamAccountName); 
        }
        Console.WriteLine("Done.");
        Console.ReadLine();
    }
  }
}

0 个答案:

没有答案