.NET Core Web 应用程序中的 SSO,使用 OIDC/OAuth 对 MS Entra 用户进行身份验证

问题描述 投票:0回答:1

我需要在 .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();

有什么我错过的吗? 我应该有授权句柄吗?

谢谢你

c# .net-core oauth-2.0 single-sign-on entra
1个回答
0
投票

您可以通过选择身份验证类型:“Microsoft 身份平台”来使用该模板。它使用

Microsoft.Identity.Web
enter image description here

此应用程序设置在此包中使用“Instance”“Domian”“TenantId”而不是“Authority”。

默认的 Oauth2 流程是“使用 PKCE(代码交换证明密钥)的授权代码流程”,不需要客户端保密。

© www.soinside.com 2019 - 2024. All rights reserved.