首页 > 解决方案 > 连接到本地 Identity Server 4 的新 Blazor 项目

问题描述

当我创建新的 Blazor 项目时,可以选择通过Individual User Accounts,使用身份验证Connect to an existing user store in the cloud。我安装并配置了本地 Identity Server 4。我可以使用它进行身份验证吗?在这种情况下,我应该为 、 和 选项指定Sign-up or Sign-ii policy哪些Reset Password Policy参数Edit Profile Policy

在此处输入图像描述

标签: asp.net-coreidentityserver4blazor

解决方案


不,该选项用于连接 Azure AD B2C 应用程序,这是 Microsoft 的云身份服务之一。

如果要连接本地 Identity Server 4,可以先安装包IdentityServer4,然后在 blazor 应用中添加认证,使用 OIDC 中间件连接 IDS4:

services.AddAuthentication(options =>
{
    options.DefaultScheme = "Cookies";
    options.DefaultChallengeScheme = "oidc";
})
.AddCookie("Cookies")
.AddOpenIdConnect("oidc", options =>
{
    options.Authority = "http://localhost:5000";
    options.RequireHttpsMetadata = false;

    options.ClientId = "mvcBlazor";
    options.SaveTokens = true;
    options.Scope.Add("openid");
    options.Scope.Add("profile");
});

services.AddMvcCore(options =>
{
    var policy = new AuthorizationPolicyBuilder()
        .RequireAuthenticatedUser()
        .Build();
    options.Filters.Add(new AuthorizeFilter(policy));
});

app.UseAuthentication();

您可以参考以下文章的代码示例和解释:

https://nightbaker.github.io/blazor/identityserver4/serverapp/2019/08/29/blazor-serverapp-identity-server-4/


推荐阅读