Esempio n. 1
0
        // Get Email Address from TFS Account or Display Name
        // source: https://paulselles.wordpress.com/2014/03/24/tfs-api-tfs-user-email-address-lookup-and-reverse-lookup/
        public string GetEmailAddress(string userName, string defaultValue)
        {
            using (var teamProjectCollection = this.connectionInfo.Token.GetCollection(this.connectionInfo.ProjectCollectionUri))
            {
                var identityManagementService = teamProjectCollection.GetService <IIdentityManagementService>();

                TeamFoundationIdentity identity = identityManagementService.ReadIdentity(
                    IdentitySearchFactor.AccountName,
                    userName,
                    MembershipQuery.None,
                    ReadIdentityOptions.ExtendedProperties);

                // if not found try again using DisplayName
                identity = identity ?? identityManagementService.ReadIdentity(
                    IdentitySearchFactor.DisplayName,
                    userName,
                    MembershipQuery.None,
                    ReadIdentityOptions.ExtendedProperties);

                if (identity == null)
                {
                    return(defaultValue);
                }

                // pick first non-null value
                string mailAddress = identity.GetAttribute("Mail", null);
                mailAddress = string.IsNullOrWhiteSpace(mailAddress) ?
                              identity.GetAttribute("ConfirmedNotificationAddress", defaultValue)
                    : mailAddress;

                return(mailAddress);
            }
        }
        private static string GetAccountName(TeamFoundationIdentity identity)
        {
            var scopeName = identity.GetAttribute("ScopeName", null);
            var account   = identity.GetAttribute("Account", null);

            if (!string.IsNullOrEmpty(scopeName) && !string.IsNullOrEmpty(account))
            {
                // If the identity has a scope name, it's a TFS group and the domain name is a classification URI (vstfs:///Classification/...).
                // In that case, the account name should be "[ScopeName]\Group" (e.g. "[MyTeamProject]\Contributors").
                return("[{0}]\\{1}".FormatInvariant(scopeName, account));
            }
            else
            {
                return(identity.UniqueName);
            }
        }
Esempio n. 3
0
        private static void SendEmail(string emailSubject, string emailBody, IEnumerable <string> artifactFiles, SmtpClient smtp, TeamFoundationIdentity tfsIdentity)
        {
            string emailAddress = tfsIdentity.GetAttribute("Mail", null);

            Console.WriteLine("Retrieved email address: " + emailAddress);

            var fromAddress = new MailAddress("*****@*****.**", "TFS Build Service");
            var toAddress   = new MailAddress(emailAddress, tfsIdentity.DisplayName);

            using (MailMessage message = new MailMessage(fromAddress, toAddress))
            {
                message.Subject = emailSubject;
                message.Body    = emailBody;

                foreach (string artifact in artifactFiles)
                {
                    Attachment attachment = new Attachment(artifact);
                    message.Attachments.Add(attachment);
                    Console.WriteLine("Adding attachment: " + attachment.Name);
                }

                smtp.Send(message);
                Console.WriteLine("Email correctly sent to: " + emailAddress);
            }
        }
        private void UpdateRichIdentity(RichIdentity richIdentity, TeamFoundationIdentity id)
        {
            const string Unknown = "";

            richIdentity.UniqueId          = id.Descriptor.Identifier;
            richIdentity.Alias             = id.GetAttribute(IdentityAttributeTags.AccountName, Unknown);
            richIdentity.DisplayName       = id.DisplayName;
            richIdentity.DistinguishedName = id.GetAttribute(IdentityAttributeTags.DistinguishedName, Unknown);
            richIdentity.Domain            = id.GetAttribute(IdentityAttributeTags.Domain, Unknown);

            if (id.GetAttribute(IdentityAttributeTags.MailAddress, "Unknown").Length != 0)
            {
                richIdentity.EmailAddress = id.GetAttribute(IdentityAttributeTags.MailAddress, Unknown);
            }

            TraceManager.TraceInformation(
                "Updated Identity (Unique Id: {0}; Alias: {1}; Display Name {2}; Distinguished Name: {3}; Domain: {4}",
                richIdentity.UniqueId ?? Unknown, richIdentity.Alias ?? Unknown, richIdentity.DisplayName ?? Unknown,
                richIdentity.DistinguishedName ?? Unknown, richIdentity.Domain ?? Unknown);
        }
        private string GetEmailAddress(TeamFoundationRequestContext requestContext, PushNotification pushNotification)
        {
            var collectionUrl     = new Uri(requestContext.GetCollectionUri());
            var collection        = TfsTeamProjectCollectionFactory.GetTeamProjectCollection(collectionUrl);
            var managementService = collection.GetService <IIdentityManagementService>();
            TeamFoundationIdentity teamFoundationIdentity = managementService.ReadIdentity(
                IdentitySearchFactor.AccountName,
                pushNotification.AuthenticatedUserName,
                MembershipQuery.None,
                ReadIdentityOptions.None);

            return(teamFoundationIdentity.GetAttribute("Mail", null));
        }
        /// <summary>
        /// Get Email Address from TFS Account or Display Name
        /// </summary>
        /// <remarks>https://paulselles.wordpress.com/2014/03/24/tfs-api-tfs-user-email-address-lookup-and-reverse-lookup/</remarks>
        private string GetEmailAddress(TfsTeamProjectCollection tpc, string displayName)
        {
            if (string.IsNullOrEmpty(displayName))
            {
                return(string.Empty);
            }
            try
            {
                IIdentityManagementService IdentityManagementService = tpc.GetService <IIdentityManagementService>();
                TeamFoundationIdentity     teamFoundationIdentity    =
                    IdentityManagementService.ReadIdentity(
                        IdentitySearchFactor.AccountName | IdentitySearchFactor.DisplayName,
                        displayName,
                        MembershipQuery.None,
                        ReadIdentityOptions.None);

                return(teamFoundationIdentity.GetAttribute("Mail", null));
            } catch
            {
                return(string.Empty);
            }
        }
Esempio n. 7
0
        // Get Email Address from TFS Account or Display Name
        // source: https://paulselles.wordpress.com/2014/03/24/tfs-api-tfs-user-email-address-lookup-and-reverse-lookup/
        public string GetEmailAddress(string userName, string defaultValue)
        {
            using (var teamProjectCollection =
                       this.connectionInfo.Token.GetCollection(this.connectionInfo.ProjectCollectionUri))
            {
                Func <string, string>[] helpers =
                {
#if !TFS2013
                    (lookup) =>
                    {
                        string email      = null;
                        var workItemStore = teamProjectCollection.GetService <WorkItemStore>();
                        if (workItemStore.TryFindIdentity(lookup, out var identity))
                        {
                            email = identity?.Email ?? string.Empty;
                        }

                        return(email);
                    },
#endif
                    (lookup) =>
                    {
                        var identityManagementService = teamProjectCollection.GetService <IIdentityManagementService>();

                        TeamFoundationIdentity identity = identityManagementService.ReadIdentity(
                            IdentitySearchFactor.AccountName,
                            lookup,
                            MembershipQuery.None,
                            ReadIdentityOptions.ExtendedProperties);

                        if (identity == null)
                        {
                            return(string.Empty);
                        }
                        else
                        {
                            string mailAddress = identity.GetAttribute("Mail", null);
                            mailAddress = string.IsNullOrWhiteSpace(mailAddress)
                                ? identity.GetAttribute("ConfirmedNotificationAddress", null)
                                : mailAddress;
                            return(mailAddress);
                        }
                    },
                    (lookup) =>
                    {
                        var identityManagementService = teamProjectCollection.GetService <IIdentityManagementService>();

                        TeamFoundationIdentity identity = identityManagementService.ReadIdentity(
                            IdentitySearchFactor.DisplayName,
                            lookup,
                            MembershipQuery.None,
                            ReadIdentityOptions.ExtendedProperties);

                        if (identity == null)
                        {
                            return(string.Empty);
                        }
                        else
                        {
                            string mailAddress = identity.GetAttribute("Mail", null);
                            mailAddress = string.IsNullOrWhiteSpace(mailAddress)
                                ? identity.GetAttribute("ConfirmedNotificationAddress", null)
                                : mailAddress;
                            return(mailAddress);
                        }
                    }
                };

                foreach (var helper in helpers)
                {
                    try
                    {
                        var result = helper(userName);
                        if (!string.IsNullOrWhiteSpace(result))
                        {
                            return(result);
                        }
                    }
                    catch {}
                }

                return(defaultValue);
            }
        }