我有一个 Blazor .NET 8 服务器端应用程序在不安全的计算机上正常工作,并且正在尝试将其移动到没有直接出站互联网访问的计算机;仅通过网络代理。该应用程序使用 OpenIdConnect,用户应使用 Microsoft Entra ID 登录。
当用户尝试登录时,服务器控制台会记录以下错误(我已从 URL 中隐藏了我的租户 ID):
但是,如果我将完全相同的 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 参数。
是否可以:
我认为你可以这样做来使用代理:
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();
这个网站可能还有更多有用的信息。
我希望这有帮助!