2011-01-14 83 views

回答

0

解決方案與使用名稱字段作爲電子郵件地址一樣簡單嗎?

+0

是的,我沒想到的是評論,但我想用用戶保持name屬性在網站上顯示他們的名字,例如「歡迎用戶名」,而不是「歡迎使用[email protected]」。 – 2011-01-14 17:12:19

1

如果您使用的是默認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變化發生於EmailLogOn視圖中。

最後,在你的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); 
} 
1

我們有相同的要求,爲此, 您應該創建自定義身份(CustomIdentity:IIdentity,ISerializable)

[Serializable] 
public class CustomIdentity : IIdentity, ISerializable 

併爲其定義Name屬性。

然後爲用戶名返還財產的電子郵件get方法

public string Name 
    { 
     get{ return Email; } 
    } 

如果需要詳細的,請對這個職位

相關問題