簡體   English   中英

如何在ASP.NET MVC 4簡單成員資格中使用“電子郵件”而不是“用戶名”登錄

[英]How to sign in with “E-Mail” instead of “UserName” in ASP.NET MVC 4 Simple Membership

我正在使用ASP.NET MVC 4開發入門級應用程序。該應用程序將具有成員資格系統。 我想使用集成的SimpleMembershipProvider,因為創建一個新的對我來說很復雜。 因此,用戶必須使用電子郵件地址而不是SimpleMembershipProvider中的用戶名登錄應用程序。 但是我不能做這個改變。 唯一的方法就是創建自己的會員資格提供程序嗎?

使用電子郵件查找用戶的ID。 將該用戶傳遞給成員身份,並以此驗證用戶身份。

您可能還希望在注冊用戶時防止重復的電子郵件,以確保1封電子郵件返回1個帳戶。 以下帖子將有助於解決這個問題:

當用戶使用默認的MVC成員資格提供程序注冊后修改其電子郵件地址時,如何防止重復的電子郵件地址?

另外,您可以在表格中為電子郵件創建唯一的密鑰,以確保不會注冊重復的電子郵件。

在AccountModels類(Models.AccountModels)中,您必須更新字段,即引入如下的電子郵件屬性

 public class RegisterModel
{
    [Required]
    [Display(Name = "User name")]
    public string UserName { get; set; }

    // Make change here 
    [Required]
    [Display(Name = "Your Email")]
    public string  Email { get; set; }

    [Required]
    [StringLength(100, ErrorMessage = "The {0} must be at least {2} characters long.", MinimumLength = 6)]
    [DataType(DataType.Password)]
    [Display(Name = "Password")]
    public string Password { get; set; }

    [DataType(DataType.Password)]
    [Display(Name = "Confirm password")]
    [Compare("Password", ErrorMessage = "The password and confirmation password do not match.")]
    public string ConfirmPassword { get; set; }
}

獲得電子郵件后,可以在登錄視圖中使用電子郵件,首先將LoginModel類更改為以下內容

    public class LoginModel
{
    [Required]
    [Display(Name = "Your Email")]
    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; }
}

當然,當用戶注冊時,電子郵件將從用戶那里獲取,因此對RegisterModel類進行如下更改

現在您已經登錄並注冊了。 然后,將最終存儲在數據庫中的實際數據在UserProfile類中定義,因此,我們向其中添加和Email字段。

    [Table("UserProfile")]
public class UserProfile
{
    [Key]
    [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
    public int UserId { get; set; }
    public string UserName { get; set; }
    public string Email  { get; set; }
}

現在您完成了更新模型。 但這現在仍然有效,因為當您單擊注冊時,它實際上會添加userName和Password並使用該對進行身份驗證。 現在讓我們進行更改。 導航到Controllers.AccountController.cs並找到以下函數並進行更新。 WebSecurity.CreateUserAndAccount(model.Email,model.Password); WebSecurity.Login(model.Email,model.Password);

//
    // POST: /Account/Register

    [HttpPost]
    [AllowAnonymous]
    [ValidateAntiForgeryToken]
    public ActionResult Register(RegisterModel model)
    {
        if (ModelState.IsValid)
        {
            // Attempt to register the user
            try
            {
                WebSecurity.CreateUserAndAccount(model.Email, model.Password);
                WebSecurity.Login(model.Email, model.Password);
                return RedirectToAction("Index", "Home");
            }
            catch (MembershipCreateUserException e)
            {
                ModelState.AddModelError("", ErrorCodeToString(e.StatusCode));
            }
        }

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

最后,當然,更改登錄操作如下。

        //
    // POST: /Account/Login

    [HttpPost]
    [AllowAnonymous]
    [ValidateAntiForgeryToken]
    public ActionResult Login(LoginModel model, string returnUrl)
    {
        if (ModelState.IsValid && WebSecurity.Login(model.Email, model.Password, persistCookie: model.RememberMe))
        {
            return RedirectToLocal(returnUrl);
        }

        // If we got this far, something failed, redisplay form
        ModelState.AddModelError("", "The Email or password provided is incorrect.");
        return View(model);
    }

這應該允許您將電子郵件和密碼用作登錄對。

一個簡單的選擇:在Models / AccountViewModels.cs文件中,您會找到許多包含如下字段的類:

[Required]
[EmailAddress]
[Display(Name = "Email")]
public string Email { get; set; }

為了提高速度,請將其更改為:

[Required]
[Display(Name = "Username")]
public string Email { get; set; }

從您的代碼中,您知道Email字段實際上是用戶名,因此您不必在任何地方進行更改。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM