在使用IdentityServer4的Blazor WebAssembly应用程序中,出现了“阻止加载混合活动内容”的问题。

7

我正在尝试在我的Web服务器上运行默认的Blazor WebAssembly项目模板。本地运行时,该项目没有任何问题。但是当我将其部署到服务器后就出现了问题。

我可以成功地进入不需要身份验证的任何页面。但是,当我尝试进入需要登录的页面时,我会看到如下消息:

尝试登录时出错:'网络错误'

enter image description here

在Web浏览器控制台中,我可以看到以下内容:

阻止加载混合活动内容“http://[subdomain.domain.com]/.well-known/openid-configuration

在Firefox的“网络”选项卡中,请求被标记为“Blocked”(已阻止)。

我的Web服务器运行在Nginx上,充当反向代理。我计划在Internet和Nginx之间保持HTTPS加密配置。Nginx与其他服务之间的通信应该是通过普通HTTP进行的。这是我的Nginx配置:

server {
        listen 80;

        location / {
            return 301 https://$host$request_uri;
        }
    }

[...]
server {
        listen 443 ssl http2;
        listen [::]:443 ssl http2;
        server_name subdomain.domain.com;

        ssl_certificate /etc/nginx_ssl/live/fullchain.pem;
        ssl_certificate_key /etc/nginx_ssl/live/privkey.pem;
        ssl_session_cache builtin:1000 shared:SSL:10m;
        ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
        ssl_ciphers HIGH:!aNULL:!eNULL:!EXPORT:!CAMELLIA:!DES:!MD5:!PSK:!RC4;
        ssl_prefer_server_ciphers on;

        location / {
            proxy_pass http://blazorapp:80;
            proxy_redirect off;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header X-Forwarded-Proto $scheme;
            proxy_set_header X-Forwarded-Host $server_name;

            proxy_http_version 1.1;
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection $connection_upgrade;
            proxy_set_header Host $host;
            proxy_cache_bypass $http_upgrade;
        }
    }

我的猜测

从浏览器错误信息中可以看出,浏览器尝试通过HTTP而不是HTTPS访问.well-known/openid-configuration。问题可能就在这里。

你有任何想法出错的原因是什么吗?

2个回答

3

我遇到了同样的问题,也想知道正确的答案,因为我目前找到的似乎只是一种权宜之计:

添加

<meta http-equiv="Content-Security-Policy" content="upgrade-insecure-requests">

将以下内容添加到Client/wwwroot/index.htmlServer/Pages/Shared/_Layout.shtml<head>部分。虽然应用程序通过这种方式显示"正在授权..."和"检查登录状态..."的时间比通过http访问时长,但至少它可以工作。

更新

之前的解决方案确实是一个妥协办法。我认为我找到了更好的方法。

原始问题发生的原因是openid-configuration包含使用不同模式(http而不是https)的URL。我们可以通过在Startup.cs中注册来更改IdentityServer使用的基本URL:

services.AddIdentityServer(
        options => {
            options.PublicOrigin = Configuration.GetValue<string>("PublicOrigin");
        })

当然,在appsettings.json中我们也必须提供正确的URL:

{
  //snip
  "PublicOrigin": "https://subdomain.domain.com",
  //snip
}

现在对我来说它完全正常工作。


0
将以下行添加到startup.cs的Configure方法中:
app.Use((ctx, next) => { 
    ctx.SetIdentityServerOrigin("https://IdsrvDomain.com"); 
    return next(); 
});

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接