如何在null时使用HttpContext.Current的会话

时间:2015-01-16 08:43:06

标签: c# asp.net .net asp.net-mvc asp.net-mvc-4

我正在尝试使用session但是当我调用SetSession方法时,我看到HttpContext.Current为null,所以我得到MinValue(来自gettor,因为session属性为null)如何解决这个问题?因此,每次我试图获取会话变量时,它都不起作用,确实= D它看起来我忘了一些东西但是......

在控制器中,我在用户连接时调用SessionManager:

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);
            }

会话经理:

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:

<system.web>
...
<sessionState mode="InProc" timeout="45" />
</system.web>

提前致谢

1 个答案:

答案 0 :(得分:1)

HttpContext.Current可能null有几个原因:

您正在运行此代码:

  • Task或其他后台主题中;
  • 来自Session_End事件;
  • 位于不支持会话的HttpModuleHttpHandler中(请参阅IRequiresSessionState标记界面)。