在 ASP.NET Forms 身份验证中使用电子邮件地址而不是用户名

发布于 2024-10-11 22:24:46 字数 103 浏览 8 评论 0原文

我正在使用内置的“Internet 应用程序”模板启动一个全新的 ASP.NET MVC 应用程序。

我如何设置以允许使用电子邮件地址和电子邮件地址登录密码而不是用户名和密码?

I'm starting a brand new ASP.NET MVC application using the built in "Internet application" template.

How can I set it up to allow login using email address & password instead of username & password?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

小忆控 2024-10-18 22:24:46

如果您使用默认的 AccountModels,您不妨更改 LogOnModel 以删除 UserName 并添加 Email,像这样:

public class LogOnModel {
    [Required]
    [DataType(DataType.EmailAddress)]
    [Display(Name = "Email address")]
    public string Email { get; set; }

    [Required]
    [DataType(DataType.Password)]
    [Display(Name = "Password")]
    public string Password { get; set; }

    [Display(Name = "Remember me?")]
    public bool RememberMe { get; set; }
}

LogOn 视图中出现的 UserName 更改为 Email

最后,在您的 AccountController 中,更改 LogOn 操作以从电子邮件地址获取用户名,以完成 SignIn 流程,如下所示:

[HttpPost]
public ActionResult LogOn(LogOnModel model, string returnUrl) {
  if (ModelState.IsValid) {
    var userName = Membership.GetUserNameByEmail(model.Email);
    if (MembershipService.ValidateUser(userName, model.Password)) {
      FormsService.SignIn(userName, model.RememberMe);
      if (Url.IsLocalUrl(returnUrl)) {
        return Redirect(returnUrl);
      } else {
        return RedirectToAction("Index", "Home");
      }
    } else {
      ModelState.AddModelError("", "The user name or password provided is incorrect.");
    }
  }

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

If you're using the default AccountModels you might as well change the LogOnModel to remove UserName and add Email, like this:

public class LogOnModel {
    [Required]
    [DataType(DataType.EmailAddress)]
    [Display(Name = "Email address")]
    public string Email { get; set; }

    [Required]
    [DataType(DataType.Password)]
    [Display(Name = "Password")]
    public string Password { get; set; }

    [Display(Name = "Remember me?")]
    public bool RememberMe { get; set; }
}

Change occurrences of UserName to Email in your LogOn view as well.

Finally in your AccountController, change the LogOn action to get the UserName from the email address to complete the SignIn process, like this:

[HttpPost]
public ActionResult LogOn(LogOnModel model, string returnUrl) {
  if (ModelState.IsValid) {
    var userName = Membership.GetUserNameByEmail(model.Email);
    if (MembershipService.ValidateUser(userName, model.Password)) {
      FormsService.SignIn(userName, model.RememberMe);
      if (Url.IsLocalUrl(returnUrl)) {
        return Redirect(returnUrl);
      } else {
        return RedirectToAction("Index", "Home");
      }
    } else {
      ModelState.AddModelError("", "The user name or password provided is incorrect.");
    }
  }

  // If we got this far, something failed, redisplay form
  return View(model);
}
时光与爱终年不遇 2024-10-18 22:24:46

我们有同样的要求并这样做,
您应该创建自定义标识(CustomIdentity:IIdentity、ISerialized)

[Serializable]
public class CustomIdentity : IIdentity, ISerializable

并为其定义 Name 属性。

然后对于名称属性的获取方法返回用户的电子邮件

    public string Name 
    { 
        get{ return Email; }
    }

如果需要详细信息,请评论此帖子

we have same requirement and do this,
you should create custom identity (CustomIdentity : IIdentity, ISerializable)

[Serializable]
public class CustomIdentity : IIdentity, ISerializable

and define Name property for it.

then for get method of name property return email of user

    public string Name 
    { 
        get{ return Email; }
    }

if detail be needed, please comment on this post

蓬勃野心 2024-10-18 22:24:46

解决方案不是像使用姓名字段作为电子邮件地址一样简单吗?

Isn't the solution as simple as using the name field as an email address?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文