我正在尝试使用Azure AD B2C运行ASP.NET Core 2.1 Web应用程序。我经过大量的研究(以及更多的试验和错误)后运行,因为文档中的说明有点偏差。
登录/注册过程使用项目设置向导期间的默认脚手架,以及新的Microsoft.AspNetCore.Authentication.AzureADB2C.UI Nuget,这会在启动期间产生简化但“后盒装”的体验。
目前的问题是我无法使用与“signin-oidc”不同的自定义回复URL进行此操作。我已经读过“signin-oidc”以某种方式被提供给提供者,因此是默认的。
我有一个OnboardingController
,其中定义了Start
动作,我希望用户在注册后登陆,所以我做了以下事情:
localhost:12345/Onboarding/Start
的工作原理。页面显示正确。"CallbackPath": "/signin-oidc"
更改为"CallbackPath": "/Onboarding/Start"
localhost:12345/Onboarding/Start
。重要注意事项:与ADB2C样本和指南不同,您必须使用signon-oidc或自定义请求路径附加回复URL! Localhost:12345还不够!
我可以确认身份验证有效:Microsoft.AspNetCore.Authentication.Cookies.CookieAuthenticationHandler:信息:AuthenticationScheme:AzureADB2CCookie已登录.Microsoft.AspNetCore.Hosting.Internal.WebHost:信息:请求在56.201ms完成然后,当我在浏览器中手动导航到/ 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..
但我从未被重定向到意图/开始页面。相反,我回到根主页,就像我使用'signin-oidc'时发生的那样。为什么这样,我怎么让它停止去那里?
2.如何根据您是否登录IN或UP来更改回复URL?我无法真正为另一个使用注册策略和另一个单独的登录策略,因为回复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 );
} );
从Web应用程序传递到Azure AD B2C的身份验证请求可以包含两个重定向URL:
/signin-oidc
。/Onboarding/Start
。您的Web应用程序可以设置返回URL,如下所示:
public class AccountController : Controller
{
public IActionResult SignUp()
{
return this.Challenge(
new AuthenticationProperties()
{
RedirectUri = Url.Action("Start", "Onboarding", values: null, protocol: Request.Scheme)
},
AzureADB2CDefaults.AuthenticationScheme);
}
}
ChallengeResult
对象为由AzureADB2CAuthenticationBuilderExtensions.AddAzureADB2C
方法添加的Azure AD B2C身份验证中间件创建身份验证质询。
ChallengeResult
构造函数的第一个参数调用由Azure AD B2C身份验证中间件注册的OpenID Connect身份验证处理程序。
此构造函数的第二个参数设置在Azure AD B2C身份验证中间件处理身份验证响应后将返回最终用户的返回URL。