29

I'm testing the Azure server with pages that use Ajax(json)/Webmethod functions.

Some of those functions check HttpContext.Current.User.Identity.IsAuthenticated before they run code. Unfortunately, if a user is logged in and the page doesn't make a full postback request to the server, only those webmethods functions that check HttpContext.Current.User.Identity.IsAuthenticated stop running completely after couple of minutes without giving any error. They don't even run the else code block (see below).

I've tested those pages on a local server and everything worked fine as it should, even after a long period of inactivity. Here is an example of a webmethod

[WebMethod]
public static string serviceMenu(int IDservice)
{
        StringBuilder SBphotoMenu = new StringBuilder();            
        if (HttpContext.Current.User.Identity.IsAuthenticated)
        {
            // Do stuff
        }
        else
        {
           // Do other stuff
        }

        return SBphotoMenu.ToString();
}

I'm calling the webmethod as follows:

function serviceMenu(IDservice) {
$.ajax({
    type: "POST",
    url: "/UserControls/serviceMenu",
    data: "{ IDservice: " + IDservice }",
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    success: function (data) {
        // Do Stuff        
    }
})
}

This behavior occurs only if the user is logged in. Now if the user is not logged in then all functions work properly even on Azure.

As a matter of fact, when the webmethods stop running and I refresh the page the user is still logged in and the webmethods start running again but for couple of minutes only and then the same behavior occurs again.

What's going wrong?

Gloria
  • 1,305
  • 5
  • 22
  • 57
  • Is there any particular reason you put user authentication on method instead of in Global.asax or some base class? – Martin Valentino May 17 '15 at 09:43
  • I'm checking if a user is authenticated or not in order to create strings accordingly in those webmethods – Gloria May 17 '15 at 09:55
  • So what do you mean by stop working? Do you mean it always enter the ```else``` logic when the authentication stop working? – Martin Valentino May 17 '15 at 09:57
  • No it doesn't even enter the else logic.... just nothing happens when I click a button that fires a function – Gloria May 17 '15 at 10:39
  • How do you call the method? Are you using jquery ajax? Can you put some code on how do you call it? Btw, what authentication do you use? Do you store it on the session? – Martin Valentino May 17 '15 at 10:44
  • Yes jQuery Ajax (json)... – Gloria May 17 '15 at 10:46
  • What is the result of the jQuery call? I'd like to think that you're getting a 401 when things stop working. Can you post the code that makes the ajax call? – Brendan Green May 20 '15 at 06:08
  • I've edited my question and added the Ajax function that calls the WebMethod.... – Gloria May 20 '15 at 06:16
  • add an error handler/callback to your $.ajax call, and give some idea what the result is. – JJS May 20 '15 at 18:32
  • why is the question tagged asp.net-identity? – JJS May 20 '15 at 19:09
  • What's wrong is that you either don't have any logging configured, or you're not checking the configured logging. The method is not failing "without giving any error". It's giving an error, and you don't know it. – John Saunders May 20 '15 at 22:01
  • Honestly I don't know how to get the error because when I debug locally the webmethods run perfectly well even after long inactivity. It's only when I test on Azure.... an error handler/callback in the $.ajax call doesn't return any error.... – Gloria May 21 '15 at 05:20
  • Gloria, open up crome or firefox debugger. Check the result of your returned xhr code. On `success` do a `console.log(data)`. on error, do a `console.log(status)` – Dave Alperovich May 21 '15 at 05:22
  • I tried the console.log(data + status + jqXHR) on Ajax error and checked it with Explorer Developer tab. I only got "[object Object]errorInternal Server Error" and no details about the error – Gloria May 21 '15 at 06:14
  • Well after many tests it seems that the session variables are not persisting on the Server.... After couple of minutes of inactivity all session variables return null or empty strings although the session hasn't expired on the Azure server. Does anyone know how to deal with session variables on Azure?? – Gloria May 21 '15 at 06:43
  • Your Web Methods probably don't share session with your application – Dave Alperovich May 22 '15 at 04:48
  • May this url helps you about session management in Azure. https://www.simple-talk.com/cloud/platform-as-a-service/managing-session-state-in-windows-azure-what-are-the-options/ – jignesh May 29 '15 at 15:00
  • @Gloria Please post your solution as an answer and accept it so that the Question remains in the SE format and becomes resolved. Thanks. – Michael Coxon Jun 05 '15 at 11:20
  • Can I accept my own question as an answer? – Gloria Jun 08 '15 at 05:17
  • No, just copy your comment (and what the problem was) into the "your Answer' section below, then accept it when the page refreshes – Ron DeFreitas Jun 09 '15 at 20:45

1 Answers1

0

The problem is caused by the session variables and not autentication. In fact the session state is not maitained for ASP.NET applications in Azure using the default "inProc" method. Azure uses other methods, of which some are costy: Table storage, SQL Azure, or Windows Azure Caching.

Gloria
  • 1,305
  • 5
  • 22
  • 57