Pass response to Blazor (Server) page OnAuthenticationFailed

17 Views Asked by At

I want to pass an error to a Blazor (Server) page, which I receive from an OpenId error event to display and handle further action. I tried this in the event listener:

    options.Events = new OpenIdConnectEvents
    {
        OnAuthenticationFailed = ctx =>
        {
            ctx.Response.Redirect("/some-blazor-page");
            ctx.Response.ContentType = "text/plain";
            return ctx.Response.WriteAsync(ctx.Exception.Message);
        },
...

But I cannot read the reponse on the Blazor page. I guess something is going on in the pipline until that page is rendered:

@page "/some-blazor-page"

@inject IHttpContextAccessor httpContextAccessor

...

@code {
    
    private string? Error { get; set; }

    protected override async Task OnInitializedAsync()
    {
        httpContextAccessor.HttpContext.Request.EnableBuffering();
        var buffer = new byte[Convert.ToInt32(httpContextAccessor.HttpContext.Request.ContentLength)];
        await httpContextAccessor.HttpContext.Request.Body.ReadAsync(buffer, 0, buffer.Length);
        Error = Encoding.UTF8.GetString(buffer); // <-- Error == ""
    }

This does not work and httpContextAccessor.HttpContext.Request is empty. Is there another way to pass the error (except of URL parameters)?

0

There are 0 best solutions below