C# 使用TFS 2012 API,如何获取用户的电子邮件地址?

C# 使用TFS 2012 API,如何获取用户的电子邮件地址?,c#,visual-studio-2012,tfs,C#,Visual Studio 2012,Tfs,我正在尝试使用API获取TFS 2012中特定用户的电子邮件地址。我已在配置文件部分设置了用户首选的电子邮件地址。我在网上做了大量的搜索,并有以下代码 var userId = "myUserId"; var collection = new TfsTeamProjectCollection(tfsUri, tfsCerd); var managementService = collection.GetService<IIdentityManagementService>(); v

我正在尝试使用API获取TFS 2012中特定用户的电子邮件地址。我已在配置文件部分设置了用户首选的电子邮件地址。我在网上做了大量的搜索,并有以下代码

var userId = "myUserId";
var collection = new TfsTeamProjectCollection(tfsUri, tfsCerd);
var managementService = collection.GetService<IIdentityManagementService>();

var member =
    managementService
        .ReadIdentity(
            IdentitySearchFactor.AccountName,
            userId,
            MembershipQuery.Direct,
            ReadIdentityOptions.ExtendedProperties);

var emailAddress = member.GetAttribute("Mail", null)
    public string GetUserEmail(string username)
    {
        using (var pctx = new PrincipalContext(ContextType.Domain))
        {
            using (UserPrincipal up = UserPrincipal.FindByIdentity(pctx, username))
            {
                return up != null && !string.IsNullOrEmpty(up.EmailAddress) ? up.EmailAddress : string.Empty;
            }
        }
    }
var userId=“myUserId”;
var collection=新的tfstreamprojectcollection(tfsUri、tfsCerd);
var managementService=collection.GetService();
变量成员=
管理服务
.ReadIdentity(
IdentitySearchFactor.AccountName,
用户ID,
MembershipQuery.Direct,
ReadIdentityOptions.ExtendedProperties);
var emailAddress=member.GetAttribute(“邮件”,null)
这段代码既是成功也是失败。它成功地检索了指定的用户;但是,问题是电子邮件属性为空。当我分析成员变量时,我注意到“Mail”属性列在那里,它是空的。然后我注意到还有另外两个属性“ConfirmedNotificationAddress”和“CustomNotificationAddress”,它们正确地包含了我的首选电子邮件地址


我想知道为什么我不能用首选电子邮件地址正确加载“Mail”变量,因为我需要这段代码在很多人的服务器上工作。

尝试使用
Mail
而不是
email
作为属性名-这对我来说很有用

此外,如果这不起作用,请检查
member.GetProperties()
的结果-这可能会为您提供正确的名称


对我来说,
GetProperty(“Mail”)
也起作用。

我遇到了同样的问题,我通过使用以下代码从广告中获取用户电子邮件地址找到了解决办法

var userId = "myUserId";
var collection = new TfsTeamProjectCollection(tfsUri, tfsCerd);
var managementService = collection.GetService<IIdentityManagementService>();

var member =
    managementService
        .ReadIdentity(
            IdentitySearchFactor.AccountName,
            userId,
            MembershipQuery.Direct,
            ReadIdentityOptions.ExtendedProperties);

var emailAddress = member.GetAttribute("Mail", null)
    public string GetUserEmail(string username)
    {
        using (var pctx = new PrincipalContext(ContextType.Domain))
        {
            using (UserPrincipal up = UserPrincipal.FindByIdentity(pctx, username))
            {
                return up != null && !string.IsNullOrEmpty(up.EmailAddress) ? up.EmailAddress : string.Empty;
            }
        }
    }
但后来我发现,当我的用户不在我的域中时,它会抛出一个异常。因此,这段代码帮助我获得了第二个源代码。如果我没有在AD中找到,我会去使用IdentityManager服务

    public TeamFoundationIdentity GetUserByAccountName(string account)
    {
        var ims = _tfServer.GetService<IIdentityManagementService>();
        return ims.ReadIdentity(IdentitySearchFactor.DisplayName, account, MembershipQuery.Expanded, ReadIdentityOptions.ExtendedProperties);
    }

那是我原来问题中的一个输入错误。我本想写“邮件”的。因此,“Mail”变量作为空字符串返回,另外两个变量返回正确的电子邮件地址。此邮件变量是否可能来自配置文件部分以外的其他位置?它可能来自Active Directory-您可以尝试使用ADExplorer.exe(来自Sysinternals Suite)查看那里用户的属性。