You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Kentico 10 如何同时支持邮箱与用户名两种登录认证方式

在Kentico中实现用户名/邮箱双登录的解决方案

没问题,我来帮你搞定这个需求——让Kentico的登录表单同时支持邮箱和用户名登录。核心思路就是扩展登录验证逻辑,让系统能自动识别用户输入的是邮箱还是用户名,再对应去CMS_User表查询验证。下面是具体的实现步骤:

1. 编写自定义用户验证方法

首先我们需要一个方法,用来判断用户输入的内容是邮箱还是用户名,然后调用Kentico的API完成验证。这个方法会先检查输入格式是否符合邮箱规范,是就按邮箱查用户,否则按用户名查:

private bool ValidateUser(string accountInput, string password)
{
    UserInfo targetUser = null;

    // 先判断输入是否为邮箱格式
    if (System.Net.Mail.MailAddress.TryCreate(accountInput, out _))
    {
        targetUser = UserInfoProvider.GetUserInfoByEmail(accountInput);
    }
    else
    {
        // 不是邮箱的话,按UserName字段查询用户
        targetUser = UserInfoProvider.GetUserInfo(accountInput);
    }

    // 验证用户存在且未被删除,同时校验密码(用Kentico内置方法保证加密逻辑一致)
    if (targetUser != null && !targetUser.UserIsDeleted)
    {
        return UserInfoProvider.ValidateUserPassword(targetUser, password);
    }

    return false;
}

2. 修改登录表单的提交逻辑

接下来要把这个验证方法集成到登录表单的提交流程里:

如果你用的是Portal Engine(WebForms)

假设你有一个自定义的登录WebPart,或者修改内置的登录控件,在登录按钮的点击事件里调用上面的验证方法:

protected void btnSubmitLogin_Click(object sender, EventArgs e)
{
    string account = txtAccount.Text.Trim();
    string password = txtPassword.Text.Trim();
    bool rememberMe = chkRememberMe.Checked;

    if (ValidateUser(account, password))
    {
        // 获取验证通过的用户对象
        UserInfo user = System.Net.Mail.MailAddress.TryCreate(account, out _) 
            ? UserInfoProvider.GetUserInfoByEmail(account) 
            : UserInfoProvider.GetUserInfo(account);

        // 生成认证票据,完成登录
        FormsAuthentication.SetAuthCookie(user.UserName, rememberMe);
        
        // 跳转到登录前的页面或默认首页
        string returnUrl = Request.QueryString["ReturnUrl"] ?? "/";
        Response.Redirect(returnUrl);
    }
    else
    {
        // 显示错误提示
        lblLoginError.Text = "账号或密码不正确,请重试";
    }
}

如果你用的是Kentico MVC

在登录Controller里处理POST请求,逻辑类似,只是用MVC的方式接收参数:

[HttpPost]
public ActionResult Login(LoginViewModel model)
{
    if (!ModelState.IsValid)
    {
        return View(model);
    }

    if (ValidateUser(model.Account, model.Password))
    {
        UserInfo user = System.Net.Mail.MailAddress.TryCreate(model.Account, out _)
            ? UserInfoProvider.GetUserInfoByEmail(model.Account)
            : UserInfoProvider.GetUserInfo(model.Account);

        // 登录用户(用Kentico的AuthenticationManager)
        AuthenticationManager.SignIn(new AuthenticationParameters(user)
        {
            Persistent = model.RememberMe
        });

        return RedirectToAction("Index", "Home");
    }

    ModelState.AddModelError("", "账号或密码错误");
    return View(model);
}

3. 关键注意事项

  • 保证UserName唯一性:确保CMS_User表的UserName列没有重复值,否则可能出现多个用户同名导致登录匹配错误的问题。
  • 复用Kentico内置密码逻辑:一定要用UserInfoProvider.ValidateUserPassword来验证密码,不要自己写哈希逻辑,避免和Kentico默认的加密方式不兼容。
  • 兼容原有登录流程:这个方案完全兼容原来的邮箱登录,不会影响老用户的使用习惯。

内容的提问来源于stack exchange,提问作者krishnan

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.13 09:05:30