Azure Active Directory B2C注销问题

pn9klfpd  于 2023-05-01  发布在  其他
关注(0)|答案(1)|浏览(125)

我在MVC ASP中集成Azure ActiveDirectory B2C。NET Core Web应用程序。我添加了以下配置:

  • 在Appsettings中。json
"AzureADB2C": {
      "Instance": "https://tenant.b2clogin.com",
      "ClientId": "11111111-1111-1111-1111-111111111111",
      "Domain": "tenant.onmicrosoft.com",
      "CallbackPath": "/signin-oidc",
      "SignedOutCallbackPath": "/signout/B2C_1_SUSI",
      "SignUpSignInPolicyId": "B2C_1_SUSI"
  }

注意:在上面的配置示例中,我用虚拟值替换了真实的的租户和客户端ID。

  • 在Startup。CS
using Microsoft.AspNetCore.Authentication.OpenIdConnect;
using Microsoft.Identity.Web;
using Microsoft.Identity.Web.UI;

namespace WebApp
{
    public class Program
    {
        public static void Main(string[] args)
        {
            WebApplicationBuilder builder = WebApplication.CreateBuilder(args);

            ConfigurationManager configuration = builder.Configuration;

            IServiceCollection services = builder.Services;

            services
                .AddAuthentication(OpenIdConnectDefaults.AuthenticationScheme)
                .AddMicrosoftIdentityWebApp(configuration.GetSection(Constants.AzureAdB2C));

            services.AddControllersWithViews();

            services.AddRazorPages().AddMicrosoftIdentityUI();

            // this is for debugging purposes
            services.Configure<OpenIdConnectOptions>(
                OpenIdConnectDefaults.AuthenticationScheme,
                options =>
                {
                    options.Events.OnAccessDenied = async context => {
                        Console.WriteLine("OnAccessDenied");
                    };
                    options.Events.OnAuthenticationFailed = async context => {
                        Console.WriteLine("OnAuthenticationFailed");
                    };
                    options.Events.OnAuthorizationCodeReceived = async context => {
                        Console.WriteLine("OnAuthorizationCodeReceived");
                    };
                    options.Events.OnMessageReceived = async context => {
                        Console.WriteLine("OnMessageReceived");
                    };
                    options.Events.OnRedirectToIdentityProvider = async context => {
                        Console.WriteLine("OnRedirectToIdentityProvider");
                    };
                    options.Events.OnRedirectToIdentityProviderForSignOut = async context => {
                        Console.WriteLine("OnRedirectToIdentityProviderForSignOut");
                    };
                    options.Events.OnRemoteFailure = async context => {
                        Console.WriteLine("OnRemoteFailure");
                    };
                    options.Events.OnRemoteSignOut = async context => {
                        Console.WriteLine("OnRemoteSignOut");
                    };
                    options.Events.OnSignedOutCallbackRedirect = async context => {
                        Console.WriteLine("OnSignedOutCallbackRedirect");
                    };
                    options.Events.OnTicketReceived = async context => {
                        Console.WriteLine("OnTicketReceived");
                    };
                    options.Events.OnTokenResponseReceived = async context => {
                        Console.WriteLine("OnTokenResponseReceived");
                    };
                    options.Events.OnTokenValidated = async context => {
                        Console.WriteLine("OnTokenValidated");
                    };
                    options.Events.OnUserInformationReceived = async context => {
                        Console.WriteLine("OnUserInformationReceived");
                    };
                });

            var app = builder.Build();

            if (app.Environment.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
            else
            {
                app.UseExceptionHandler("/Home/Error");
                app.UseHsts();
            }

            app.UseHttpsRedirection();
            app.UseStaticFiles();
            app.UseRouting();

            app.UseAuthentication();
            app.UseAuthorization();

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

                endpoints.MapRazorPages();
            });

            app.Run();
        }
    }
}

注册和登录流程工作完美,但当试图注销时失败了。
我正在通过/MicrosoftIdentity/Account/SignedOut触发注销过程
问题在于,该过程在OnRedirectToIdentityProviderForSignOut和OnSignedOutCallbackRedirect之间无限循环。
有什么办法让它正常工作吗?

vmjh9lq9

vmjh9lq91#

在你的启动。cs文件中,修改OnRedirectToIdentityProviderForSignOut事件以显式设置post_logout_redirect_uri参数。使用以下代码替换现有的OnRedirectToIdentityProviderForSignOut事件:

options.Events.OnRedirectToIdentityProviderForSignOut = async context =>
{
    Console.WriteLine("OnRedirectToIdentityProviderForSignOut");

    // Set the post_logout_redirect_uri parameter
    context.ProtocolMessage.SetPostLogoutRedirectUri(context.Properties.RedirectUri);

    await Task.CompletedTask;
};

相关问题