修改用户管理器以通过电子邮件搜索 - Pipleline返回用户,但界面上没有显示任何内容

时间:2016-08-17 19:42:54

标签: c# sitecore usermanager

我也在这里发布了这个问题: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,但不幸的是我无法弄清楚问题。

谢谢!

Albraa

0 个答案:

没有答案