Losing Session after Request

Asked

Viewed 540 times

0

Good guys, I have a problem that so far could not solve. It only starts to occur after a ajax request, in which I submit a document and return to url in a json, to be caught in the javascript, be assigned to a link and be downloaded.

How it happens: after generating the first report, if the user tries to create another one soon after, the application kind that calls the Application_start again, and thus losing the "linkage" that had the Session. The funny thing is that in the browser (I’m using the Chrome), Session continues to appear in Cookies.

Another interesting fact, is that when I click on the link to download, it also calls again the Application_start...

NT: Utilizo ASPNET MVC 5

Example of how I create the Sessions:

    [HttpPost]
    [AllowAnonymous]
    public ActionResult LoginRequest(string login, string pass)
    {
        string loginCache = "usuario";
        string senhaCache = "123123";

        if (login == loginCache && pass == senhaCache)
        {
            System.Web.HttpContext.Current.Session["Nome"] = "Admin";
            System.Web.HttpContext.Current.Session["Email"] = loginCache;

            return Json(
                new
                {
                    sucesso = true,
                    href = "GerarRelatorio/Index"
                }
            );
        }
        else
        {
            return Json(
                new {
                    sucesso = false,
                    msg = "Login e/ou Senha estão incorretos."
                }    
            );
        }

Example of how I’m returning the url:

        var newCaminhoContent = Path.Combine(urlDownloadFolder, arquivoModel.Nome);

        var urlDownloadHTML = ( newCaminhoContent.Substring(newCaminhoContent.IndexOf(@"Content")) );

        return Json(new
        {
            hasError = false,
            msg = "O arquivo foi gerado com sucesso!",
            href = urlDownloadHTML,
            nome = arquivoModel.Nome,
            ext = arquivoModel.ExtensaoString
        });

Example of how to get the url in javascript:

$.post(
    AppConfig.baseUrl + "/GerarRelatorio/Gerar",
    {
        cm: cm,
        tr: tr
    },
    function (data, status) {


        console.log(data);

        try {
            if (data.hasError === false) {

                getTimeToShowLink(data);

                //var win = window.open(AppConfig.baseUrl + data.href);
                //win.focus();

                //$('#modal-status').modal('hide');
            }
            else {
                $('#div-modal-loading').hide();
                $('#div-modal-msg').html(data.msg);                    
            }
        }
        catch (e) {
            console.log(e);
        }
        finally {
            //$btn.button('reset');
            $('#modal-status').modal('hide');
        }
    }
);

function getTimeToShowLink(data) {

    $('#btn-download')
       .attr('href', AppConfig.baseUrl + data.href)
       .attr('download', data.nome)
       .show()

    var tempo = 10;
    var sec = 0;

    var interval = setInterval(function () {

        sec = pad(++sec % 60);

        if (parseInt(sec) <= parseInt(tempo)) {
            $("#div-msg-download").html(
                'Se o download não começar automaticamente em até ' + (tempo - sec) + ' segundos, clique aqui...');
        }
        else {
            $('#div-msg-download').html('Clique aqui para fazer o download...');
            clearInterval(interval);
        }

    }, 1000);
}
  • 1

    From what I understand, you’re calling the application on Cors. We understand that js is on the client machine and that the session cookie is not sent at the time of the ajax request, so the system understands that it is another client requesting...

  • Good, but you know how this situation could be corrected?

  • It depends on several things, but you can try taking concatenation from endpoint, if the address is the same clear. It would be possible?

  • How could I do that?

  • For example, here: Appconfig.baseUrl + "/Gerarrelatorio/Gerar", it would look like this: "/Gerarrelatorio/Gerar". However, once again. It depends on how your environment was set up, how your logic was built...

  • I get it... I’ll test it here.

Show 1 more comment

1 answer

0


Good guys, I just needed to change the httpRuntime to make it work. Apparently the application for some reason, updated the web config. and that made the pool of IIS restarted every time a new request was made, calling multiple times the method Application_start.

Before:

<httpRuntime targetFramework="4.6.1" />

Afterward:

<httpRuntime fcnMode="Disabled" targetFramework="4.6.1" />

That kind of prevents the web config. to be updated, avoiding the App pool from restarting the website.

Browser other questions tagged

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