Blazor Webapp (8.0) - 实现注销功能

问题描述 投票:0回答:1

注销 我正在实现注销功能。我们使用 AzureADB2C 进行身份验证,并使用 SQL Server 来授权角色。我的公司 Azure 管理员已配置所有策略信息、重定向、登录和注销回调路径等..

我正在显示带有用户名和密码的自定义登录页面。单击登录按钮后,我可以对用户进行身份验证和授权,并显示默认页面。此外,当我单击注销链接时,它正在注销(即清除会话),但将用户重定向到 azure B2C 登录屏幕。 (附图)

当用户单击注销链接时如何将用户重定向到自定义登录页面。目前,注销链接 href 定义为 MicrosoftIdentity/Account/SignOut。如果这不是正确的方法,如何清除用户会话并将用户重定向到自定义登录页面?

我尝试使用以下代码来实现注销功能。

     //Here is appsettings.json
    {
      "AzureAdB2c": {
        "callbackPath": "/signin-oidc",
        "signedOutCallbackPath": "/signout-callback-oidc",
        "clientId": "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxx",
        "domain": "xxxxxxxxb2cdev.onmicrosoft.com",
        "instance": "https://xxxxxxxxb2cdev.b2clogin.com/",
        "tenantId": "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxx",
         "SignUpSignInPolicyId": "B2C_1A_CC24PORTAL",
         "RedirectUri": "https://dev-xxxxxxxx-ui-eus-as.azurewebsites.net"
       },
       "BuildConfiguration": "DEV"
     }

//Here is logout link in NavMenu.razor
            <div class="nav-item px-3">
                 <NavLink class="nav-link" **href="MicrosoftIdentity/Account/SignOut"**>
                  <span class="bi bi-box-arrow-right-nav-menu" aria-hidden="true"></span> Logout
                </NavLink>
            </div>

//Here is program.cs

using Blazored.LocalStorage;
using CC24_UI.Components;
using Microsoft.AspNetCore.Authentication.OpenIdConnect;
using Microsoft.Identity.Web;
using Microsoft.Identity.Web.UI;
using System.Runtime;
using Serilog;
using Telerik.Blazor.Services;

var builder = WebApplication.CreateBuilder(args);

Log.Logger = new LoggerConfiguration()
.Enrich.FromLogContext()
.WriteTo.File(@"C:\home\logfiles\CC24.txt")
.CreateLogger();

Log.Information("builder environment: {0}", builder.Environment.EnvironmentName);

builder.Services.AddAuthentication(OpenIdConnectDefaults.AuthenticationScheme)
.AddMicrosoftIdentityWebApp(builder.Configuration.GetSection("AzureAdB2c"));

builder.Services.AddControllersWithViews()
.AddMicrosoftIdentityUI();

builder.Services.AddAuthorization(options =>
{
  // By default, all incoming requests will be authorized according to the default policy
  options.FallbackPolicy = options.DefaultPolicy;
});

builder.Services.AddRazorPages().AddMicrosoftIdentityUI();

builder.Services.AddRazorComponents()
    .AddInteractiveServerComponents().AddMicrosoftIdentityConsentHandler();

builder.Services.AddServerSideBlazor();

builder.Services.AddTelerikBlazor();

builder.Services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();

builder.Services.AddBlazoredLocalStorage();

builder.Services.AddHttpContextAccessor();

var app = builder.Build();

// Configure the HTTP request pipeline.
if (!app.Environment.IsDevelopment())
{
  app.UseExceptionHandler("/Error", createScopeForErrors: true);
  // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
  app.UseHsts();
}

app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseRouting();
app.UseAuthentication();
app.UseAuthorization();
app.MapControllers();
app.MapRazorComponents<App>().AddInteractiveServerRenderMode();
app.UseAntiforgery();

app.Run();

我非常感谢您在这里的帮助和指导。

asp.net-core blazor blazor-webapp
1个回答
0
投票

您可以更改重定向提供商地址,如下所示以手动处理它。

builder.Services.AddAuthentication(OpenIdConnectDefaults.AuthenticationScheme)
    .AddMicrosoftIdentityWebApp(options =>
    {
        builder.Configuration.Bind("AzureAd", options);
        options.Events.OnRedirectToIdentityProvider = async context =>
        {
            // Customize the login URL
            context.ProtocolMessage.IssuerAddress = "https://your-custom-login-url";
            await Task.CompletedTask;
        };
    });
© www.soinside.com 2019 - 2024. All rights reserved.