Azure 使用带有令牌的长 URL 给出错误

问题描述 投票:0回答:1

我使用 React 和 React Router Dom 托管了一个前端应用程序到 Azure。

我有一个带有令牌的重置密码链接,如下所示

https://abc.azurewebsites.net/reset/CfDJ8AkOTLe70aJDl6Jq93G40f4OoMX35xv1bg73%2fU9WyIDfJae9LcF8iFFF6oiO7atp8l9O%2fr1lBq%2bMPZd2aPkELXWPB7YQ5lYoAkh3t2QdB5a%2fB%2bRvJordH34lEbPRBV%2by842E5z%2b1ZNSBstZljGOPZ6tOQNjLGH%2byAAUURGK2Z8rSFjQa22t0RXaVTzP59yCEagf42DGK9UM1PLqtSun655EVkoRH8Jg2LtgK2YZYv6zD

这适用于本地主机。但是在 Azure 中它给出了这个错误

“您要查找的资源已被删除、更名或暂时不可用。”

我已经添加了一些帖子中建议的 web.config。它适用于较短的 URL,但不确定如何使其适用于带有标记的较大 URL。

<?xml version="1.0"?>
<configuration>
  <system.web>
    <customErrors mode="Off"/>
    <httpRuntime maxQueryStringLength = "10000" />
  </system.web>
  <system.webServer>
    <rewrite>
      <rules>
        <rule name="React Routes" stopProcessing="true">
          <match url=".*" />
          <conditions logicalGrouping="MatchAll">
            <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
            <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
            <add input="{REQUEST_URI}" pattern="^/(api)" negate="true" />
          </conditions>
          <action type="Rewrite" url="/" />
        </rule>
      </rules>
    </rewrite>
    <security>
      <requestFiltering>
        <requestLimits maxQueryString="10000" />
      </requestFiltering>
    </security>
  </system.webServer>
</configuration>
reactjs azure web react-router web-config
1个回答
0
投票

错误消息是由于带有令牌的URL的长度。

Azure 有一个

default limit of 2048 characters
的 URL。您可以通过将以下配置添加到您的
web.config
文件来增加限制。

<?xml version="1.0"?>
<configuration>
    <system.web>
        <customErrors mode="Off"/>
        <httpRuntime maxQueryStringLength = "32768" />
    </system.web>
    <system.webServer>
        <rewrite>
            <rules>
                <rule name="React Routes" stopProcessing="true">
                    <match url=".*" />
                    <conditions logicalGrouping="MatchAll">
                        <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
                        <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
                        <add input="{REQUEST_URI}" pattern="^/(api)" negate="true" />
                    </conditions>
                    <action type="Rewrite" url="/" />
                </rule>
            </rules>
        </rewrite>
        <security>
            <requestFiltering>
                <requestLimits maxQueryString="32768" />
            </requestFiltering>
        </security>
    </system.webServer>
</configuration>

根据 Azure Web App URL 长度限制。 查询字符串的最大长度增加到 32768 个字符.

并且您需要调整

maxQueryStringLength
元素中的
httpRuntime
值以匹配您在
requestLimits
元素中设置的新限制。

方法 2

const myToken = "long token"; 
const encoded_Token = encodeURIComponent(myToken); 
const reset_PasswordUrl = `https://somesample.com/reset-password/${encoded_Token}`;

确保 URL 格式正确并且可以被服务器正确解析是一个很好的做法。

如果token太长,可能会超过Azure允许的最大URL长度。在这种情况下,您需要考虑使用 POST 请求而不是 GET 请求来传递令牌。

或者,您可以尝试使用标记化服务或生成更短的标记来缩短标记。

Another option is

storing the token in a database
and passing a
unique identifier in the URL
instead of the entire token.

有关更多信息,请参阅 SO 链接。

© www.soinside.com 2019 - 2024. All rights reserved.