I posted this question here too: https://community.sitecore.net/developers/f/8/t/3681
Hi all,
I am trying to modify the User Manager to search by email. I am using Sitecore 7.1. I updated the UserProvider pipeline to check for emails using RegEx. If the search term is an email, then members are retrieved by email, not user name:
using Sitecore;
using Sitecore.Common;
using Sitecore.Configuration;
using Sitecore.Diagnostics;
using Sitecore.Security.Accounts;
using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;
using System.Web.Security;
namespace Configuration.SharedCNA.Pipelines
{
public class CustomUserProvider : UserProvider
{
public override IFilterable<User> GetUsers()
{
var users = new Filterable<User>(GetAllUsers, GetAllUsers, GetUserCount, GetUsersByName, GetUsersByNameCount);
return users;
}
protected override IEnumerable<User> GetUsersByName(int pageIndex, int pageSize, string userNameToMatch)
{
var users = FindUsers(pageIndex, pageSize, userNameToMatch);
return users;
}
private int GetUsersByNameCount(string userNameToMatch)
{
int totalRecords;
string userWithNoWildcard = StringUtil.RemovePostfix(Settings.Authentication.VirtualMembershipWildcard,
StringUtil.RemovePrefix(Settings.Authentication.VirtualMembershipWildcard, userNameToMatch));
if (Regex.IsMatch(userWithNoWildcard,
@"^([0-9a-zA-Z]([-.\w]*[0-9a-zA-Z])*@([0-9a-zA-Z][-\w]*[0-9a-zA-Z]\.)+[a-zA-Z]{2,9})$"))
Membership.FindUsersByEmail(userWithNoWildcard, 0, 1, out totalRecords);
else
Membership.FindUsersByName(userNameToMatch, 0, 1, out totalRecords);
return totalRecords;
}
protected IEnumerable<User> FindUsers(int pageIndex, int pageSize, string userNameToMatch)
{
Assert.ArgumentNotNull(userNameToMatch, "userNameToMatch");
IEnumerable<User> users;
string userWithNoWildcard = StringUtil.RemovePostfix(Settings.Authentication.VirtualMembershipWildcard,
StringUtil.RemovePrefix(Settings.Authentication.VirtualMembershipWildcard, userNameToMatch));
if (Regex.IsMatch(userWithNoWildcard,
@"^([0-9a-zA-Z]([-.\w]*[0-9a-zA-Z])*@([0-9a-zA-Z][-\w]*[0-9a-zA-Z]\.)+[a-zA-Z]{2,9})$"))
users = FindByEmail(pageIndex, pageSize, userWithNoWildcard);
else
users = FindByUsername(pageIndex, pageSize, userNameToMatch);
return users;
}
protected IEnumerable<User> FindByUsername(int pageIndex, int pageSize, string userNameToMatch)
{
Assert.ArgumentNotNull(userNameToMatch, "userNameToMatch");
int total;
return
new Enumerable<User>(
() => Membership.FindUsersByName(userNameToMatch, pageIndex, pageSize, out total).GetEnumerator(),
o => User.FromName(((MembershipUser)o).UserName, false));
}
protected IEnumerable<User> FindByEmail(int pageIndex, int pageSize, string userNameToMatch)
{
Assert.ArgumentNotNull(userNameToMatch, "userNameToMatch");
int total;
return
new Enumerable<User>(
() => Membership.FindUsersByEmail(userNameToMatch, pageIndex, pageSize, out total).GetEnumerator(),
o => User.FromName(((MembershipUser)o).UserName, false));
}
}
}
I attached by instance to the debugger and tried to search for a test user. I named that account "test" and set the email to "test.test@ttttt". After I clicked search, the breakpoint in my code was hit, and I saw that it returned the user! However, the interface showed nothing at all! I tried a different scenario where I created another account with the user name and the email set to "test.test@ttttt", attached the debugger again, and again searched by the email. In the code, I saw that two users where returned, but the interface only displayed the account with the user name set to "test.test@ttttt". Any ideas what could be causing this issue? I tried to investigate the UserManager.aspx file, along with the the UserManager and UserProvider in the Sitecore.Kernel and Sitecore.Client dlls, but unfortunately I couldn't figure out the problem.
Thanks!
Albraa