我也在这里发布了这个问题:https ://community.sitecore.net/developers/f/8/t/3681
大家好,
我正在尝试修改用户管理器以通过电子邮件进行搜索。我正在使用 Sitecore 7.1。我更新了 UserProvider 管道以使用 RegEx 检查电子邮件。如果搜索词是电子邮件,则通过电子邮件而不是用户名检索成员:
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));
}
}
}
我通过实例附加到调试器并尝试搜索测试用户。我将该帐户命名为“test”并将电子邮件设置为“test.test@ttttt”。单击搜索后,我的代码中的断点被击中,我看到它返回了用户!但是,界面什么都没有显示!我尝试了一个不同的场景,我创建了另一个用户名和电子邮件设置为“test.test@ttttt”的帐户,再次附加调试器,并再次通过电子邮件进行搜索。在代码中,我看到返回了两个用户,但界面只显示了用户名设置为“test.test@ttttt”的帐户。任何想法可能导致此问题?我试图调查 UserManager.aspx 文件,以及 Sitecore.Kernel 和 Sitecore.Client dll 中的 UserManager 和 UserProvider,
谢谢!
阿尔布拉