(ASP.NET MVC) Create Session variable

Asked

Viewed 931 times

1

I have an ASP.NET MVC application that is published on two IIS servers (homologation and production);

In this application, I use the Session variable to save the user’s login in a part of it, however, in the production server is not working the creation of the Session variable, returning me the error: object reference not set to an instance of an object

I believe it may be some configuration on IIS because the same code works on the homologation server and also on localhost, so I can’t debug.

I never made configuration in IIS, but while studying briefly about it, I found that there is a configuration for Session State, as shown below, however, by default, it is already set to True and even changing to False, still the same error.

Follow code line for analysis:

// POST: Auth/Login/{login}
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Login(DTOViewModel login)
{
    try
    {
        this.Autenticar(login.Usuario, login.Senha);
        Session["user"] = login.Usuario; // <---- aqui que está dando problema
        return Json(new { url = Url.Action("Index", "Usuario/Home") });
    }
    catch (Exception ex)
    {
        return Json(new { erro = ex.Message });
    }
}

Follow IIS configuration image:

Definição de Sessão no IIS

Thanks to those who can help. :)

  • It seems to me a common balancing behavior, you may be creating the session on one server and the next request is answered by the other, where this session was not created.

  • I’m not sure I understand your reply @Leandroangelo, could you enlighten me? From what I understand the session variable may not have been created at this time of the application, but what confuses me is the homologation server works normally.

  • Forget it, I read the beginning of your question wrong.

1 answer

0


In case anyone comes to have the same problem I had, I will leave documented the solution:

I asked the same question in Oveflow Stack in English and follows the link with the solution: Solution

Basically, the change that should be made in the web.config

<configuration> 
    <system.webServer>
        <modules>
          <remove name="Session" />
          <add name="Session" type="System.Web.SessionState.SessionStateModule"/>
        </modules>   
    </system.webServer> 
</configuration>

Browser other questions tagged

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