我正在尝试使用 Azure AD B2C 运行ASP.NET Core 2.1 Web 应用程序。我在经过大量研究(以及更多的试验和错误)后运行了这个,因为文档中的说明有点偏离。
登录/注册过程使用项目设置向导期间的默认脚手架,加上新的Microsoft.AspNetCore.Authentication.AzureADB2C.UI Nuget,这在启动过程中产生了简化但“后备箱”的体验。
目前的问题是我无法使用不同于“signin-oidc”的自定义回复 URL 来完成这项工作。我已经读过“signin-oidc”以某种方式被嵌入到提供程序中,因此是默认设置。
我定义OnboardingController
了一个Start
操作,我希望用户在注册后登陆,所以我做了以下事情:
- A)我测试了 URL 是否
localhost:12345/Onboarding/Start
有效。页面显示正确。 - B)在 appsettings.json 我将 AzureAdB2C 更改
"CallbackPath": "/signin-oidc"
为"CallbackPath": "/Onboarding/Start"
C) 我去租户并将应用程序的回复 URL 更改为
localhost:12345/Onboarding/Start
. 重要附注:与 ADB2C 示例和指南不同,您必须在回复 URL 后附加 signon-oidc 或自定义请求路径!本地主机:12345 还不够!我可以确认身份验证有效: Microsoft.AspNetCore.Authentication.Cookies.CookieAuthenticationHandler:Information: AuthenticationScheme: AzureADB2CCookie 已登录。Microsoft.AspNetCore.Hosting.Internal.WebHost:Information: 请求在 56.201 毫秒内完成
然后,当我在浏览器中手动导航到 /Onboarding/Start 时,我得到
来自 RemoteAuthentication 的错误:关联失败。
Microsoft.AspNetCore.Hosting.Internal.WebHost:Information: Request starting HTTP/1.1 GET http://localhost:12345/Onboarding/start
Microsoft.AspNetCore.Authentication.OpenIdConnect.OpenIdConnectHandler:Warning: .AspNetCore.Correlation. state property not found.
Microsoft.AspNetCore.Authentication.OpenIdConnect.OpenIdConnectHandler:Information: Error from RemoteAuthentication: Correlation failed..
1. 但我从未被重定向到预期/开始页面。相反,我回到了根主页,就像我使用“signin-oidc”时发生的情况一样。为什么会这样,我如何让它停止去那里?
2. 我怎样才能让回复 URL 因您是登录还是 UP 不同而有所不同?我真的不能对一个使用注册策略,对另一个使用单独的登录策略,因为回复 URL 是相同的。
我是 .Net Core 的新手,我什至不知道如何调试它。使用 B2C 登录似乎是一个晦涩难懂的过程。任何见解将不胜感激。
编辑:这是自定义帐户控制器,它未内置在 AzureADB2C 的 Nuget 包中。虽然 Nuget 包提供了一个内部 AccountController,但它不允许您设置自定义回复 URL。然而,使用我自己的帐户控制器对我不起作用。我也没有收到“相关失败”错误,而是完全没有错误。
[Route( "[controller]/[action]" )]
public class AccountController : Controller
{
private readonly AzureADB2COptions azureAdB2COptions;
private const string PolicyAuthenticationProperty = "Policy";
private string scheme = AzureADB2CDefaults.AuthenticationScheme;
public AccountController( IOptions<AzureADB2COptions> b2cOptions )
{
azureAdB2COptions = b2cOptions.Value;
}
[HttpGet]
[Route( "/[controller]/SignIn" )]
public IActionResult SignIn()
{
var callbackUrl = Url.Action( nameof( OnboardingController.Start ), "Onboarding", values: null, protocol: Request.Scheme );
var properties = new AuthenticationProperties { RedirectUri = callbackUrl };
properties.Items[PolicyAuthenticationProperty] = azureAdB2COptions.SignUpSignInPolicyId;
return this.Challenge( properties, scheme );
}
B2C 的 startup.cs 代码与为我生成的默认 .NETCore 2.1 模板相同:
services.AddAuthentication( AzureADB2CDefaults.AuthenticationScheme )
.AddAzureADB2C( options =>
{
Configuration.Bind( "AzureAdB2C", options );
} );