由于网络设置,Blazor 无法访问 login.microsoftonline.com

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

我有一个 Blazor .NET 8 服务器端应用程序在不安全的计算机上正常工作,并且正在尝试将其移动到没有直接出站互联网访问的计算机;仅通过网络代理。该应用程序使用 OpenIdConnect,用户应使用 Microsoft Entra ID 登录。

当用户尝试登录时,服务器控制台会记录以下错误(我已从 URL 中隐藏了我的租户 ID):

  • 消息:IDX20803:无法从以下位置获取配置:“https://login.microsoftonline.com/xxxxxxxx-xxxx-xxxx-xxxx-xxxx/v2.0/.well-known/openid-configuration”。将在“21/03/2024 3:22:28 am +00:00”重试。异常:'System.IO.IOException:IDX20804:无法从以下位置检索文档:'[“System.String”类型的 PII 已隐藏。有关更多详细信息,请参阅 https://aka.ms/IdentityModel/PII。]'。
  • ---> System.Net.Http.HttpRequestException:没有这样的主机已知。 (登录.microsoftonline.com:443)
  • ---> System.Net.Sockets.SocketException (11001): 没有这样的主机已知。

但是,如果我将完全相同的 URL 粘贴到与托管 Blazor 应用程序相同的计算机上运行的 MS Edge,它将成功检索文档。浏览器使用“系统”>“代理设置”中的代理配置,但 Kestrel 似乎没有使用此功能。

Program.cs中的相关代码是:

builder.Services.AddAuthentication(OpenIdConnectDefaults.AuthenticationScheme)
    .AddMicrosoftIdentityWebApp(builder.Configuration.GetSection("AzureAd"));
builder.Services.AddControllersWithViews()
    .AddMicrosoftIdentityUI();

并且在

appsettings.json
下的
"AzureAD"
中,它具有
"Instance": "https://login.microsoftonline.com/",
,然后是其他 AzureAD 参数。

是否可以:

  • 告诉 Kestrel 使用系统代理设置来检索此配置文档,或者
  • 对应用程序源进行一些更改,以便根本不需要执行此步骤? (例如我可以将文档保存在本地并阅读吗)
blazor openid-connect kestrel-http-server microsoft-entra-id
1个回答
0
投票

我认为你可以这样做来使用代理:

public class ProxyHttpClientFactory : IMsalHttpClientFactory
{
    private static readonly HttpClient s_httpClient;

    static ProxyHttpClientFactory()
    {
        var webProxy = new WebProxy(
            new Uri("http://my.proxy"),
            BypassOnLocal: false);

        webProxy.Credentials = new NetworkCredential("user", "pass");

        var proxyHttpClientHandler = new HttpClientHandler
        {
            Proxy = webProxy,
            UseProxy = true,
        };

        s_httpClient = new HttpClient(proxyHttpClientHandler);
    }

    public HttpClient GetHttpClient()
    {
        return s_httpClient;
    }
}

// When you need to http requests you can do this : 
IMsalHttpClientFactory httpClientFactory = new ProxyHttpClientFactory();

var pca = PublicClientApplicationBuilder
    .Create("<App ID of the Native app>")
    .WithHttpClientFactory(httpClientFactory)
    .WithDefaultRedirectUri()
    .WithAuthority("https://login.microsoftonline.com/{<Tenant ID>}")
    .Build();

这个网站可能还有更多有用的信息。

我希望这有帮助!

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