Identityserver4 如何配置Blazor客户端以使用外部标识服务器4服务器执行身份验证
我们正在开发由Blazor服务器(针对.NET Core 3,使用ElectronNET.API 5.22.14)和Blazor客户端(针对.NET标准2.1)组成的新产品 我们不想在Blazor服务器中托管identity server,因为我们有一个现有的identity server 4服务器 是否可以在Blazor客户端中显示带有登录/注册选项的登录页面,该客户端通过IdentityServer4服务器进行身份验证(例如identity server中的本地DB登录)?-我在网上找到的所有示例都在Blazor服务器中托管identity serverIdentityserver4 如何配置Blazor客户端以使用外部标识服务器4服务器执行身份验证,identityserver4,blazor-client-side,Identityserver4,Blazor Client Side,我们正在开发由Blazor服务器(针对.NET Core 3,使用ElectronNET.API 5.22.14)和Blazor客户端(针对.NET标准2.1)组成的新产品 我们不想在Blazor服务器中托管identity server,因为我们有一个现有的identity server 4服务器 是否可以在Blazor客户端中显示带有登录/注册选项的登录页面,该客户端通过IdentityServer4服务器进行身份验证(例如identity server中的本地DB登录)?-我在网上找到的所
在线文档中是否有一个示例或章节概述Blazor客户端的正确设置?例如,如何配置
Startup.cs
这可能有效。我正在创建一个没有任何外部插件的简单令牌传递模式。我确信有某种方式它不会那么好,但是它在没有外部依赖的情况下完成了工作。尽管如此,这一方法看起来是可靠的
这是完全可能的。
对于BlazorWebAssembly(前端托管模式),我们可以执行以下操作
让我们假设在https://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"
}
}
现在我们必须在Program.cs上配置Blazor客户端应用程序的OpenId连接设置
注意,有一个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,以便在用户通过身份验证后询问凭据并存储令牌。您是否找到解决方案?我也有同样的情况。
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";
});