这个问题很大程度上依赖于这里的问题/答案:
在 asp.net MVC4 应用程序中重新连接到 Servicestack 会话
基本上,我有一个使用 ServiceStack 进行身份验证的 asp.net MVC 应用程序。正如上面提到的问题中所建议的,我将会话 cookie 添加到响应中,然后稍后将其添加到 jsonserviceclient cookie 容器中。
使用此代码在登录屏幕上对用户进行身份验证:
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult login(UserModel user)
{
try
{
var AuthResponse = client.Post(new Auth
{
provider = "credentials",
UserName = user.user_id,
Password = user.password,
RememberMe = true
});
if (AuthResponse.SessionId != null)
{
FormsAuthentication.SetAuthCookie(AuthResponse.UserName, true);
var response = System.Web.HttpContext.Current.Response.ToResponse();
response.Cookies.AddSessionCookie(
SessionFeature.PermanentSessionId, AuthResponse.SessionId);
return Redirect("/Default");
}
}
catch (Exception ex)
{
FormsAuthentication.SignOut();
return Redirect("/");
}
return View();
}
但是,我从该行得到一个空引用异常:
response.Cookies.AddSessionCookie(SessionFeature.PermanentSessionId, AuthResponse.SessionId);
堆栈跟踪如下所示:
at ServiceStack.ServiceHost.Cookies.ToHttpCookie(Cookie cookie)
at ServiceStack.ServiceHost.Cookies.AddCookie(Cookie cookie)
at ServiceStack.ServiceHost.Cookies.AddSessionCookie(String cookieName, String cookieValue, Nullable`1 secureOnly)
at FmStoreScreenMvc3.Controllers.AuthController.login(UserModel user) in <project> 51
我想知道这个配置是否有明显的问题。
更新/注意 我已将违规行(抛出 nullref 的位置)更改为:
response.Cookies.AddSessionCookie("foo", "bar", false);
我仍然得到同样的错误。只是寻找什么是空的以及为什么