Page 1 of 1

Benachrichtigen Sie den externen APIs, dass der Benutzer angemeldet ist

Posted: 03 Jun 2025, 10:04
by Anonymous
Ich habe Azure Enra externe ID implementiert. Ich kann mich anmelden und mich abmelden. In der Datei programm.cs Wenn ich die Entra -ID konfiguriert habe, verwende ich die Identitätsanmeldung und Abmeldeendpunkte. Ich kann den Code nicht ändern. Ich möchte nur einen Anruf bei der externen API vor dem Logout haben. Hier ist meine Programme.cs -Datei. HHW kann ich einen Vor-Logout-Anruf tätigen?

Code: Select all

using Microsoft.AspNetCore.Authentication.OpenIdConnect;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc.Authorization;
using Microsoft.Identity.Web;
using Microsoft.Identity.Web.UI;
using System.IdentityModel.Tokens.Jwt;

var builder = WebApplication.CreateBuilder(args);

JwtSecurityTokenHandler.DefaultMapInboundClaims = false;

builder.Services.AddMicrosoftIdentityWebAppAuthentication(builder.Configuration)
.EnableTokenAcquisitionToCallDownstreamApi(
[
builder.Configuration.GetSection("DownstreamApi:Scopes:Read").Get()!,
builder.Configuration.GetSection("DownstreamApi:Scopes:Write").Get()!
]
)
.AddDownstreamApi("DownstreamApi", builder.Configuration.GetSection("DownstreamApi"))
.AddInMemoryTokenCaches();

builder.Services.AddControllersWithViews(options =>
{
var policy = new AuthorizationPolicyBuilder()
.RequireAuthenticatedUser()
.Build();
options.Filters.Add(new AuthorizeFilter(policy));
}).AddMicrosoftIdentityUI();

builder.Services.AddRazorPages();// Add services to the container.

builder.Services.AddControllersWithViews();

var app = builder.Build();

// Configure the HTTP request pipeline.
if (!app.Environment.IsDevelopment())
{
app.UseExceptionHandler("/Home/Error");
// 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.UseAuthorization();

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

app.Run();

async Task TestMethodAsync(HttpContext context)
{
// Actual async work here
Console.WriteLine("Performing pre-logout actions");
await Task.Delay(1);
}

// This is what I have tried but it's not working
//builder.Services.ConfigureApplicationCookie(options =>
//{
//    options.Events.OnSigningOut = async context =>
//    {
//        // Call your custom method here
//        await TestMethodAsync(context.HttpContext);
//    };
//});