在web.config中进行表单身份验证
我正在使用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");
    }
}
                        链接地址: http://www.djcxy.com/p/42075.html
                        上一篇: Forms authentication in web.config
下一篇: Asp.Net Forms Authentication when using iPhone UIWebView
