我在使用现有 AccountController(由 MVC 制作)测试登录方法时遇到问题...
我有这个简单的测试方法:
[TestMethod]
public void LogOnTest1() {
AccountController controller = new AccountController();
LogOnModel logonModel = new LogOnModel();
logonModel.UserName = "test";
logonModel.Password = "test1234";
if ( controller.MembershipService == null ) {
controller.MembershipService = new AccountMembershipService();
}
if ( controller.FormsService == null ) {
controller.FormsService = new FormsAuthenticationService();
}
var result = controller.LogOn( logonModel, "" ) as ViewResult;
Assert.AreEqual( "Index", result.ViewName );
}
和中定义的方法AccountController
:
[HttpPost]
public ActionResult LogOn( LogOnModel model, string returnUrl ) {
if ( ModelState.IsValid ) {
if ( MembershipService.ValidateUser( model.UserName, model.Password ) ) {
FormsService.SignIn( model.UserName, model.RememberMe );
if ( !string.IsNullOrEmpty( 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 );
}
上述方法不是我定义/修改的。刚刚在创建 asp.net mvc 项目时创建。
问题出在一线
if ( MembershipService.ValidateUser( model.UserName,model.Password ) ) {
尽管我提供了正确的登录信息,但它总是返回false。
我的错误在哪里?