azure - 抑制关联 cookie 以强制客户端连接到另一个 Azure 应用程序节点

标签 azure azure-web-app-service sitefinity session-affinity

我有一个 C# Web 应用程序,该应用程序使用的组件 (Progress Telerik Sitefinity CMS) 需要很长时间(2 分钟)才能初始化。在此阶段访问该站点的用户将被重定向到每秒轮询状态的页面,直到初始化完成。 (这是内置的 Sitefinity 行为)。

我在 Azure 应用服务中托管我的应用程序。如果我增加实例数量(纵向扩展),我的一些用户最终会在新节点仍在初始化时使用它。问题是,由于 Azure 添加了关联 cookie,它们停留在此节点上。

我想要亲和性,除非网站正在初始化。在这种情况下,我想删除 cookie 并进行民意调查。在这种情况下,我会被分配一个随机节点,因此在几秒钟内就会找到一个初始化节点。

问题是:我如何实现这一目标?发生的大部分事情都是在 Sitefinity 中处理的,因此我求助于更改 global.asax 中的内容。这不起作用。我尝试将其放入我的 global.asax.cs 中:

protected void Application_PreRequestHandlerExecute(object sender, EventArgs e)
{
    var path = HttpContext.Current.Request.Url.AbsolutePath;
    // "/sitefinity/status" is the page the client is redirected to
    // "/appstatus" is used to poll initialization status
    if (path == "/appstatus" || path == "/sitefinity/status")
    {
        // "ARRAffinity" is the Azure affinity cookie
        Response.Cookies.Remove("ARRAffinity");
        // just removing the cookie didn't work so i tried to override it
        Response.Cookies.Add(new HttpCookie("ARRAffinity", "-") { HttpOnly = true });
        // reportedly, this suppresses cookie adding by Azure
        Response.Headers.Add("ARR-Disable-Session-Affinity", "true");
    };
}

如何强制我的客户端到不同的节点?

编辑 我想我发现了(部分)问题here .

  • 首先,需要“/”。这会返回 302 重定向,同时也会返回 ARRAffinity cookie。
  • 然后,请求“/sitefinity/status”。 ARR-Disable-Session-Affinity 和 cookie 均被删除。这意味着,客户端上的 cookie 并未被清除。
  • 轮询时,客户端已经拥有 cookie。因此用户永远不会被重定向到另一个节点。

所以这可能就是问题所在。现在解决这个问题...

编辑

我遵循了 Vesselin Vassilevs 的建议并将其添加到我的站点配置文件中:

<appSettings>
    <add key="sf:AppStatusPageResponseCode" value="503" />
</appSettings>

但是因为我仍然偶然到达初始化节点,所以我还通过更改我的 global.asax.cs 抑制了关联 cookie:

    protected void Application_EndRequest(object sender, EventArgs e)
    {
        var httpCode = Response.StatusCode;
        var isRedirectBackFromStatusPage = httpCode == 302 && Request.Url.AbsolutePath == "/sitefinity/status";
        var isFinalSitefinityStatusPoll = httpCode == 404 && Request.Url.AbsolutePath == "/appstatus";
        if (isRedirectBackFromStatusPage || isFinalSitefinityStatusPoll)
        {
            var cookie = Request.Cookies["ARRAffinity"];
            if (cookie != null) Response.Cookies.Add(cookie);
            return;
        }
        if (httpCode != 200 || !Response.ContentType.StartsWith("text/html"))
        {
            Response.Headers.Add("ARR-Disable-Session-Affinity", "true");
        };
    }

最佳答案

为什么不完全禁用 arr 关联 cookie? Sitefinity 后端在没有 arr cookie 和多个实例的情况下工作正常。

编辑:我们需要告诉 Azure 在 Sitefinity 初始化期间该站点尚未准备好。问题在于,appStatus 页面(在初始化期间由 Sitefinity 显示)返回状态代码 302 甚至 200,这使 Azure 认为该站点运行正常。我在这里写过:https://sitefinitydevelopment.com/blog/sitefinity's-application-status-page-can-cause-you-big-problems.html 根据您的 Sitefinity 版本,您可以在那里实现自定义解决方案(在系统重新启动期间手动返回 http 代码 503)或在 web.config 中设置以下设置 (Sitefinity 9+)

<add key="sf:AppStatusPageResponseCode" value="503" />

关于azure - 抑制关联 cookie 以强制客户端连接到另一个 Azure 应用程序节点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49693225/

相关文章:

c# - 如何知道使用 Sitefinity OwinContext 登录失败的原因?

azure - 通过 Bicep 为 StaticWebApp 创建 AppInsights 实例

azure - 将 Office 365 API 调用权限添加到 Azure 管理控制台

azure - 当部署为部署容器图像名称时如何激活 Web 应用程序中的设置

azure - API 的 JWT token 和 OAUTH 策略的 Azure 保管库 key

Sitefinity 脚本冲突

sql-server - 移动、创建、删除 Azure SQL 弹性池所需的最低访问级别(权限)是多少?

azure - 将文件上传到 CloudBlockBlob 时出现访问问题 - Windows Azure

node.js - 在Azure中部署Angular4通用应用程序无效的启动命令

javascript - Sitefinity 的 Disqus