我试图使用会话,但是当我打电话SetSession方法,我看到HttpContext.Current是空的,所以我得到MINVALUE(从gettor因为会话属性为null)如何解决这个问题?正因为如此,每次我试图让会话变量,它不工作,的确= D它看起来我忘了的东西,但...
I'm trying to use session but when I call the SetSession method, I see that HttpContext.Current is null, so I get MinValue (from gettor because session property is null) how to fix this issue? because of that, everytime I'm trying to get session variables, it doesn't work, indeed =D it looks I forgot something but...
从控制器我打电话时,用户连接的是SessionManager:
From the controller I call the SessionManager when user is connecting:
var result = await SignInManager.PasswordSignInAsync(model.Email, model.Password, model.RememberMe, shouldLockout: false); switch (result) { case SignInStatus.Success: { var identityUser = UserManager.FindByEmail(model.Email); if (identityUser.Matricule.IsNotNull() && !identityUser.Matricule.HasValue) { SetAlert("Error"); } else { SessionManager.matricule = identityUser.Matricule.Value; var user = BL.PersonnelBL.GetAll(SessionManager.matricule); SessionManager.firstName = user.prenom; SessionManager.lastName = user.nom; SessionManager.chainId = user.chaine.chaine; SessionManager.secteurId = user.chaine.secteur.Id; SessionManager.serviceId = user.chaine.service.Id; SessionManager.isAuditor = user.auditTarget.IsNull() ? false : true; } // Redirect to local return RedirectToLocal(returnUrl); } case SignInStatus.LockedOut: return View("Lockout"); case SignInStatus.RequiresVerification: return RedirectToAction("SendCode", new { ReturnUrl = returnUrl }); case SignInStatus.Failure: default: ModelState.AddModelError("", "Invalid login attempt."); return View(model); }
会话管理器:
Session manager:
public class SessionManager { public static int matricule { get { object property = GetSession("MATRICULE"); if (property != null) { return Convert.ToInt32(property); } return int.MinValue; } set { SetSession("MATRICULE", value); } } } private static bool SetSession(string key, object value) { if (HttpContext.Current != null && HttpContext.Current.Session != null) { HttpContext.Current.Session[key] = value; return true; } return false; }
里面的Web.Config:
Inside Web.Config:
<system.web> ... <sessionState mode="InProc" timeout="45" /> </system.web>
在此先感谢
推荐答案有几个原因,你的 HttpContext.Current 可能是空:
There are a few reasons your HttpContext.Current could benull:
您运行此code:
- 在工作或其它后台线程;
- 从 Session_End中事件;
- 在 HTTP模块或的HttpHandler 不支持会话(请参阅 IRequiresSessionState 标志接口)。
- in a Task or another background thread;
- from the Session_End event;
- in a HttpModule or HttpHandler that doesn't support sessions (see the IRequiresSessionState flag interface).
更多推荐
如何使用会话HttpContext.Current在空
发布评论