首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

从C-Sharp/C上的Active Directory获取用户帐户状态(已锁定/已解锁)

从C-Sharp/C上的Active Directory获取用户帐户状态(已锁定/已解锁)的方法是通过查询用户帐户的属性来实现的。以下是一个简单的C#示例,展示了如何获取用户帐户的状态:

代码语言:csharp
复制
using System;
using System.DirectoryServices;
using System.DirectoryServices.AccountManagement;

public class ActiveDirectoryHelper
{
    public static bool IsAccountLocked(string username)
    {
        using (var context = new PrincipalContext(ContextType.Domain))
        {
            using (var user = UserPrincipal.FindByIdentity(context, IdentityType.SamAccountName, username))
            {
                if (user == null)
                {
                    throw new ArgumentException("User not found in Active Directory.");
                }

                return user.IsAccountLockedOut();
            }
        }
    }
}

在这个示例中,我们使用了System.DirectoryServices和System.DirectoryServices.AccountManagement命名空间中的类。我们首先创建了一个PrincipalContext对象,用于连接到Active Directory域。然后,我们使用UserPrincipal.FindByIdentity方法查找用户帐户。最后,我们调用user.IsAccountLockedOut()方法来检查用户帐户是否已锁定。

需要注意的是,这个示例仅适用于Windows环境下的Active Directory。如果您使用的是其他类型的目录服务,则需要使用其他API来查询用户帐户状态。

页面内容是否对你有帮助?
有帮助
没帮助

相关·内容

没有搜到相关的合辑

领券