当加入会话cookie来ServiceStack nullreference例外 [英] nullreference exception when adding session cookie to ServiceStack
问题描述
这个问题依赖于相当多的问题/答案在这里:
This question relies quite a bit on the question/answer here:
<一个href=\"http://stackoverflow.com/questions/19160839/reconnecting-to-servicestack-session-in-an-asp-net-mvc4-application\">Reconnecting在一个asp.net应用MVC4 Servicestack会议
Reconnecting to Servicestack session in an asp.net MVC4 application
基本上,我有一个使用ServiceStack进行身份验证一个asp.net MVC应用程序。所建议在上述我加入会话cookie到响应,然后将它添加到jsonserviceclient饼干容器后述的问题。
Basically, I have an asp.net MVC application that is using ServiceStack for authentication. As suggested in the question mentioned above I am adding a session cookie to the response and then adding it to the jsonserviceclient cookie container later.
使用这个code在登录屏幕的用户进行身份验证:
Using this code to authenticate the user at the login screen:
[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();
}
不过,我正从线空裁判异常:
However, I am getting a null ref exception from the line:
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
我想知道如果有什么似乎明显不对的配置。
I'm wondering if anything seems obviously wrong with this configuration.
更新/注意
我已经改变了违规线(凡nullref被抛出)为:
Update/note I have changed the offending line (Where the nullref is thrown) to:
response.Cookies.AddSessionCookie("foo", "bar", false);
我仍然得到同样的错误。刚刚打猎什么为空,为什么
I still get the same error. Just hunting for what is null and why
推荐答案
而不是:
response.Cookies.AddSessionCookie(
SessionFeature.PermanentSessionId, AuthResponse.SessionId);
尝试:
response.SetCookie(SessionFeature.PermanentSessionId,
AuthResponse.SessionId, TimeSpan.FromDays(14)));
这篇关于当加入会话cookie来ServiceStack nullreference例外的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!