[英]ASP.NET MVC4 Simplemembership or autoFormsAuthentication?
[英]Inconsistent display names of user account in ASP.NET MVC4 SimpleMembership
我正在使用Visual Studio 2012建立一個ASP.NET MVC4 Internet網站。
VS2012默認情況下會生成一個模板網站,並實現了SimpleMembership。
SimpleMembership功能非常方便,希望有一件事讓我感到困惑:
例如,我創建了一個用戶帳戶,用戶名為“ Miles”,然后使用名稱“ Miles”登錄,一切都很好。 然后我注銷並使用名稱“ miles”(全部小寫)登錄,登錄也成功,但是用戶名顯示為“ miles”。 更具體地說,User.Identity.Name的值是“ miles”,而不是數據庫中的“ Miles”。
同樣,我可以使用“ miLes”,“ mILes”,“ MILES”等進行登錄,並且用戶名將相同。 常識是,如果身份驗證不區分大小寫,則用戶名應與數據庫中的用戶名完全相同,在我的情況下為“ Miles”,而不是我在登錄文本框中鍵入的用戶名。
有誰知道如何解決這個問題? 謝謝!
在您的AccountController中,您可能會默認看到類似以下內容:
[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public ActionResult Login(LoginModel model, string returnUrl)
{
if (ModelState.IsValid && WebSecurity.Login(model.UserName, model.Password, persistCookie: model.RememberMe))
{
return RedirectToLocal(returnUrl);
}
// If we got this far, something failed, redisplay form
ModelState.AddModelError("", "The user name or password provided is incorrect.");
return View(model);
}
WebSecurity.Login()
(在您在表單中鍵入的模型上傳遞確切的字符串)。
如果查看WebSecurity.cs的源代碼,您將看到FormsAuthentication.SetAuthCookie()
被調用(再次,仍然傳遞確切的類型化字符串)。 在此方法實際在響應中設置cookie之前,它將執行GetAuthCookie()
,仍然傳遞您登錄時鍵入的確切字符串。
實際上,此方法再次使用您鍵入的數據來構建身份驗證HttpCookie
對象。
如果需要與數據庫中的名稱完全匹配的名稱,則需要從數據庫中查詢記錄,並將該值傳遞給WebSecurity.Login()
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.