【Azure Developer】記錄一段驗證AAD JWT Token時需要設置代理獲取openid-configuration內容
問題描述
如果在使用.NET代碼對AAD JWT Token進行驗證時候,如果遇見無法訪問 Unable to obtain configuration from: 'https://login.partner.microsoftonline.cn/<common or your tenant id>/v2.0/.well-known/openid-configuration‘, 可以配置 HttpClientHandler.Proxy 代理。
問題解答
...
services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme) .AddJwtBearer(options => { options.Authority = https://login.partner.microsoftonline.cn/<common or tenant id>; options.TokenValidationParameters = new Microsoft.IdentityModel.Tokens.TokenValidationParameters { ValidateIssuerSigningKey = false, ValidateAudience = true, ValidateIssuer = true, ValidateLifetime = true, ValidAudience = "Entra ID Application ID", ValidIssuer = https://login.partner.microsoftonline.cn/<common or tenant id>/v2.0, }; options.BackchannelHttpHandler = new HttpClientHandler { UseProxy = true, Proxy = Utility.GetWebProxy(httpConfiguration) };
options.Events ??= new JwtBearerEvents(); var onTokenValidatedHandler = options.Events.OnTokenValidated; options.Events.OnTokenValidated = async context => { var httpContext = context.HttpContext; lock (httpContext) { httpContext.Items[ServiceConstants.HttpContextTokenKey] = (context.SecurityToken is JwtSecurityToken or JsonWebToken ? context.SecurityToken : null); } await onTokenValidatedHandler(context).ConfigureAwait(false); }; }); ...
參考資料
HttpClientHandler.Proxy 屬性:https://learn.microsoft.com/zh-cn/dotnet/api/system.net.http.httpclienthandler.proxy?view=net-8.0#system-net-http-httpclienthandler-proxy
當在復雜的環境中面臨問題,格物之道需:濁而靜之徐清,安以動之徐生。 云中,恰是如此!

浙公網安備 33010602011771號