Webmethods with HttpContext.Current.User.Identity.IsAuthenticated stop working after inactivity on Azure
Asked Answered
S

1

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?

Skewback answered 17/5, 2015 at 9:28 Comment(20)
Is there any particular reason you put user authentication on method instead of in Global.asax or some base class?Goebel
I'm checking if a user is authenticated or not in order to create strings accordingly in those webmethodsSkewback
So what do you mean by stop working? Do you mean it always enter the else logic when the authentication stop working?Goebel
No it doesn't even enter the else logic.... just nothing happens when I click a button that fires a functionSkewback
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?Goebel
Yes jQuery Ajax (json)...Skewback
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?Armada
I've edited my question and added the Ajax function that calls the WebMethod....Skewback
add an error handler/callback to your $.ajax call, and give some idea what the result is.Rafael
why is the question tagged asp.net-identity?Rafael
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.Hargrave
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....Skewback
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)Maine
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 errorSkewback
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??Skewback
Your Web Methods probably don't share session with your applicationMaine
May this url helps you about session management in Azure. simple-talk.com/cloud/platform-as-a-service/…Baur
@Skewback Please post your solution as an answer and accept it so that the Question remains in the SE format and becomes resolved. Thanks.Find
Can I accept my own question as an answer?Skewback
No, just copy your comment (and what the problem was) into the "your Answer' section below, then accept it when the page refreshesBuenabuenaventura
S
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.

Skewback answered 11/6, 2015 at 14:40 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.