我们使用 Microsoft 的 webapp 示例在前端的 Umbraco 中实现了 Azure AD B2C https://github.com/Azure-Samples/active-directory-b2c-dotnet-webapp-and-webapi
大多数时候,这通常是有效的,但过了一段时间,每个人都开始受到重定向循环的影响。重新访问网站即可解决问题。
当用户使用 id 令牌重定向回站点时,这似乎是导致 .AspNet.Cookies cookie 停止设置的原因。
有什么想法吗?
对于遇到同样问题并找到此问题的人,我想分享在我的案例中导致此问题的原因以及我如何解决它。
AD B2C 应用程序注册期望有一个 RedirectURI。我忘了放
signin-oidc
如此改变:
https://localhost:5000
至
https://localhost:5000/signin-oidc
解决了我的问题。
这是默认值 -
/signin-oidc
- 除非明确设置了其他内容。
我在注销时遇到无限循环问题,这是因为缺少 Razor 页面的支持。默认 Microsoft.Identity.Web.UI SignOut 操作使用 /Account/SignedOut Razor 页面作为回调 URL。
var callbackUrl = Url.Page("/Account/SignedOut", pageHandler: null, values: null, protocol: Request.Scheme);
我在我的 Asp.Net core Web 应用程序中添加了 Razor 支持,它解决了问题。
services.AddRazorPages();
和
app.UseEndpoints(endpoints =>
{
endpoints.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}");
endpoints.MapRazorPages();
});
请确保您的应用程序注册中的回复 URL 与 web.config 中的重定向 URI 匹配。尝试将这两个设置设置为您的主主页 URL,以确保您的应用程序正确注册。另请确保应用程序 ID 和客户端 ID 匹配,并且在 Web 配置中设置了正确的租户。这需要是 onmicrosoft.com 租户。另外,请确保您的用户拥有应用程序的正确权限。
请按照我的博客和视频中的说明进行操作,以确保这些设置正确。 https://medium.com/@marilee.turscak/reply-urls-vs-postlogoutredirecturis-in-azure-active-directory-aad-20f57a03267b
https://www.youtube.com/watch?v=A9U1VGyztEM
您还可以尝试删除该应用程序并重新发布。如果这些都不起作用,那么实际上可能是平台本身的问题。
仅在 Web 应用程序中的 TLS/SSL 设置下启用 HTTPS。
对我来说,这是因为我的 b2c 配置设置中没有定义范围,如下所示:
"Resources": {
"myApi": {
"ResourceUri": "https://localhost:44361",//"https://my.ui.com",
"ResourceScopes": [
"https://myapp.onmicrosoft.com/my-api/Admin.Read.Write" // this was wrong, which caused my looping
]
}
}
对我来说,这是 Azure B2C 中过期的秘密/证书。查看网络日志以查看是否有任何消息很重要,值得庆幸的是,有消息告诉我到底要在哪里查看
我还遇到了注销重定向循环。它实际上会注销,但只是陷入循环。就我而言,我在 Azure 中配置的重定向 URL 很好(我有
/signin-oidc
)。
我按照指南添加自己的帐户控制器操作,而不是使用内置的
MicrosoftIdentity/Account/SignOut
(同时还添加 id_token
验证以确保注销):https://learn.microsoft.com/en-us /azure/active-directory-b2c/enable-authentication-web-application-options#secure-your-logout-redirect
我的startup.cs代码按照文档,我的控制器代码如下所示(文档代码缺少
AuthenticationProperties
变量):
namespace Cosmos.WebPortal.Controllers;
[AllowAnonymous]
[Area("MicrosoftIdentity")]
[Route("[area]/[controller]/[action]")]
public class MyAccountController : Controller
{
[HttpGet("{scheme?}")]
public async Task<IActionResult> SignOutAsync([FromRoute] string scheme)
{
scheme ??= OpenIdConnectDefaults.AuthenticationScheme;
var redirectUrl = Url.Content("~/");
var properties = new AuthenticationProperties { RedirectUri = redirectUrl };
//obtain the id_token
var idToken = await HttpContext.GetTokenAsync("id_token");
//send the id_token value to the authentication middleware
properties.Items["id_token_hint"] = idToken;
return SignOut(properties, CookieAuthenticationDefaults.AuthenticationScheme, scheme);
}
}
所以我的注销链接现在指向此控制器,例如'MicrosoftIdentity/MyAccount/SignOut'
这似乎工作正常,没有无限循环。有点令人沮丧,因为我不太明白原因或区别,但它有效。