Search code examples
identityserver4blazor

Blazor, EntityServer4 routing to login


In the Blazor WebAssembly (hosted) with Authentication template, the Login button on the header is

<a href="authentication/login">Log in</a>

However, on the server the login page is at:

/Areas/Identity/Pages/Account/Login (.cshtml)

Note: The cshtml page has @page directive but does not specify a roue.

So, how does the system resolve "authentication/login" to "/Areas/Identity/Pages/Account/Login?


Solution

  • <a href="authentication/login">Log in</a>
    

    The value of the href attribute is a relative local url...It points to the Authentication component in your client app. The second segment of the url, login, is actually a parameter passed to the Authentication component. Here's the definition of the component:

    Authentication.razor

    @page "/authentication/{action}"
    @using Microsoft.AspNetCore.Components.WebAssembly.Authentication
    <RemoteAuthenticatorView Action="@Action" />
    
    @code{
        [Parameter] public string Action { get; set; }
    }
    

    Note that the login string is assigned to the Action attribute of the RemoteAuthenticatorView component that handles remote authentication operations. In this case telling her that we wish to login the user. If you're interested to know how the url is created and follow the whole process of the authentication, you may consult the classes that are involved in the this process... I deem it a waste of time, though. And complicated.

    The cshtml page has @page directive but does not specify a roue

    This is a Razor Pages page, not a Razor Page component. A Razor Page component, also known as routable must be annotated with a @page directive plus route template, as for instance: @page "/counter". This tell Blazor that the current component is routable, and its relative url is given in the route template The @page directive, without a route template, in Razor Pages page, such as the Login.cshtml, "makes the file into an MVC action - which means that it handles requests directly, without going through a controller."