我正在使用MVC3,并将用户认证放在web.config文件中。这是绕过sqlserver身份验证。
代码如下web.config中所示:
<authentication mode="Forms"> <forms loginUrl="~/Account/LogOn" timeout="2880" > <credentials passwordFormat="Clear"> <user name="test123" password="test123" /> </credentials> </forms> </authentication>
我尝试使用提到的用户标识和密码进行登录,我在页面中出现错误
登录失败。请更正错误,然后重试。
* The user name or password provided is incorrect.
当我调试到AccountController.cs文件,失败在MembershipService.ValidateUser(model.UserName,model.Password)方法。
解决方法
如果您检查标准的ASP.NET MVC 3 AccountController.cs和AccountModels.cs文件,您将学习内部使用的
MembershipProvider.ValidateUser方法(通过
Membership.Provider)。如果要在web.config中存储密码,则应该使用
FormsAuthentication.Authenticate方法。
例如:
public class AuthorizationController : Controller { public ActionResult LogOn() { return View("LogOn"); } [AcceptVerbs(HttpVerbs.Post)] public ActionResult LogOn(string userName,string password,bool rememberMe,string returnUrl) { if (!ValidateLogOn(userName,password)) return View("LogOn"); FormsAuthentication.SetAuthCookie(userName,rememberMe); if (!string.IsNullOrEmpty(returnUrl)) return Redirect(returnUrl); else return RedirectToAction("Index","News"); } private bool ValidateLogOn(string userName,string password) { if (string.IsNullOrEmpty(userName)) ModelState.AddModelError("username","User name required"); if (string.IsNullOrEmpty(password)) ModelState.AddModelError("password","Password required"); if (ModelState.IsValid && !FormsAuthentication. Authenticate(userName,password)) ModelState.AddModelError("_FORM","Wrong user name or password"); return ModelState.IsValid; } public RedirectToRouteResult LogOff() { FormsAuthentication.SignOut(); return RedirectToAction("LogOn"); } }
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。