Search code examples
cookiesmiddlewareblazor

How to redirect to Blazor with token


I have a Middleware which performs an authentification and should then reroute to a Blazor web application.

The problem is that I get the token put in the request query and I want it in the body of the request.

Middleware:

public async Task Invoke(HttpContext context) {
    string token = context.Request.Query["token"];

    if (!context.User.Identity.IsAuthenticated) {
         //do some logic to authenticate
    }
    else  
        await this.next(context);
}

Configure:

public void Configure(IApplicationBuilder app, IHostingEnvironment env) 
{
    app.UseResponseCompression();
    app.UseAuthentication();
    app.UseMiddleware<MultiAuthWare>();

    app.UseMvc(routes => {
                routes.MapRoute(name: "default", template: "{controller}/{action}/{id?}");
    });

    app.UseBlazor<Client.Startup>();
}

Blazor entry point:

The server redirects to : http://localhost:[portno]/?token=[a string] and I do not know why.Any who i have tried setting both routes for the entry page of Blazor and it does not load it.

@page "/"
@page "/?token={token}"
@inherits HomeBase
@functions()
{

}

PS: I do not understand why does the server put the token in the query string ?


Solution

  • 1) To retrieve token from get parameters you should to parse current url, you can do it in your HomeBase:

            var url = UriHelper.GetAbsoluteUri();  // By injection (see link)
            var uriBuilder = new UriBuilder(url);  // System namespace
            var q = System.Web.HttpUtility.ParseQueryString(uriBuilder.Query);
            var token = q["token"];
    

    2) I don't understand the second part of your question, when you talk about to send token in body.

    More info at Get current Url in a Blazor component