我試圖找出一種方法讓 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();
}
uj5u.com熱心網友回復:
你所擁有的根本不起作用。OpenIDConnect 僅處理身份驗證部分,但接收到的令牌只會在您的設定中丟失。您遇到的第二個問題是您沒有將 JwtBearer 與 AddOpenIdConnect 混合使用。JwtBearer 用于從客戶端接收訪問令牌的 API。
您需要配置的適當骨架是:
services.AddAuthentication(options =>
{
options.DefaultScheme = CookieAuthenticationDefaults.AuthenticationScheme;
options.DefaultChallengeScheme = OpenIdConnectDefaults.AuthenticationScheme;
}).AddCookie(opt =>
{
}).AddOpenIdConnect(options =>
{
});
您應該將 OpenIDConnectHandler 與 Cookie 處理程式結合使用。然后它會起作用。
有關更多詳細資訊,請參閱此文章: 如何使用 OpenID Connect 實施 Blazor 身份驗證?
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/364317.html
標籤:验证 asp.net-core-mvc openid 连接 asp.net-core-5.0
