【发布时间】:2018-03-27 20:34:20
【问题描述】:
我的应用程序中有以下代码。 Constants.SESSION_USER 是一个字符串值。
public class BaseController : Controller
{
public Account UserAccount
{
get
{
if (Session[Constants.SESSION_USER] is User)
{
return Session[Constants.SESSION_USER] as User;
}
return null;
}
set
{
Session[Constants.SESSION_USER] = value;
}
}
}
以下是我将用户设置为Session
public class AccountController : BaseController
{
[HttpPost]
public JsonResult Login(LoginViewModel loginView)
{
User loggedInAccount = null;
// some logic to validate the user
// if no validation issues
User loggedAccount = new User(loginView.ID);
//set the logged account into the session
this.UserAccount = loggedAccount;
}
}
在下面的代码中,我正在访问我之前在AccountController 中设置的用户会话
[HttpPost]
public JsonResult GetSession()
{
User loggedInAccount = null;
if (this.UserAccount != null) // this.UserAccount is null
{
//get the user from the session
loggedInAccount = this.UserAccount;
}
}
当通过 ajax 请求调用此 GetSession() 方法时,this.UserAccount 为 null。
我有点困惑,因为有时这有效,有时无效。这里有什么问题?
【问题讨论】:
-
您的 AJAX 请求不发送会话 cookie。如果您不想解决安全问题,请不要重新发明身份验证。
-
@CodeCaster: session cookie 是在 ajax 请求中自动发送的,不是吗?
-
如果是 HttpOnly cookie,则不会。我们真的不能说什么,因为它都是定制的。
-
@CodeCaster:ASP.NET 会话 cookie 是一个 HttpOnly cookie。我在 angularjs 中使用 ajax 请求调用这个 GetSesssion() 方法。 ajax 请求没有会话 cookie。我必须附上它,你能帮我解决这个问题吗?。
-
谁能帮帮我
标签: ajax asp.net-mvc asp.net-mvc-4 session session-variables