Asp.Net身份 - 不区分大小写的电子邮件和用户名 [英] Asp.Net Identity - case insensitive email and usernames

查看:137
本文介绍了Asp.Net身份 - 不区分大小写的电子邮件和用户名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法让Asp.Net身份来区分使用电子邮件地址和用户名?

Is there a way to get Asp.Net Identity to be case insensitive with email addresses and usernames?

目前,如果我称之为FindByEmailAsync(电子邮件),它只会工作,如果电子邮件地址被存储正是因为它是键入的时刻(区分大小写)

At the moment if I call "FindByEmailAsync(email)" it will only work if the email address is being stored exactly as it's is typed (case sensitive)

推荐答案

您可以更改用户是如何注册,以便用户名设置为小写,并在同时登录时。

You can change how the user is registered so that the username is set to lowercase and when logging in as well.

有关注册用户,在的AccountController

For registering the user, in the AccountController

    [HttpPost]
    [AllowAnonymous]
    [ValidateAntiForgeryToken]
    public async Task<ActionResult> Register(RegisterViewModel model)
    {
        if (ModelState.IsValid)
        {
            var user = new ApplicationUser() { UserName = model.Email.ToLowerInvariant(), Email = model.Email };
            IdentityResult result = await UserManager.CreateAsync(user, model.Password);
            if (result.Succeeded)
            {
                await SignInAsync(user, isPersistent: false);

                return RedirectToAction("Index", "Home");
            }
            else
            {
                AddErrors(result);
            }
        }

和在日志记录:

    [HttpPost]
    [AllowAnonymous]
    [ValidateAntiForgeryToken]
    public async Task<ActionResult> Login(LoginViewModel model, string returnUrl)
    {
        if (ModelState.IsValid)
        {
            var user = await UserManager.FindAsync(model.Email.ToLowerInvariant(), model.Password);
            if (user != null)
            {
                await SignInAsync(user, model.RememberMe);
                return RedirectToLocal(returnUrl);
            }
            else
            {
                ModelState.AddModelError("", "Invalid username or password.");
            }
        }

        // If we got this far, something failed, redisplay form
        return View(model);
    }

这篇关于Asp.Net身份 - 不区分大小写的电子邮件和用户名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆