HttpContext.Current.Session - Null

1

For a project business rule, whenever an object of type ViewModelBase is instantiated, I need to perform some validations on the user profile to define which buttons the user will have access to in View that will be loaded.

For this, I created within the constructor of this class a call to the ObterPermissoes method, which uses as base the data stored in the user session.

The problem is that every time that I update a DLL on the server where the application is published (IIS), the value of the object of this session comes null on the users' first access, generating exception in the application

HttpContext.Current.Session["UsuarioLogado"]

The problem is that in%% of View , the same method is executed and the session loads normally. But when the user performs a query action, when passing the method index the session is null, even if previously, to load the screen, the information has already been read the correct way.

I'm really lost as to the reason for this error ...

    
asked by anonymous 08.08.2016 / 15:35

1 answer

0

When you upgrade your system, IIS exits all active sessions. This behavior is correct and predicted. It does not make sense to keep sessions from a lagged version of the system.

The simplest fix is to check if your key is not null:

if (HttpContext.Current.Session["UsuarioLogado"] != null) { ... }

The performative fix to your problem is to drop permission per session and write your own authorization attribute .

    
28.09.2016 / 20:35