简体   繁体   English

使用C#登录到Active Directory

[英]Using C# to logon to Active Directory

I am wondering how to set the Logon Count in Active Directory (AD) from C#. 我想知道如何从C#中设置Active Directory(AD)中的登录计数。 Currently, I have a library that checks to see if a specific user is in AD based on username and password, but when I look at that user in AD Explorer, I don't see the logon count number getting bigger (from 1 to 2 and so on). 当前,我有一个库可以根据用户名和密码检查特定用户是否在AD中,但是当我在AD Explorer中查看该用户时,我看不到登录计数越来越大(从1到2)等等)。

I am assuming that I need to access a 'Logon' method from C# in order to do this, but I don't see a method like that in any of the DirectoryServices (as well as AccountManagement) methods. 我假设我需要从C#访问'Logon'方法来执行此操作,但是在任何DirectoryServices(以及AccountManagement)方法中都没有看到类似的方法。

Any suggestions? 有什么建议么?

Thank you, Tim 谢谢蒂姆

Have you checked all your domain controllers? 您检查了所有域控制器吗? Per the documentation of that attribute: 根据该属性的文档

This attribute is not replicated and is maintained on each domain controller in the domain. 此属性不会被复制,并在域中的每个域控制器上维护。 To get an accurate value for the user's total number of successful logon attempts in the domain, each domain controller in the domain must be queried and the sum of the values should be used. 若要获得用户在域中成功登录尝试总数的准确值,必须查询域中的每个域控制器,并应使用值的总和。 Keep in mind that the attribute is not replicated, therefore domain controllers that are retired may have counted logons for the user as well, and these will be missing from the count. 请记住,该属性不会被复制,因此退休的域控制器可能也已为用户计算了登录次数,因此这些计数将丢失。

edit: I guess it depends on how you're testing the user name and password. 编辑:我想这取决于您如何测试用户名和密码。 In one of my applications I check the U/P in fashion similar to this. 在我的一个应用程序中,我以类似的方式检查U / P。

NetworkCredential credentials = new NetworkCredential(userName, password, domain);
        LdapDirectoryIdentifier id = new LdapDirectoryIdentifier(domain);
        using (LdapConnection connection = new LdapConnection(id, credentials, AuthType.Kerberos))
        {
            connection.SessionOptions.Sealing = true;
            connection.SessionOptions.Signing = true;

            try
            {
                connection.Bind();
            }
            catch (LdapException lEx)
            {
                if (ERROR_LOGON_FAILURE == lEx.ErrorCode)
                {
                    return false;
                }
                throw;
            }

        }
        return true;

Which after several attempts didn't seem to increment the logon counter on the DC I was hitting. 经过几次尝试似乎都没有增加我所击中的DC上的登录计数器。

However, when I used this method 但是,当我使用这种方法时

using (PrincipalContext pc = new PrincipalContext(ContextType.Domain))
        {
            bool authenticated;
            authenticated = pc.ValidateCredentials(userName, password);
            if (authenticated)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

It did increment it. 它确实增加了它。 At least after a couple tries. 至少经过几次尝试。 We have a bunch of DCs here and I don't know how to target a specific one. 我们这里有很多区议会,我不知道如何针对特定的区议会。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM