Httpcontext.Current.Session - Null

Asked

Viewed 648 times

3

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

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

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

HttpContext.Current.Session["UsuarioLogado"]

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

I’m really lost on the reason for this mistake...

1 answer

2

When updating your system, IIS expires all active sessions. This behavior is correct and predicted. It makes no sense to keep sessions from a outdated system version.

The simplest fix is in checking that your key is not null:

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

The performance fix to your problem is to abandon permission by Session and write your own authorization attribute.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.