clement
clement

Reputation: 4266

how to use session with HttpContext.Current at null

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...

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

Inside Web.Config:

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

Thanks in advance

Upvotes: 1

Views: 1629

Answers (1)

Patrick Hofman
Patrick Hofman

Reputation: 156918

There are a few reasons your HttpContext.Current could benull:

You are running this code:

  • 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).

Upvotes: 1

Related Questions