How to redirect to Blazor with token

自作多情 提交于 2020-02-02 13:14:20

问题


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 ?


回答1:


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



来源:https://stackoverflow.com/questions/55340677/how-to-redirect-to-blazor-with-token

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!