Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I define the SignedOut page in Microsoft.Identity.Web?

I'm successfully signing in and out using Azure AD B2C in a Blazor Server app, but it's not clear to me the proper way to define the SignedOut page. This question seems to be more applicable to Microsoft.Identity.Web.UI, because this SignedOut page seems to be hardcoded to a very generic SignedOut.cshtml:

Signed out
You have successfully signed out.

The documentation seems to indicate this can be changed, but it does not say how exactly.

From the documentation: "By default, the logout URL displays the signed-out view page SignedOut.cshtml.cs. This page is also provided as part of MIcrosoft.Identity.Web."

Given I'm writing a Blazor app, I tried creating a SignedOut.razor component, but that did not override it:

@page "/MicrosoftIdentity/Account/SignedOut"

Here is the source from Microsoft.Identity.Web.UI. As you can see it's hardcoded.

public IActionResult SignOut([FromRoute] string scheme)
{
    if (AppServicesAuthenticationInformation.IsAppServicesAadAuthenticationEnabled)
    {
        return LocalRedirect(AppServicesAuthenticationInformation.LogoutUrl);
    }
    else
    {
        scheme ??= OpenIdConnectDefaults.AuthenticationScheme;
        var callbackUrl = Url.Page("/Account/SignedOut", pageHandler: null, values: null, protocol: Request.Scheme);
        return SignOut(
             new AuthenticationProperties
             {
                 RedirectUri = callbackUrl,
             },
             CookieAuthenticationDefaults.AuthenticationScheme,
             scheme);
    }
}
like image 466
Jason Honingford Avatar asked Dec 03 '20 03:12

Jason Honingford


2 Answers

Microsoft.Identity.Web v1.9

Updated: Here's my preferred method

Just add this to your startup.cs under Configure. Here I've just redirected to my home page, but you can redirect to your own custom signout page if you wish.

app.UseRewriter(
new RewriteOptions().Add(
    context =>
    {
        if (context.HttpContext.Request.Path == "/MicrosoftIdentity/Account/SignedOut")
        {
            context.HttpContext.Response.Redirect("/");
        }
    }));

Method #2

While writing the question I did find one way to do this that is very simple. It still seems odd this is the intended way, so please feel free to improve or add better answers. I suspect new versions will come out to make this easier.

Because Microsoft.Identity.Web.UI is a Reusable Class Library (RCL), any page can be overridden just by adding it to your web app in the same location.

As you can see, I almost accomplished this by creating my own SignedOut.razor page and giving it the same path as the URL. That doesn't work, because it's a razor component, and it has to match the path in the source, not the URL in the web app.

Thankfully it's open source. I had to find the path here, since it wasn't obvious to me. https://github.com/AzureAD/microsoft-identity-web

So here is the correct path you need in your project and the best answer I could find that is working to give yourself a real SignedOut page. I suppose you'd have to add a redirect here if you did not want a SignedOut page.

Areas/MicrosoftIdentity/Pages/Account/SignedOut.cshtml

like image 53
Jason Honingford Avatar answered Sep 25 '22 15:09

Jason Honingford


I followed Jason's solution offered above and added the SignedOut.cshtml file to my Blazor Server project. I then modified the OnGet() method to redirect back my blazor app's home page:

public class SignedOut : PageModel
{
    public ActionResult OnGet()
    {
        return Redirect("/");
    }
}

For reference, my project folder layout looks somewhat like this:

  • AppRoot
    • Areas
      • MicrosoftIdentity
        • Pages
          • Account
            • SignedOut.cshtml
    • Pages
    • Shared
    • wwwroot
like image 40
Austin Kaylor Avatar answered Sep 26 '22 15:09

Austin Kaylor