我正在创建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(字符串defaultScheme)或AddAuthentication(操作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)
答案 0 :(得分:1)
您可以将代码修改为:
'video' => $data['image']['file_name']
services.AddAuthentication(AzureADDefaults.AuthenticationScheme)
.AddAzureAD(options => Configuration.Bind("AzureAd", options));
在不要求特定方案时默认使用。
还修改中间件顺序:
AzureADDefaults.AuthenticationScheme
要确保身份验证中间件在授权中间件之前启动。
答案 1 :(得分:1)
在使用OpenID Connect (OIDC) authentication时,请在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。