如何测试此代码(ASP.NET MVC 4、.NET 4.5 Web 应用程序中的登录方法):
public ActionResult Login(LoginModel model, string returnUrl)
{
if (ModelState.IsValid && _userCredentialsService.ValidateUser(model.UserName, model.Password))
{
SessionAuthentication.SetAuthCookie(model.UserName, _userCredentialsService.GetUserGroups(model.UserName), model.RememberMe);
return RedirectToLocal(returnUrl);
}
}
使用此 SetAuthCookie 方法:
public static void SetAuthCookie(IEnumerable<Claim> claims, bool isPersistent)
{
if (!HttpContext.Current.Request.IsSecureConnection && FormsAuthentication.RequireSSL)
{
throw new HttpException(500, "Connection is not secured with SSL");
}
var sessionToken = CreateSessionSecurityToken(CreatePrincipal(claims.ToList()), isPersistent);
FederatedAuthentication.SessionAuthenticationModule.WriteSessionTokenToCookie(sessionToken);
}
访问时出现空引用异常SessionAuthenticationModule
(该异常是在尝试获取 HttpContext 时提到的属性在内部引发的)。这是堆栈跟踪:
at System.IdentityModel.Services.FederatedAuthentication.GetHttpContextModule[T]()
at System.IdentityModel.Services.FederatedAuthentication.GetHttpModule[T]()
at System.IdentityModel.Services.FederatedAuthentication.get_SessionAuthenticationModule()
我在位于测试程序集中的 app.config 中制作了我的 web.config 的精确副本,并且代码在应用程序的正常运行时工作。
我同时使用了 HttpSimulator 和 MvcContrib 测试助手,以便设置 HttpContext 但仍然抛出异常。有没有人有关于如何测试它的解决方案或解决方法?