我使用 React 和 React Router Dom 托管了一个前端应用程序到 Azure。
我有一个带有令牌的重置密码链接,如下所示
这适用于本地主机。但是在 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>
错误消息是由于带有令牌的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 链接。