Azure App Service - 访问PWA webmanifest.json时出现CORS OAUTH2错误

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

我想在我的App Service中添加PWA功能。它是用C#ASP.NET编写的。但是,Chrome无法访问清单,Edge甚至无法获取favicon(这可能是一个单独的问题)。其他桌面浏览器显然还没有PWA功能。

Chrome是唯一请求清单的浏览器。这是我的控制台中的错误消息:

Failed to load https://login.windows.net/b79fd714-9c54-449d-b377-3b59deb2d3b6/oauth2/authorize?response_type=code+id_token&redirect_uri=https%3A%%{myappname}.azurewebsites.net%.auth%2Flogin%2Faad%2Fcallback&client_id=67bedd5c-5478-44ec-ba13-85061c71114b&scope=openid+profile+email&response_mode=form_post&nonce=df52b6dc64dd4c3393957675f365ae93_20180903064438&state=redir%3D%252Fwebmanifest.json: Redirect from 'https://login.windows.net/b79fd714-9c54-449d-b377-3b59deb2d3b6/oauth2/authorize?response_type=code+id_token&redirect_uri=https%3A%%{myappname}.azurewebsites.net%.auth%2Flogin%2Faad%2Fcallback&client_id=67bedd5c-5478-44ec-ba13-85061c71114b&scope=openid+profile+email&response_mode=form_post&nonce=df52b6dc64dd4c3393957675f365ae93_20180903064438&state=redir%3D%252Fwebmanifest.json' to 'https://login.microsoftonline.com/b79fd714-9c54-449d-b377-3b59deb2d3b6/oauth2/authorize?response_type=code+id_token&redirect_uri=https%3A%%{myappname}.azurewebsites.net%.auth%2Flogin%2Faad%2Fcallback&client_id=67bedd5c-5478-44ec-ba13-85061c71114b&scope=openid+profile+email&response_mode=form_post&nonce=df52b6dc64dd4c3393957675f365ae93_20180903064438&state=redir%3D%252Fwebmanifest.json' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'https://{myappname}.azurewebsites.net' is therefore not allowed access.

看来我在App Service中启用的AAD身份验证干扰了我的浏览器的清单请求。

我知道要快速解决这个问题,Microsoft必须将此应用程序添加到他们的CORS中。这当然不是我期望发生的事情。但是,我需要一些替代方法让浏览器访问清单。

我已将必要的mimeMaps添加到我的web.config中。

<staticContent>
  <remove fileExtension=".json" />
  <mimeMap fileExtension=".json" mimeType="application/json" />
  <remove fileExtension=".webmanifest" />
  <mimeMap fileExtension=".webmanifest" mimeType="application/json" />
</staticContent>

这是我的Head的顶部:

<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="content-type" content="text/html; charset=utf />
    <meta name="viewport" content="width=device-width, initial-scale=>

    <link rel="apple-touch-icon" sizes="180x180" href="/apple-touch-icon.png">
    <link rel="icon" type="image/png" sizes="32x32" href="/favicon-32x32.png">
    <link rel="icon" type="image/png" sizes="16x16" href="/favicon-16x16.png">
    <link rel="manifest" href="/webmanifest.json">
    <link rel="mask-icon" href="/safari-pinned-tab.svg" color="#5bbad5">
    <meta name="apple-mobile-web-app-title" content="{myappname}">
    <meta name="application-name" content="{myappname}">
    <meta name="msapplication-TileColor" content="#da532c">
    <meta name="theme-color" content="#ffffff">


    <title>@ViewBag.Title - {myappname}</title>

如果我使用某种AJAX请求,我会在网上关注其中一个tutorials。但是,据我所知,我几乎无法控制标题。

注意:我的应用程序名称的所有出现都已替换为{myappname}。我真的希望那些令牌是暂时的......


在上面的教程链接中,有一个相关的文本块比我的问题本身更能解释我的问题:

在用户向Azure AD进行身份验证以登录应用程序之后的典型方案中,Azure App Service会为客户端浏览器的经过身份验证的会话设置名为“AppServiceAuthSession”的cookie。 Web应用程序可以对应用程序的各种功能使用XMLHttpRequest / AJAX请求,发送到Azure App Service的请求也将包含AppServiceAuthSession cookie。如果请求中不存在此cookie,Azure App Service会将请求重定向到Azure AD以进行登录。此重定向导致AJAX请求成为CORS请求,因为目标域更改,默认情况下Azure AD不允许跨源请求。

在我的情况下,浏览器正在发送不包含必要标头的清单请求。

html azure cors progressive-web-apps azure-web-app-service
1个回答
6
投票

我只是在这里展开它,但您可以尝试在清单链接上使用crossOrigin属性,如下所示:

 <link rel="manifest" href="/webmanifest.json" crossOrigin="use-credentials">

以下是一些可能有用的相关链接。根据第一页,在请求manifest.json时,默认情况下不会传递用户凭据:

如果有帮助请告诉我。

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