logout I am implementing the logout functionality. We are using AzureADB2C to authenticate and SQL server for authorizing the roles. My company Azure admin has configured all the policy information, redirects, signin and signedout callback paths etc..
I am displaying a custom login page with username and password. Upon clicking on the login button, I am able to authenticate and authorize users and default page is displayed. Also when i click on the logout link, It is logging out (i.e clearing the session), but redirecting user to azure B2C login screen. (attached image)
How to redirect user to custom login page when user click on the logout link. Currently the logout link href is defined to MicrosoftIdentity/Account/SignOut. If this is not the correct way, how to clear user session and redirect user to custom login page?
I tried following code for logout functionality.
//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();
I really appreciate your help and direction from here.
You could manually handle the response in OnSignedOutCallbackRedirect
like following:
builder.Services.AddAuthentication(OpenIdConnectDefaults.AuthenticationScheme)
.AddMicrosoftIdentityWebApp(options =>
{
builder.Configuration.Bind("AzureAdB2c", options);
options.Events.OnSignedOutCallbackRedirect = async context =>
{
context.Response.Redirect("/CustomPage"); // Change this to your desired URL
context.HandleResponse(); // Prevent the default behavior
await Task.CompletedTask;
};
});