首页 > 解决方案 > 从企业代理后面使用 .Net Core 3.00 对 Azure AD 进行身份验证

问题描述

我有一个 .Net 核心应用程序。我尝试使用 AzureAd 身份验证。它在 LocalHost 上运行良好,但是当我在公司服务器上部署应用程序时,我遇到了代理凭据问题。我使用了以下代码但无法正常工作:

services.AddAuthentication(AzureADDefaults.AuthenticationScheme)
                .AddAzureAD(options => Configuration.Bind("AzureAd", options))
                .AddOpenIdConnect(options => options.BackchannelHttpHandler = new HttpClientHandler
                {
                    UseProxy = true,
                    Proxy = new WebProxy
                    {
                        Credentials = new NetworkCredential
                        {
                            UserName = "my_UserName",
                            Password = "my_Password "
                        },
                        Address = new Uri("my_Domain:my_Port")
                    }
                }); 

应用设置:

"AzureAd": {
   "Instance": "https://login.microsoftonline.com/",
   "Domain": "My_Domain",
   "TenantId": "*************",
   "ClientId": "******",
   "CallbackPath": "/signin-oidc"
 } 

标签: c#.net-coreproxyazure-ad-b2c

解决方案


经过深入搜索,我找到了解决方案:

services.AddAuthentication(AzureADDefaults.AuthenticationScheme)
        .AddAzureAD(options => Configuration.Bind("AzureAd", options))
        .AddCookie(options =>
             {
               options.SlidingExpiration = true;
               options.ExpireTimeSpan = TimeSpan.FromMinutes(60);
              });

services.Configure<OpenIdConnectOptions>(AzureADDefaults.OpenIdScheme, options =>
            {
                options.BackchannelHttpHandler = new HttpClientHandler
                {
                    UseProxy = true,
                    Proxy = new WebProxy()
                    {
                        Credentials = new NetworkCredential
                        {
                            UserName = "My_UserName",
                            Password = "My_Password"
                        },
                        Address = new Uri("My_Domain:My_Port")
                    }
                };
            });

            services.AddHttpClient();


推荐阅读