Azure AD B2C .NET Core 直接重定向而不显示登录屏幕

Azure AD B2C .NET Core redirects directly without showing login screen

我正在尝试创建一个简单的 .NET Core 3.1 MVC 应用程序,它需要使用 B2C 通过 Azure Active Directory 进行身份验证。

我已经阅读了多个文档,但仍然无法正常工作。我能够 运行 用户流程成功(在 https://jwt.ms/ 中收到信息)。但是,如果我 运行ning 我的应用程序,则会发生以下情况:

我拿了一个示例项目(https://fabrikamb2c.b2clogin.com)来查看问题是在我的代码中,还是在 Azure 设置中。当我使用示例设置执行此操作时,我正确地看到了登录屏幕。当我切换到自己的设置时,出现了上面的情况。所以我猜这与 Azure 中的设置有关。正确的流程(示例设置也是如此)

我确实看到了这个 URL 中的一些差异(例如围绕范围),但无法弄清楚是什么导致了我的问题。目前正在考虑权限?

Azure 设置

内部 Azure 订阅 2

内部 B2C 订阅(链接到订阅 2)

我希望任何人都可以在这里给我一些启发...

更新: 我试图通过使用 Fiddler 获得更多光线。我可以看到:

回复包含:

{
  "issuer": "https://{myb2cdomain}.b2clogin.com/{b2c-id}/v2.0/",
  "authorization_endpoint": "https://{myb2cdomain}.b2clogin.com/{myb2cdomain}.onmicrosoft.com/b2c_1_susi/oauth2/v2.0/authorize",
  "token_endpoint": "https://{myb2cdomain}.b2clogin.com/{myb2cdomain}.onmicrosoft.com/b2c_1_susi/oauth2/v2.0/token",
  "end_session_endpoint": "https://{myb2cdomain}.b2clogin.com/{myb2cdomain}.onmicrosoft.com/b2c_1_susi/oauth2/v2.0/logout",
  "jwks_uri": "https://{myb2cdomain}.b2clogin.com/{myb2cdomain}.onmicrosoft.com/b2c_1_susi/discovery/v2.0/keys",
  "response_modes_supported": [
    "query",
    "fragment",
    "form_post"
  ],
  "response_types_supported": [
    "code",
    "code id_token",
    "code token",
    "code id_token token",
    "id_token",
    "id_token token",
    "token",
    "token id_token"
  ],
  "scopes_supported": [
    "openid"
  ],
  "subject_types_supported": [
    "pairwise"
  ],
  "id_token_signing_alg_values_supported": [
    "RS256"
  ],
  "token_endpoint_auth_methods_supported": [
    "client_secret_post",
    "client_secret_basic"
  ],
  "claims_supported": [
    "idp",
    "sub",
    "tfp",
    "iss",
    "iat",
    "exp",
    "aud",
    "acr",
    "nonce",
    "auth_time"
  ]
}

获取 https://{myb2cdomain}.b2clogin.com/{myb2cdomain}.onmicrosoft.com/b2c_1_susi/discovery/v2.0/keys

包含

{
  "keys": [
    {"kid":"{id}","nbf":1111111111,"use":"sig","kty":"RSA","e":"AQAB","n":"{long-id}"}
  ]
}

得到 https://{myb2cdomain}.b2clogin.com/{myb2cdomain}.onmicrosoft.com/b2c_1_susi/oauth2/v2.0/authorize?client_id=xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx&redirect_uri =https%3A%2F%2Flocalhost%3A5000%2Fsignin-oidc&response_type=code%20id_token&scope=openid%20profile%20offline_access%20&response_mode=form_post&nonce={long.string}&client_info=1&state={long.string2}&x-client-SKU=ID_NETSTANDARD2_0&x-client-ver=5.5.0.0

文本中的回复不可读...尽管当我将回复设置为 XML 时,我确实在此处看到了一些 HTML:

<html />
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Logging in...</title>
<meta name="CACHE-CONTROL" content="NO-CACHE" />
<meta name="PRAGMA" content="NO-CACHE" />
<meta name="EXPIRES" content="-1" />
</head>
<body>
<form id="auto" method="post" action="https://jwt.ms">
<div>
<input type="hidden" name="error" id="error" value="redirect_uri_mismatch" />
<input type="hidden" name="error_description" id="error_description" value="AADB2C90006: The redirect URI 'https://localhost:44316/signin-oidc' provided in the request is not registered for the client id 
...

包含... 请求中提供的重定向 URI 'https://localhost:44316/signin-oidc' 未注册客户端 ID。

我想我需要检查重定向 URI 的...

好吧...似乎无法在本地使用重定向 URL https://jwt.ms as redirect URL (or I simply do not know how. See also https://github.com/aspnet/Security/issues/1757).

jwt.ms URL 非常适合测试用户流程,但不适用于生产,因为应用程序设置值 CallbackPath 需要相对路径。

所以...我在我的 B2C 应用程序注册中将重定向 URI 添加到 https://localhost:44316/signin-oidc,然后... tada!有效。

现在我将 CallBackPath 添加到我的 appsettings.json,只是为了记录它。

"CallbackPath": "/signin-oidc" // Default value: /signin-oidc. If change, please edit or add the link into the B2C App Registration

我让我的代码稍后再使用它

options.CallbackPath = AzureAdB2COptions.CallbackPath;

如果你把这两行都去掉,它也能工作。只要您在应用程序注册重定向 URI 中指定 /signin-oidc。

遗憾的是,发生的错误并没有轻易地显示给最终用户...