如何使用会话HttpContext.Current在空

编程入门 行业动态 更新时间:2024-10-25 00:37:05
本文介绍了如何使用会话HttpContext.Current在空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我试图使用会话,但是当我打电话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在空

本文发布于:2023-11-09 15:54:46,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1572790.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:如何使用   HttpContext   Current

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!