使用HttpContext.Current.User.Identity.IsAuthenticated的Web方法在Azure上不活动后停止工作

Glo*_*ria 29 c# asp.net session-variables azure webmethod

我正在使用使用Ajax(json)/ Webmethod函数的页面测试Azure服务器.

其中一些函数HttpContext.Current.User.Identity.IsAuthenticated在运行代码之前检查.不幸的是,如果用户登录并且页面没有向服务器发出完整的回发请求,那么只有那些检查HttpContext.Current.User.Identity.IsAuthenticated在几分钟后完全停止运行的webmethods函数没有给出任何错误.他们甚至没有运行else代码块(见下文).

我已经在本地服务器上测试了这些页面,即使在长时间不活动之后,一切正常.这是一个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();
}
Run Code Online (Sandbox Code Playgroud)

我正在调用webmethod如下:

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        
    }
})
}
Run Code Online (Sandbox Code Playgroud)

仅当用户登录时才会出现此问题.现在,如果用户未登录,则即使在Azure上,所有功能也能正常工作.

事实上,当webmethods停止运行并且我刷新页面时,用户仍然登录并且webmethods再次开始运行但只有几分钟,然后再次发生相同的行为.

出了什么问题?

Glo*_*ria 0

该问题是由会话变量而不是身份验证引起的。事实上,Azure 中的 ASP.NET 应用程序不会使用默认的“inProc”方法维护会话状态。Azure 使用其他方法,其中一些方法成本高昂:表存储、SQL Azure 或 Windows Azure 缓存。