【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

 

posted @   路边两盏灯  阅读(14)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?
历史上的今天:
2023-06-14 【Azure 应用服务】Azure Function App在部署时候遇见 503 ServiceUnavailable
点击右上角即可分享
微信分享提示