0

我们正在开发一个由 Blazor 服务器(面向 .NET Core 3 并使用 ElectronNET.API 5.22.14)和 Blazor 客户端(面向 .NET Standard 2.1)组成的新产品。

我们不想在 Blazor 服务器中托管身份服务器,因为我们有一个现有的 IdentityServer4 服务器。

是否可以在通过 IdentityServer4 服务器进行身份验证的 Blazor 客户端中显示带有登录/注册选项的登录页面(例如身份服务器中的本地数据库登录)?- 我在网上找到的所有示例都在 Blazor 服务器中托管身份服务器。

在线文档中是否有示例或部分概述了 Blazor 客户端的正确设置?例如如何配置Startup.cs.

4

2 回答 2

1

这可能会奏效。我正在创建一个没有任何外部插件的简单令牌传递模式。我敢肯定有某种方式它不会那么好,但它可以在没有外部依赖的情况下完成工作。不过,这种方法看起来很可靠。

https://chrissinty.com/securing-your-blazor-apps-authentication-with-clientside-blazor-using-webapi-aspnet-core-identity/

于 2019-10-15T19:26:28.733 回答
0

这是完全可能的。对于 Blazor Web 程序集(前端托管模式),我们可以执行以下操作。

假设我们有一个正在运行的 IdentityServer 4https://localhost:5001和一个运行 Blazor Web Assembly 的客户端 SPA 应用程序https://localhost:5003

对于 Identity Server,让我们配置一个新客户端。

new Client
{
    ClientId = "spa",
    ClientUri = "https://localhost:5003",
    AllowedGrantTypes = GrantTypes.Code,

    RequireClientSecret = false, // for auth code flow there is no secret required as it couldn't be securely stored in the front-end anyway

    // where to redirect to after login
    RedirectUris = { "https://localhost:5003/authentication/login-callback" },
    
    // where to redirect to after logout
    PostLogoutRedirectUris = { "https://localhost:5003/signout-callback-oidc" },

    // CORS
    AllowedCorsOrigins =     { "https://localhost:5003" },

    AllowedScopes = new List<string>
    {
        IdentityServerConstants.StandardScopes.OpenId,
        IdentityServerConstants.StandardScopes.Profile,
        "api1"
    }
}

对于 Blazor 应用程序,假设我们已经创建了一个默认应用程序并选择了身份验证Store user accounts in-app,以便它为登录功能创建样板并添加包

<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly.Authentication" Version="3.2.1" />

现在我们必须在 Program.cs 中配置 Blazor 客户端应用的 OpenId Connect 设置。

请注意,默认情况下可以读取 appsettings.json,builder.Configuration.Bind("Local", options.ProviderOptions);为简单起见,我们可以手动硬编码设置。

builder.Services.AddOidcAuthentication(
    options =>
    {
        //let's hardcode the values for now. We can enable reading from settings later.
        //builder.Configuration.Bind("Local", options.ProviderOptions);

        options.ProviderOptions.Authority = "https://localhost:5001";
        options.ProviderOptions.ClientId = "spa";
        options.ProviderOptions.DefaultScopes.Add("openid");
        options.ProviderOptions.DefaultScopes.Add("profile");
        options.ProviderOptions.DefaultScopes.Add("api1");
        options.ProviderOptions.PostLogoutRedirectUri = "https://localhost:5003/counter";
        options.ProviderOptions.RedirectUri = "https://localhost:5003/authentication/login-callback";
        options.ProviderOptions.ResponseType = "code";
    });

该配置应该足以让 blazor 应用程序重定向到 Identity Server 以询问凭据并在用户通过身份验证后存储令牌。

于 2020-07-25T13:38:03.317 回答