我正在尝试找出一种让mvc对AzureAD oidc令牌进行身份验证的方法。我的应用程序只是后端,没有登录或注销。所以我想从OnAuthorizationAsync(**AuthorizationFilterContext** context)获取用户声明,但它总是在httpcontext中返回空。我认为这可能是AddOpenIdConnect中的某种配置问题。以下是我在ConfigureServices中的设置。需要做什么才能获得更多的用户声明?
services.AddAuthentication(OpenIdConnectDefaults.AuthenticationScheme
// options =>
//{
// options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
// options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme; //AuthorizationConstants.AuthenticationSchemes.Oidc
//} //behave the same with or without this setting
)
.AddOpenIdConnect("oidc", options =>
{
options.ClientId = azureAdConfig.ClientId;
options.ConfigurationManager = new ConfigurationManager<OpenIdConnectConfiguration>(azureAdConfig.EntryUrl, new OpenIdConnectConfigurationRetriever());
});
Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
// routing and other things
app.UseAuthentication();
app.UseAuthorization();
}发布于 2021-11-19 08:25:13
你所拥有的根本不起作用。OpenIDConnect只处理身份验证部分,但是收到的令牌在您的设置中丢失了。你遇到的第二个问题是你没有把JwtBearer和AddOpenIdConnect混为一谈。JwtBearer用于从客户端接收访问令牌的接口。
您需要配置的适当框架是:
services.AddAuthentication(options =>
{
options.DefaultScheme = CookieAuthenticationDefaults.AuthenticationScheme;
options.DefaultChallengeScheme = OpenIdConnectDefaults.AuthenticationScheme;
}).AddCookie(opt =>
{
}).AddOpenIdConnect(options =>
{
});您应该将OpenIDConnectHandler与Cookie处理程序结合使用。那么它就会起作用。
有关更多详细信息,请参阅本文:How do I implement Blazor authentication with OpenID Connect?
https://stackoverflow.com/questions/70028026
复制相似问题