[英]Challenge and Authentication Schemes not found for Azure AD Authentication with dotnet Core Web MVC app
我正在創建一個 ASP.NET Core 3.1 MVC Web 應用程序並嘗試設置 Azure AD 身份驗證(在我的 Mac 上使用 Visual Studio for Mac)。 我相信我做了一切必要的事情來在Startup.cs
設置 AD 身份驗證:
services.AddAuthentication(o =>
{
o.DefaultScheme = CookieAuthenticationDefaults.AuthenticationScheme;
o.DefaultChallengeScheme = OpenIdConnectDefaults.AuthenticationScheme;
o.DefaultAuthenticateScheme = OpenIdConnectDefaults.AuthenticationScheme;
})
.AddAzureAD(options => Configuration.Bind("AzureAd", options))
.AddCookie();
我確保我使用中間件:
app.UseAuthorization();
app.UseAuthentication();
當應用程序在 chrome 瀏覽器中彈出時,我收到以下錯誤:
處理請求時發生未處理的異常。
InvalidOperationException: 未指定 authenticationScheme,也未找到 DefaultChallengeScheme。 可以使用 AddAuthentication(string defaultScheme) 或 AddAuthentication(Action configureOptions) 設置默認方案。
我的代碼顯示了 authenticationScheme 和 DefaultChallengeScheme 設置,不確定為什么找不到這兩個方案。 有誰知道?
Nuget版本:
Microsoft.AspNetCore.Authentication (2.2.0)
Microsoft.AspNetCore.Authentication.AzureAD.UI (3.1.2)
Microsoft.AspNetCore.Authentication.OpenIdConnect (3.1.2)
您可以將代碼修改為:
services.AddAuthentication(AzureADDefaults.AuthenticationScheme)
.AddAzureAD(options => Configuration.Bind("AzureAd", options));
當未請求特定方案時,將默認使用AzureADDefaults.AuthenticationScheme
。
還要修改中間件順序:
app.UseAuthentication();
app.UseAuthorization();
確保身份驗證中間件在授權中間件之前觸發。
當您使用OpenID Connect (OIDC) 身份驗證時,請調用 ConfigureServices 方法中的 AddOpenIdConnect 方法:
services.AddAuthentication(options =>
{
options.DefaultScheme = CookieAuthenticationDefaults.AuthenticationScheme;
options.DefaultChallengeScheme = OpenIdConnectDefaults.AuthenticationScheme;
options.DefaultAuthenticateScheme = OpenIdConnectDefaults.AuthenticationScheme;
})
.AddCookie()
.AddOpenIdConnect(options =>
{
options.Authority = "https://login.microsoftonline.com/your_tenantId";
options.ClientId = "your_clientId";
});
和 appsettings.json:
"AzureAd": {
"Domain": "xxx.onmicrosoft.com",
"Instance": "https://login.microsoftonline.com/",
"ClientId": "xxxxxxxxxxxxxxxx",
"TenantId": "xxxxxxxxxxxxxxxx",
"CallbackPath": "/signin-oidc"
}
在注冊的 Azure 廣告應用程序中,使用/signin-oidc
設置重定向 URL。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.