public Identity Match(string name, CancellationToken cancellationToken)
        {
            if (!configurationStore.GetIsEnabled())
            {
                return(null);
            }

            string domain;
            string normalisedName;

            objectNameNormalizer.NormalizeName(name, out normalisedName, out domain);

            using (var context = contextProvider.GetContext(domain))
            {
                if (cancellationToken.IsCancellationRequested)
                {
                    return(null);
                }

                var userPrincipal = new UserPrincipal(context);

                if (normalisedName.Contains("@"))
                {
                    userPrincipal.UserPrincipalName = normalisedName;
                }
                else
                {
                    userPrincipal.SamAccountName = normalisedName;
                }

                var searcher = new PrincipalSearcher
                {
                    QueryFilter = userPrincipal
                };

                var users = searcher.FindAll();
                if (!users.Any() || users.Count() > 1)
                {
                    return(null);
                }

                return(users
                       .Select(u => identityCreator.Create("", u.UserPrincipalName, ConvertSamAccountName(u, domain), u.DisplayName))
                       .First());
            }
        }
예제 #2
0
        public ExternalUserLookupResult Search(string searchTerm, CancellationToken cancellationToken)
        {
            if (!configurationStore.GetIsEnabled())
            {
                return(new ExternalUserLookupResult(DirectoryServicesAuthentication.ProviderName, Enumerable.Empty <IdentityResource>().ToArray()));
            }

            objectNameNormalizer.NormalizeName(searchTerm, out var partialName, out var domain);

            using (var context = contextProvider.GetContext(domain))
            {
                if (cancellationToken.IsCancellationRequested)
                {
                    return(null);
                }

                var identities = new List <Principal>(SearchBy(new UserPrincipal(context)
                {
                    Name = "*" + partialName + "*"
                }));
                identities.AddRange(SearchBy(new UserPrincipal(context)
                {
                    UserPrincipalName = "*" + partialName + "*"
                }));
                identities.AddRange(SearchBy(new UserPrincipal(context)
                {
                    SamAccountName = "*" + partialName + "*"
                }));

                var identityResources = identities.Distinct(new PrincipalComparer())
                                        .Select(u => identityCreator.Create("", u.UserPrincipalName, ConvertSamAccountName(u, domain),
                                                                            u.DisplayName).ToResource())
                                        .ToArray();

                return(new ExternalUserLookupResult(DirectoryServicesAuthentication.ProviderName, identityResources));
            }
        }
 Identity NewIdentity(string emailAddress, string userPrincipalName, string samAccountName, string displayName)
 {
     return(identityCreator.Create(emailAddress, userPrincipalName, samAccountName, displayName));
 }