如何检查是否Windows用户帐户名域的存在? [英] How to check if Windows user account name exists in domain?

查看:195
本文介绍了如何检查是否Windows用户帐户名域的存在?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

什么是C#来检查Windows用户帐户名称存在的最简单,最有效的方法?这是在域环境中

What is the simplest and most efficient way in C# to check if a Windows user account name exists? This is in a domain environment.


  • 输入:用户在[域] / [用户]格式的名称(例如mycompany\bob )

  • 输出:如果用户名存在,如果不是

我做了错误的。找到这篇文章但例子也有相关的认证和操纵用户帐户和他们假定您已经有用户专有名称,而我开始与用户帐户名。

I did find this article but the examples there are related to authenticating and manipulating user accounts, and they assume you already have a user distinguished name, whereas I am starting with the user account name.

我敢肯定,我可以使用AD想出解决办法,但我之前,所以我想知道如果有一个简单的更高级别的API,做我需要什么。

I'm sure I can figure this out using AD, but before I do so I was wondering if there is a simple higher level API that does what I need.

* UPDATE *

有可能很多方法可以做到这一点,拉斯发布一个可以工作,但我无法弄清楚如何调整它在我的环境中工作。我没有找到一个不同的方法,使用WinNT供应商,做的工作对我来说:

There are probably many ways to do this, Russ posted one that could work but I couldn't figure out how to tweak it to work in my environment. I did find a different approach, using the WinNT provider that did the job for me:

    public static bool UserInDomain(string username, string domain)
    {
        string path = String.Format("WinNT://{0}/{1},user", domain, username);

        try
        {
            DirectoryEntry.Exists(path);
            return true;
        }
        catch (Exception)
        {
            // For WinNT provider DirectoryEntry.Exists throws an exception
            // instead of returning false so we need to trap it.
            return false;
        }
    }

PS
。对于那些谁不熟悉上面使用的API:您需要添加到的System.DirectoryServices参考使用它

P.S. For those who aren't familiar with the API used above: you need to add a reference to System.DirectoryServices to use it.

我发现的链接,帮助我与此:我如何获取用户信息使用ADSI
中的示例使用ADSI但可以适用于.NET的DirectoryServices为好。他们还展示了用户对象可能有用的其他属性。

The link I found that helped me with this: How Can I Get User Information Using ADSI The examples use ADSI but can be applied to .NET DirectoryServices as well. They also demonstrate other properties of the user object that may be useful.

推荐答案

的System.DirectoryServices 命名空间中的文章到底是什么需要和用于这一目的。如果我没有记错,这是围绕着 Active Directory中的包装服务器接口 COM接口

编辑:

有点像下面应该这样做(这大概可以做一些检查和处理)。它将使用当前安全上下文的域查找域控制器,但可以很容易地修改了指定的服务器传递。

Something like the following should do it (it could probably do with some checking and handling). It will use the domain of the current security context to find a domain controller, but this could easily be amended to pass in a named server.

public bool UserInDomain(string username, string domain)
{
    string LDAPString = String.Empty;
    string[] domainComponents = domain.Split('.');
    StringBuilder builder = new StringBuilder();

    for (int i = 0; i < domainComponents.Length; i++)
    {
        builder.AppendFormat(",dc={0}", domainComponents[i]);
    }
    if (builder.Length > 0)
        LDAPString = builder.ToString(1, builder.Length - 1);

    DirectoryEntry entry = new DirectoryEntry("LDAP://" + LDAPString);

    DirectorySearcher searcher = new DirectorySearcher(entry);

    searcher.Filter = "sAMAccountName=" + username;

    SearchResult result = searcher.FindOne();

    return (result != null) ? true : false;
}

和具有以下

Console.WriteLine(UserInDomain("username","MyDomain.com").ToString());

这篇关于如何检查是否Windows用户帐户名域的存在?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆