【问题标题】:How can I get Active Directory username from their Exchange email address?如何从他们的 Exchange 电子邮件地址获取 Active Directory 用户名?
【发布时间】:2013-04-03 20:11:59
【问题描述】:

这是我正在尝试使用的代码,但我并没有真正使用它:

public static string GetUserIdFromEmail(string emailAddress)
    {
        string displayName = string.Empty;

        if (emailAddress.Contains("@sub.domain.edu") || emailAddress.Contains("@domain.edu"))
        {
            displayName = emailAddress.Split(new char[] { '@' })[0];
        }
        else
        {
            //no active directory account.
            return string.Empty;
        }


        // set up domain context
        using (PrincipalContext ctx = new PrincipalContext(ContextType.Domain))
        {
            // find user by display name
            try
            {
                UserPrincipal user = UserPrincipal.FindByIdentity(ctx, displayName);
                if (user != null)
                {
                    return user.SamAccountName; // or UserPrincipleName
                }
                else
                {
                    return string.Empty;
                }
            }
            catch (Exception)
            {
                return "Error";
            }
        }
    }

以下是我对我们当前系统的了解:

  1. 电子邮件将是独一无二的并遵循特定的命名约定

  2. 命名约定是名字的首字母和全名 (dstanley)。

  3. Exchange 上的一些旧帐户是 first.last@email.com

  4. 我不知道自己在做什么,但我知道自己想要什么。

我希望能够获取用户域的电子邮件地址并在活动目录中找到他们的记录。上面的代码是在正确的轨道上还是我可以做一些更简单的事情?

【问题讨论】:

  • 就个人而言,我喜欢承认 4 号的勇气。
  • 哈,胆子?更像是“如果这是一个愚蠢的问题,请不要向我扔石头”之类的声明。我只是从未真正使用过 Intranet 应用程序,并尝试以编程方式获取 AD 内容。
  • 在我们的行业中,人们常常没有意识到承认自己不知道自己在做什么是完全可以的。这就是这个网站存在的原因之一。

标签: c# active-directory exchange-server


【解决方案1】:

为了使用标准活动目录库访问用户电子邮件,您必须首先拥有 samaccount。这意味着该问题的解决方案需要您遍历每个 samaccount 并比较电子邮件地址,直到找到匹配项。

伪代码

foreach(user in activedirectory) 
{
  if (user.email = targeted_email)
  {
      return matched samaccount
  }
}

我认为没有更好的方法来解决这个问题,因此它是一种资源密集型解决方案。

【讨论】:

    猜你喜欢
    • 2013-04-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-27
    • 1970-01-01
    • 2012-04-03
    相关资源
    最近更新 更多