我需要在 .NET Core Web 应用程序中快速实现 SSO,使用 OIDC/OAuth 对 MS Entra 用户进行身份验证。
这是我以前没有做过的事情,所以会询问任何做过的人我是否走对了路,因为我看过几个教程,每个教程的方法都略有不同。
我的想法是添加以下或类似内容到appsettings.json:
{
"AzureAd": {
"Authority": "url-here",
"ClientId": "Enter_the_Application_Id_Here",
"ClientCredentials": [
{
"SourceType": "ClientSecret",
"ClientSecret": "Enter_the_Client_Secret_Here"
}
],
"CallbackPath": "/signin-oidc",
"SignedOutCallbackPath": "/signout-callback-oidc"
},
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
},
"AllowedHosts": "*"
}
安装 nuget 包 Microsoft.Identity.Web、Microsoft.Identity.Web.UI
将以下内容添加到Program.cs:
JwtSecurityTokenHandler.DefaultMapInboundClaims = false;
builder.Services
.AddAuthentication(OpenIdConnectDefaults.AuthenticationScheme)
.AddMicrosoftIdentityWebApp(builder.Configuration);
builder.Services.AddControllersWithViews(options =>
{
var policy = new AuthorizationPolicyBuilder()
.RequireAuthenticatedUser()
.Build();
options.Filters.Add(new AuthorizeFilter(policy));
}).AddMicrosoftIdentityUI();
有什么我错过的吗? 我应该有授权句柄吗?
谢谢你