0

我一直在使用 Microsoft.Identity 平台并试图让它与 aspnetcore 6.0 中的基本模板一起使用,但我陷入了无限登录循环。

我正在使用从 web 应用程序和 web api 模板创建的项目(使用这些命令"dotnet new webapi --force --auth IndividualB2C""dotnet new webapp --force --auth IndividualB2C"),然后更新两者中的 appsettings 以连接到我的 Azure B2C 租户。

网页应用

"AzureAdB2C": {
        "CallbackPath": "/signin-oidc",
        "ClientId": "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx",
        "ClientSecret": "xxxxx",
        "Domain": "xxxx.onmicrosoft.com",
        "EditProfilePolicyId": "B2C_1_EditProfile",
        "Instance": "https://xxxx.b2clogin.com/",
        "SignedOutCallbackPath": "/signout/B2C_1_susi",
        "SignUpSignInPolicyId": "B2C_1_SUSI"
    },
    "DownstreamApi": {
        "BaseUrl": "https://localhost:7208/",
        "Scopes": "https://xxxx.onmicrosoft.com/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/user.impersonation"
    }

网页接口

"AzureAdB2C": {
    "ClientId": "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx",
    "Domain": "xxxx.onmicrosoft.com",
    "Instance": "https://xxxx.b2clogin.com/",
    "Scopes": "https://xxxx.onmicrosoft.com/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/user.impersonation"
    "SignUpSignInPolicyId": "B2C_1_SUSI"
}

进行这些更改后,我可以运行应用程序,Web 应用程序将尝试通过我的社交帐户登录,然后进入登录服务器和应用程序重定向 URL 之间的无限循环重定向。

我确实收到一条引发 MicrosoftIdentityWebChallengeUserException 的日志消息(大概是因为我需要同意范围)。我的理解是[AuthorizeForScopes(ScopeKeySection = "DownstreamApi:Scopes")]Razor 页面上的属性应该为我处理这个同意工作流,但它似乎没有这样做。

在配置这些示例应用程序或捕获 WebChallenge 异常的方法时,我需要做些什么额外的事情(如果是,我如何将质询发回给用户)?

我觉得我在这里遗漏了一些明显的东西来使它起作用,但我似乎找不到它。任何人都可以提供一些指导以使其正常工作吗?

4

1 回答 1

0

请检查这是否可以缩小问题范围:

  1. 请清除 cookie 并重试。

  2. AuthorizeForScopes 属性需要该方法中的确切范围。任何不正确的范围都将导致 MsalUiRequiredException 。尝试将参数硬编码到所需的任何范围:[AuthorizeForScopes(Scopes = new[] { "User.impersonation"})]

  3. 尝试IExceptionFilter 在 Startup.cs 中使用并添加过滤器,如下所示:

services.AddMvc(options =>
    {
        var policy = new AuthorizationPolicyBuilder()
            .RequireAuthenticatedUser()
            .Build();
        options.Filters.Add(new AuthorizeFilter(policy));
        options.Filters.Add(new AuthorizeForScopesAttribute(new string[] { Scopes:[] }));
    })

请查看此SO 参考

参考:

活动目录aspnetcore 问题| github

于 2021-12-09T14:34:25.073 回答