简体   繁体   English

.net 5 Azure b2c 注销循环

[英].net 5 Azure b2c logout loop

Hello I'm trying to implement azure b2c in an app.您好,我正在尝试在应用程序中实现 azure b2c。 Everything works fine logging in. But when I log out it get stuck in an infinite loop.登录时一切正常。但是当我注销时,它陷入了无限循环。 All the examples I can find seem to be .net core 3.1.我能找到的所有示例似乎都是 .net core 3.1。

Here is my appsettings.这是我的应用程序设置。

"AzureAdB2C": {
"Instance": "https://xxx.b2clogin.com",
"TenantId": "xxx",
"ClientId": "xxx",
"Domain": "xxx.onmicrosoft.com",
"SignedOutCallbackPath": "/signout/B2C_1_Normal",
"SignUpSignInPolicyId": "B2C_1_Normal",
//"CallbackPath": "/signin/B2C_1_Normal"
}

Here is my startup.这是我的启动。

 services.AddAuthentication(OpenIdConnectDefaults.AuthenticationScheme)
 .AddMicrosoftIdentityWebApp(options =>
 {
     Configuration.Bind("AzureAdB2C", options);
 }).EnableTokenAcquisitionToCallDownstreamApi(new string[] { Configuration["ApiScope"] 
 })
 .AddInMemoryTokenCaches
services.AddControllersWithViews(options=>
{
    var policy = new AuthorizationPolicyBuilder()
        .RequireAuthenticatedUser()
        .Build();
    options.Filters.Add(new AuthorizeFilter(policy));
}).AddMicrosoftIdentityUI();

To sign out, I just have a button click.要退出,我只需单击一个按钮。

<a class="nav-link" asp-area="MicrosoftIdentity" asp-controller="Account" asp-action="SignOut">Sign out</a>

It does sign me out, but I'm stuck in a redirect loop back to my app, and then back to the B2C.它确实让我退出,但我被困在一个重定向循环中回到我的应用程序,然后又回到 B2C。

I tried to test in my local environment , and it was tested successfully with sign in and sign out from my webapplication using AzureAdB2C credentials.我尝试在本地环境中进行测试,并且使用 AzureAdB2C 凭据从我的 Web 应用程序登录和注销成功地进行了测试。

Here are the steps that i followed:以下是我遵循的步骤:

Created a B2C tenant in Portal and link it to my subscription.在 Portal 中创建了一个 B2C 租户并将其链接到我的订阅。

在此处输入图像描述

After linked created an App registration with selected as below and authenticate with id and access token .链接后创建了一个应用注册,选择如下,并使用 id 和 access token 进行身份验证。

在此处输入图像描述

在此处输入图像描述

I have added the userflow and select the following to configure:我已经添加了用户流并选择以下配置:

在此处输入图像描述

Then in visual studio created an Asp.net core web application and provide the domain and policy details which i created in above然后在 Visual Studio 中创建了一个 Asp.net 核心 Web 应用程序并提供我在上面创建的域和策略详细信息

在此处输入图像描述

Domain name - Azure AD B2C Overview tab Application Id - From app registration overview tab Sign-up or sign-in policy - From user flows Overview域名 - Azure AD B2C 概览选项卡 应用程序 ID - 来自应用注册概览选项卡 注册或登录策略 - 来自用户流概览

Copied the reply URI and paste in redirect URI section in portal复制回复 URI 并粘贴到门户的重定向 URI 部分

在此处输入图像描述

Here is my appsettings.json file这是我的appsettings.json文件

在此处输入图像描述

And it allows me to sign-in and after sign-out it redirect to my home page as well :它允许我登录并在注销后也重定向到我的主页:

在此处输入图像描述

在此处输入图像描述

For more information please refer the below links:有关更多信息,请参阅以下链接:

. . How to sign out of Azure ADB2C |MS Q&A . 如何退出 Azure ADB2C |MS 问答

. . Configure session behavior in Azure Active Directory B2C | 在 Azure Active Directory B2C 中配置会话行为 | MS DOC 医学博士

I had the same issue and it was because of missing support of Razor pages.我遇到了同样的问题,这是因为缺少对 Razor 页面的支持。 The default Microsoft.Identity.Web.UI SignOut action uses /Account/SignedOut Razor page as callback url.默认的 Microsoft.Identity.Web.UI SignOut 操作使用 /Account/SignedOut Razor 页面作为回调 url。

var callbackUrl = Url.Page("/Account/SignedOut", pageHandler: null, values: null, protocol: Request.Scheme);

I added Razor support in my Asp.Net core web app and it fixed the issue.我在我的 Asp.Net 核心 Web 应用程序中添加了 Razor 支持,它解决了这个问题。

 services.AddRazorPages();

and

app.UseEndpoints(endpoints =>
        {
            endpoints.MapControllerRoute(
                name: "default",
                pattern: "{controller=Home}/{action=Index}/{id?}");
            endpoints.MapRazorPages();
        });

Alternatively you can use your own logout endpoint, which can use any action as signed out callback url.或者,您可以使用自己的注销端点,它可以使用任何操作作为注销回调 url。

Thanks.谢谢。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM