Welkom bij WerfNet..

2024-08-13 11:56:39

Usermanager search by name and email

The Sitecore Usermanager gives you the posibility to search users on their user-account name. But suppose you are connected to the Active directory and the user accounts are not derived from the users' name. For example ABC1234. When you then have to find an account for a user and you do not know the account name, it is hard to find them. Esp. when the list of users is large.

With the help of our great friend Google I was able to find some code that makes it possible to search by account-name AND email address. full name is not so easy, because that information is not available at the moment of search. A piece of code and an extra config is all it takes.
When you have deployed this you can search on both (partial) account-name and email-address. As emailaddresses are often reflecting the real name of the users it is now possible to find users by name.

 

Here is config (MyProject.Foundation.Security.Providers.cs):

<?xml version="1.0" encoding="utf-8" ?>
<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/" xmlns:x="http://www.sitecore.net/xmlconfig/">
    <sitecore>
        <userManager>
            <patch:attribute name="defaultProvider">custom</patch:attribute>
            <providers>
                <clear/>
                <add name="custom" type="MyProject.Foundation.Security.Providers.CustomUserProvider, MyProject.Foundation"/>
            </providers>
        </userManager>
    </sitecore>
</configuration>

Here is the code (CustomUserProvider.cs):

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Security;
using Sitecore;
using Sitecore.Common;
using Sitecore.Diagnostics;
using Sitecore.Security.Accounts;

namespace MyProject.Foundation.Security.Providers
{
    public class CustomUserProvider : UserProvider
    {
        public override IFilterable GetUsers()
        {
            return new Filterable(this.GetAllUsers, this.GetAllUsers, this.GetUserCount, this.GetUsersByName, this.GetUsersByNameCount);
        }

        protected override IEnumerable GetUsersByName(int pageIndex, int pageSize, string userNameToMatch)
        {
            Assert.ArgumentNotNull(userNameToMatch, "userNameToMatch");
            int total;
            var userWithNoWildcard = this.GetUserWithNoWildcard(userNameToMatch);
            var membershipUserByName = Membership.FindUsersByName(userNameToMatch, pageIndex, pageSize, out total).OfType().ToList();
            var membershipUserByEmail = Membership.FindUsersByEmail(userNameToMatch, pageIndex, pageSize, out total).OfType().ToList();

            var membershipUser = membershipUserByEmail;

            foreach (var user in membershipUserByName)
            {
                if (!membershipUser.Where(m => m.UserName == user.UserName).Any())
                {
                    membershipUser.Add(user);
                }
            }

            var users = new Enumerable(() => membershipUser.GetEnumerator(), o => User.FromName(((MembershipUser)o).UserName, false));
            return users;
        }

        private string GetUserWithNoWildcard(string userNameToMatch)
        {
            var virtualMembershipWildcard = Sitecore.Configuration.Settings.Authentication.VirtualMembershipWildcard;
            return StringUtil.RemovePostfix(
                virtualMembershipWildcard,
                StringUtil.RemovePrefix(virtualMembershipWildcard, userNameToMatch));
        }

        private bool IsValidEmail(string email)
        {
            try
            {
                var addr = new System.Net.Mail.MailAddress(email);
                return addr.Address.Equals(email, StringComparison.OrdinalIgnoreCase);
            }
            catch
            {
                return false;
            }
        }

        private int GetUsersByNameCount(string userNameToMatch)
        {
            int totalRecords;
            var userWithNoWildcard = this.GetUserWithNoWildcard(userNameToMatch);
            if (this.IsValidEmail(userWithNoWildcard))
            {
                Membership.FindUsersByEmail(userWithNoWildcard, 0, 1, out totalRecords);
            }
            else
            {
                Membership.FindUsersByName(userNameToMatch, 0, 1, out totalRecords);
            }

            return totalRecords;
        }
    }
}