如何使用令牌重定向到Blazor

问题描述 投票:0回答:1

我有一个Middleware执行身份验证,然后应该重新路由到Blazor Web应用程序。

问题是我在请求查询中放入了token,我希望它在请求的主体中。

中间件:

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);
}

配置:

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切入点:

服务器重定向到:http://localhost:[portno]/?token=[a string],我不知道为什么。我尝试为Blazor的入口页设置两个路由并且它没有加载它。

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

}

PS:我不明白为什么服务器将token放在查询字符串中?

cookies middleware blazor
1个回答
1
投票

1)要从get参数中检索token,你应该解析current url,你可以在你的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)当你谈到在体内发送令牌时,我不明白你问题的第二部分。

© www.soinside.com 2019 - 2024. All rights reserved.