C# 未找到使用dotnet Core Web MVC应用进行Azure AD身份验证的质询和身份验证方案
我正在创建一个ASP.NET Core 3.1 MVC web应用程序,并尝试设置Azure AD身份验证(在我的Mac上使用Visual Studio for Mac)。我相信我已经做了一切必要的工作,在C# 未找到使用dotnet Core Web MVC应用进行Azure AD身份验证的质询和身份验证方案,c#,authentication,asp.net-core-mvc,azure-active-directory,C#,Authentication,Asp.net Core Mvc,Azure Active Directory,我正在创建一个ASP.NET Core 3.1 MVC web应用程序,并尝试设置Azure AD身份验证(在我的Mac上使用Visual Studio for Mac)。我相信我已经做了一切必要的工作,在Startup.cs中设置了广告认证: services.AddAuthentication(o => { o.DefaultScheme = CookieAuthenticationDefaults.AuthenticationS
Startup.cs
中设置了广告认证:
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(操作配置选项)设置默认方案
我的代码同时显示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();
确保在授权中间件启动之前启动身份验证中间件。您可以将代码修改为:
services.AddAuthentication(AzureADDefaults.AuthenticationScheme)
.AddAzureAD(options => Configuration.Bind("AzureAd", options));
AzureADDefaults.AuthenticationScheme
在未请求特定方案时默认使用
还要修改中间件顺序:
app.UseAuthentication();
app.UseAuthorization();
要确保身份验证中间件在授权中间件之前启动。在使用时,请在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 ad应用程序中,使用/signin oidc
设置重定向url
在使用时,请在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 ad应用程序中,使用/signin oidc
设置重定向url