首页 > 解决方案 > 从 web.config 中的查询字符串中去除 URL

问题描述

我正在使用 Google 通过 OAuth2 对用户进行身份验证。该站点使用 Angular 5 构建并托管在 Azure 应用服务 (IIS) 中。当 google 对用户进行身份验证时,它会发回一个包含一些 URI 的 URL:

https://domain.azurewebsites.net/membership/oauth2?code=<authentication code>&scope=openid%20email%20https://www.googleapis.com/auth/plus.me%20https://www.googleapis.com/auth/userinfo.email.

如您所见,在 之后&scope,有 2 个 URI。azure 网站返回一个错误,表明

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

如果我从后面剥离查询字符串&scope,它就可以正常工作。

我试图用重写规则去除 web.config 中的那部分:

    <rule name="Remove paging parameters" stopProcessing="true">
        <match url="^(.*)\/membership\/oauth2(.*)$" />
        <conditions trackAllCaptures="true">
            <add input="{QUERY_STRING}" pattern="^(.*)(scope=.+)(.*)$" />
        </conditions>
        <action type="Redirect" url="{C:1}{C:3}" appendQueryString="false" />
    </rule>

然而,什么也没有发生。任何帮助将不胜感激。

旁注:在 localhost 中一切正常,因为它是为 Angular 站点提供服务的 nodejs。该问题在 Azure 应用服务中引起,因为它由 IIS 提供服务。

标签: angularazureweb-configazure-web-app-service

解决方案


对于将来面临同样问题的任何人,我按照@kamranicus的回答解决了这个问题。该帖子中显示的正确重写网址如下:

<rule name="Google Login - Remove scope parameter" stopProcessing="true">
   <match url="google/redirect/url(.*)?$" />
   <conditions trackAllCaptures="true">
       <add input="{QUERY_STRING}" pattern="(.*)(&amp;?scope=.+&amp;?)(.*)" />
   </conditions>
   <action type="Rewrite" url="google/redirect/url?{C:1}{C:3}" appendQueryString="false" />
</rule>

推荐阅读