我正在使用內置的「Internet應用程序」模板啓動一個全新的ASP.NET MVC應用程序。在ASP.NET Forms身份驗證中使用電子郵件地址而不是用戶名
我該如何設置它以允許使用電子郵件地址登錄&而不是用戶名爲&的密碼?
我正在使用內置的「Internet應用程序」模板啓動一個全新的ASP.NET MVC應用程序。在ASP.NET Forms身份驗證中使用電子郵件地址而不是用戶名
我該如何設置它以允許使用電子郵件地址登錄&而不是用戶名爲&的密碼?
解決方案與使用名稱字段作爲電子郵件地址一樣簡單嗎?
如果您使用的是默認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; }
}
的UserName
變化發生於Email
在LogOn
視圖中。
最後,在你的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);
}
我們有相同的要求,爲此, 您應該創建自定義身份(CustomIdentity:IIdentity,ISerializable)
[Serializable]
public class CustomIdentity : IIdentity, ISerializable
併爲其定義Name屬性。
然後爲用戶名返還財產的電子郵件get方法
public string Name
{
get{ return Email; }
}
如果需要詳細的,請對這個職位
是的,我沒想到的是評論,但我想用用戶保持name屬性在網站上顯示他們的名字,例如「歡迎用戶名」,而不是「歡迎使用[email protected]」。 – 2011-01-14 17:12:19