首页 > 解决方案 > asp.net core IdentityServer 4没有持久授权Safari

问题描述

我有一个使用身份 4 进行身份验证的 asp.net core blazor wasm 应用程序。它根据 Visual Studio 进行设置并部署到 Azure 应用服务。当我通过 Edge 在 Windows 上使用它时,它按预期工作,我可以登录并在表 PersistedGrants 中看到一个新条目,并且我可以使用多台计算机登录而不会出现问题。

当我知道从 Mac 上的 Safari 或 iOS 设备上的任何浏览器登录时(因为它们都使用相同的浏览器引擎),整个 PersistedGrants 表将被删除。据我所知,如果我使用已经获得授权的同一个用户或另一个用户登录并不重要,所有条目都将被删除。登录过程在所有情况下都返回成功。

在包方面,我使用 Microsoft.AspNetCore.ApiAuthorization.IdentityServer 3.1.4 和 Blazor 的发布版本。

我的启动:

...
services.AddDbContext<ISupportToolContext, SupportToolContext>(options => options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));
        services.AddDefaultIdentity<ApplicationUser>(options => options.SignIn.RequireConfirmedAccount = true)
                .AddRoles<ApplicationIdentityRole>()
                .AddEntityFrameworkStores<SupportToolContext>();

        Log.Information($"Identity Server Key Type loaded: {Configuration.GetSection("IdentityServer:Key")["Type"]}");

        services.AddIdentityServer()
                .AddApiAuthorization<ApplicationUser, SupportToolContext>();

        services.AddAuthentication()
                .AddIdentityServerJwt();

        services.Configure<IdentityOptions>(options =>
        {
            // Password settings
            options.Password.RequireDigit = false;
            options.Password.RequiredLength = MIN_PASSWORD_LENGTH;
            options.Password.RequireNonAlphanumeric = false;
            options.Password.RequireUppercase = false;
            options.Password.RequireLowercase = false;

            // Lockout settings
            options.Lockout.DefaultLockoutTimeSpan = TimeSpan.FromMinutes(LOCKOUT_DURATION);
            options.Lockout.MaxFailedAccessAttempts = MAX_TRIES_BEFORE_LOCKOUT;
            options.Lockout.AllowedForNewUsers = true;

            // Email Settings
            options.User.RequireUniqueEmail = true;
            options.SignIn.RequireConfirmedEmail = true;
        });

        // Configure LifeSpan of Identity email tokens
        services.Configure<DataProtectionTokenProviderOptions>(options =>
        {
            options.TokenLifespan = TimeSpan.FromDays(IDENTITY_TOKEN_DURATION);
        });

        services.ConfigureApplicationCookie(options =>
        {
            options.Cookie.HttpOnly = false;
            options.Events = new CookieAuthenticationEvents
            {
                OnRedirectToLogin = context =>
                {
                    context.Response.StatusCode = UNAUTHORIZED_STATUS_CODE;
                    return Task.CompletedTask;
                }
            };
        });
        ...

和客户端 Programm.cs:

        var builder = WebAssemblyHostBuilder.CreateDefault(args);
        builder.RootComponents.Add<App>("app");

        builder.Services.AddBlazoredModal();
        builder.Services.AddI18nText();
        builder.Services.AddAuthorizationCore();

        // Service registration
        ...

        builder.Services.AddHttpClient("ApplySupportTool.ServerAPI", client => client.BaseAddress = new Uri(builder.HostEnvironment.BaseAddress))
               .AddHttpMessageHandler<BaseAddressAuthorizationMessageHandler>();

        // Supply HttpClient instances that include access tokens when making requests to the server project
        builder.Services.AddTransient(sp => sp.GetRequiredService<IHttpClientFactory>().CreateClient("ApplySupportTool.ServerAPI"));

        builder.Services.AddApiAuthorization();
        builder.Services.AddOptions();

        await builder.Build().RunAsync();

在 index.html 中:

<script src="_content/Microsoft.AspNetCore.Components.WebAssembly.Authentication/AuthenticationService.js"></script>
<script src="_framework/blazor.webassembly.js"></script>

谁能指出我可能是什么或如何找到更多信息?

标签: asp.net-core-webapiidentityserver4blazor-client-side

解决方案


推荐阅读