BlazorWASMCheckSessionLoop
创始人
2024-12-21 20:00:22
0

Blazor WASM CheckSession 循环问题通常是由于在操作 Blazor WebAssembly 应用时使用了旧浏览器版本或强制刷新的情况下发生的。为了解决此问题,可以在本地存储中保留用户信息并在重启应用程序时恢复会话。以下是一个示例实现:

在 Startup.cs 文件中添加以下代码:

public void ConfigureServices(IServiceCollection services)
{
    services.AddAuthorizationCore();

    // Add the authentication services
    services.AddAuthentication(options =>
    {
        options.DefaultScheme = JwtBearerDefaults.AuthenticationScheme;
        options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
    })
    .AddJwtBearer(options =>
    {
        options.Authority = "https://my-auth-server.com/";
        options.Audience = "my-audience";

        // Add the token to the SignalR connection
        options.Events = new JwtBearerEvents()
        {
            OnMessageReceived = context =>
            {
                context.Token = context.Request.Query["access_token"];
                return Task.CompletedTask;
            }
        };
    });

    services.AddControllers();
    services.AddSpaStaticFiles(configuration =>
    {
        configuration.RootPath = "ClientApp/dist";
    });
}

public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
    // Store the user information in the local storage
    app.Use(async (context, next) =>
    {
        var user = context.User;
        if (user.Identity.IsAuthenticated)
        {
            var claims = new List();
            claims.Add(new Claim("name", user.Identity.Name));
            claims.AddRange(user.Claims);

            var identity = new ClaimsIdentity(claims, "Bearer");
            var principal = new ClaimsPrincipal(identity);

            await context.RequestServices.GetRequiredService()
                .HttpContext.Authentication.SignInAsync("Bearer", principal);
        }

        await next.Invoke();
    });

    app.UseHttpsRedirection();

    app.UseRouting();

    app.UseAuthentication();
    app.UseAuthorization();

    app.UseEndpoints(endpoints =>
    {
        endpoints.MapControllers();
    });

    app.UseSpaStaticFiles();
    app.UseSpa(spa =>
    {
        spa.Options.DefaultPageStaticFileOptions = new StaticFileOptions

相关内容

热门资讯

私人局九分钟!智星德州菠萝we... 私人局九分钟!智星德州菠萝wepower插件(本来真的有挂)-今日头条1、全新机制【智星德州菠萝软件...
安装三分钟!德州扑克后台监控w... 安装三分钟!德州扑克后台监控wepoke辅助有挂(果真真的有挂)-知乎1、德州扑克后台监控系统规律教...
最新款两分钟!微扑克俱乐部机器... 最新款两分钟!微扑克俱乐部机器人wepoke软件透明是真的(总是真的有挂)-哔哩哔哩微扑克俱乐部机器...
靠谱两分钟!鱼扑克有挂德扑之星... 靠谱两分钟!鱼扑克有挂德扑之星ai代打(其实真的有挂)-今日头条1、进入到德扑之星ai代打黑科技之后...
打法八分钟!微扑克发牌规律性总... 打法八分钟!微扑克发牌规律性总结德扑之星里面可以隐藏入池率(一直真的有挂)-知乎1、很好的工具软件,...
安卓版本三分钟!德州智能辅助w... 安卓版本三分钟!德州智能辅助wpk可以提高胜率(的确真的有挂)-微博客户端;1.wpk可以提高胜率 ...
免费3分钟!wepoke辅助机... 免费3分钟!wepoke辅助机器人poker master安卓版外挂(其实真的有挂)-哔哩哔哩;1、...
私人房六分钟!微扑克有辅助挂w... 私人房六分钟!微扑克有辅助挂wepoke软件机器人(其实真的有挂)-小红书1、微扑克有辅助挂ai机器...
俱乐部六分钟!红龙扑克是正规的... 俱乐部六分钟!红龙扑克是正规的wpk线上代打(好像真的有挂)-知乎红龙扑克是正规的辅助器中分为三种模...
德州9分钟!智星德州菠萝成牌闯... 德州9分钟!智星德州菠萝成牌闯关德扑之星可以埋牌(一直真的有挂)-知乎1、用户打开应用后不用登录就可...