C#以其他用户身份访问Active Directory

时间:2018-12-12 15:48:55

标签: c# active-directory impersonation

我想在以下问题上寻求您的帮助。我正在临时应用程序上工作,将仅由我使用一次。

其中一部分是为AD中的3000多个用户执行密码重置,然后向他们发送新的凭据。

我可以以普通用户身份从AD读取内容,但是我必须使用特权帐户对其进行修改。我怎样才能做到这一点?我知道,我可以使用PowerShell并在几秒钟内完成它,但是我想学习如何在C#中做到这一点。

我要搜索用户的代码很简单

from django.urls import path
from.import views 

urlpatterns = {
    path('', views.article_list, name='list'),
    path('<slug:slug>/', views.article_detail, name='detail'),
}

Here is my trackback

  Environment:


    Request Method: GET
    Request URL: http://127.0.0.1:8000/articles/

    Django Version: 2.1.4
    Python Version: 3.7.1
    Installed Applications:
    ['django.contrib.admin',
     'django.contrib.auth',
     'django.contrib.contenttypes',
     'django.contrib.sessions',
     'django.contrib.messages',
     'django.contrib.staticfiles',
     'articles']
    Installed Middleware:
    ['django.middleware.security.SecurityMiddleware',
     'django.contrib.sessions.middleware.SessionMiddleware',
     'django.middleware.common.CommonMiddleware',
     'django.middleware.csrf.CsrfViewMiddleware',
     'django.contrib.auth.middleware.AuthenticationMiddleware',
     'django.contrib.messages.middleware.MessageMiddleware',
     'django.middleware.clickjacking.XFrameOptionsMiddleware']


    Template error:
    In template C:\Users\BOLADE\Desktop\django-playlist\blog\templates\base_layout.html, error at line 0
       'set' object is not reversible
       1 : {%  load static from staticfiles %}
       2 : <!DOCTYPE html>
       3 : <html lang="en">
       4 : <head>
       5 :     <meta charset="UTF-8">
       6 :     <title>Articles</title>
       7 :     <link rel="stylesheet" href="{% static 'styles.css' %}">
       8 : </head>
       9 : <body>
       10 : 


    Traceback:

    File "C:\Users\BOLADE\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\core\handlers\exception.py" in inner
      34.             response = get_response(request)

    File "C:\Users\BOLADE\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\core\handlers\base.py" in _get_response
      126.                 response = self.process_exception_by_middleware(e, request)

    File "C:\Users\BOLADE\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\core\handlers\base.py" in _get_response
      124.                 response = wrapped_callback(request, *callback_args, **callback_kwargs)

    File "C:\Users\BOLADE\Desktop\django-playlist\blog\articles\views.py" in article_list
      9.     return render(request, 'articles/article_list.html', {'articles': articles})

    File "C:\Users\BOLADE\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\shortcuts.py" in render
      36.     content = loader.render_to_string(template_name, context, request, using=using)

    File "C:\Users\BOLADE\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\template\loader.py" in render_to_string
      62.     return template.render(context, request)

    File "C:\Users\BOLADE\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\template\backends\django.py" in render
      61.             return self.template.render(context)

    File "C:\Users\BOLADE\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\template\base.py" in render
      171.                     return self._render(context)

    File "C:\Users\BOLADE\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\template\base.py" in _render
      163.         return self.nodelist.render(context)

    File "C:\Users\BOLADE\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\template\base.py" in render
      937.                 bit = node.render_annotated(context)

    File "C:\Users\BOLADE\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\template\base.py" in render_annotated
      904.             return self.render(context)

    File "C:\Users\BOLADE\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\template\loader_tags.py" in render
      150.             return compiled_parent._render(context)

    File "C:\Users\BOLADE\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\template\base.py" in _render
      163.         return self.nodelist.render(context)

    File "C:\Users\BOLADE\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\template\base.py" in render
      937.                 bit = node.render_annotated(context)

    File "C:\Users\BOLADE\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\template\base.py" in render_annotated
      904.             return self.render(context)

    File "C:\Users\BOLADE\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\template\loader_tags.py" in render
      62.                 result = block.nodelist.render(context)

    File "C:\Users\BOLADE\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\template\base.py" in render
      937.                 bit = node.render_annotated(context)

    File "C:\Users\BOLADE\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\template\base.py" in render_annotated
      904.             return self.render(context)

    File "C:\Users\BOLADE\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\template\defaulttags.py" in render
      209.                     nodelist.append(node.render_annotated(context))

    File "C:\Users\BOLADE\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\template\base.py" in render_annotated
      904.             return self.render(context)

    File "C:\Users\BOLADE\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\template\defaulttags.py" in render
      442.             url = reverse(view_name, args=args, kwargs=kwargs, current_app=current_app)

    File "C:\Users\BOLADE\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\urls\base.py" in reverse
      90.     return iri_to_uri(resolver._reverse_with_prefix(view, prefix, *args, **kwargs))

    File "C:\Users\BOLADE\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\urls\resolvers.py" in _reverse_with_prefix
      562.             self._populate()

    File "C:\Users\BOLADE\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\urls\resolvers.py" in _populate
      430.                     url_pattern._populate()

    File "C:\Users\BOLADE\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\urls\resolvers.py" in _populate
      413.             for url_pattern in reversed(self.url_patterns):

    Exception Type: TypeError at /articles/
    Exception Value: 'set' object is not reversible

我如何做,但是作为不同的用户?

我看过一些指南,但是没有一个解释a-z……只是关于如何模仿的建议,但没有关于如何使用它的建议。这里有一篇有关模拟的文章,但使用的是LDAP协议(public class ADSecurity { public static string getUserName(string sam) { PrincipalContext ctx = new PrincipalContext(ContextType.Domain); UserPrincipal user = UserPrincipal.FindByIdentity(ctx, IdentityType.SamAccountName, sam); return user.Name; } } )。但是据我了解,这确实很慢。

任何建议表示赞赏。我需要从现在开始2天运行它,因此在最坏的情况下,我使用PowerShell来执行它。

谢谢。

1 个答案:

答案 0 :(得分:1)

有几种方法可以做到:

  1. 以所需的凭据运行应用程序(按住Shift并右键单击.exe文件,然后使用“以其他用户身份运行”)。如果您只做一次,这是最简单的。
  2. 使用接受用户名和密码的PrincipalContext constructor
public class ADSecurity
{
    public static string getUserName(string sam)
    {
        PrincipalContext ctx = new PrincipalContext(ContextType.Domain, null, username, password);
        UserPrincipal user = UserPrincipal.FindByIdentity(ctx, IdentityType.SamAccountName, sam);
        return user.Name;
    }
}
  1. 使用接受用户名和密码的DirectoryEntry constructor

我不确定您在说哪个示例是“缓慢的”,但是根据我的经验,只要正确使用DirectoryEntry几乎总是更快。 System.DirectoryServices.AccountManagement名称空间(在示例中使用的名称空间)无论如何都在幕后使用DirectoryEntry

当然,选项2和3要求您知道密码。