我正在使用 Visual Studio 2012 并尝试按照建议进行操作,但显示错误:
To call this method, the "Membership.Provider" property must be an instance of "ExtendedMembershipProvider".
所以我发现应该对带有MVC 4和实体框架的VS2012上的默认登录表单进行一些更改,如下所示:
在文件“AccountController.cs”上
关于“公共 ActionResult 登录(LoginModel 模型,字符串 returnUrl)”
更改
if (ModelState.IsValid && WebSecurity.Login(model.UserName, model.Password, persistCookie: model.RememberMe))
为了
if (ModelState.IsValid && Membership.Provider.ValidateUser(model.UserName, model.Password))
在“公共 ActionResult LogOff()”上
更改
WebSecurity.Logout();
为了
FormsAuthentication.SignOut();
并添加以下内容: FormsAuthentication.SetAuthCookie(model.UserName, false);
public ActionResult Login(LoginModel model, string returnUrl)
{
if (ModelState.IsValid && Membership.Provider.ValidateUser(model.UserName, model.Password))
{
FormsAuthentication.SetAuthCookie(model.UserName, false);
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);
}