IdentityServer4

IdentityServer4 快速入门8:使用ASP.NET

2020-04-29  本文已影响0人  Charles2018

注意

对于任何先决条件(例如模板),请先查看概述。

IdentityServer专为灵活性而设计,其中一部分使您可以将所需的任何数据库用于用户及其数据(包括密码)。如果您是从新的用户数据库开始的,则可以选择ASP.NET Identity。本快速入门介绍了如何在IdentityServer中使用ASP.NET Identity。

此快速入门使用ASP.NET Identity的方法是为IdentityServer主机创建一个新项目。这个新项目将替代我们在先前的快速入门中建立的先前的IdentityServer项目。这个新项目的原因是由于使用ASP.NET Identity时UI资产的差异(主要是登录和注销的差异)。此解决方案中的所有其他项目(针对客户端和API)将保持不变。

注意

本快速入门假定您熟悉ASP.NET Identity的工作方式。如果不是,建议您首先了解它

新建项目为ASP.NET身份

第一步是将ASP.NET Identity的新项目添加到您的解决方案中。我们提供了一个模板,其中包含使用IdentityServer进行ASP.NET Identity所需的最少UI资产。最终,您将删除IdentityServer的旧项目,但是需要迁移一些项目。

首先创建一个将使用ASP.NET身份的新IdentityServer项目:

cd quickstart/src
dotnet new is4aspid -n IdentityServerAspNetIdentity

当提示“种子”用户数据库时,为“是”选择“是”。这将用我们的“ alice”和“ bob”用户填充用户数据库。他们的密码是“ Pass123 $”。

注意

该模板使用Sqlite作为用户数据库,并且EF迁移已在模板中预先创建。如果希望使用其他数据库提供程序,则需要更改代码中使用的提供程序并重新创建EF迁移。

检查新的项目

在您选择的编辑器中打开新项目,然后检查生成的代码。请务必查看:

IdentityServerAspNetIdentity.csproj

注意对IdentityServer4.AspNetIdentity的引用。此NuGet程序包包含IdentityServer的ASP.NET Identity集成组件。

Startup.cs

在ConfigureServices注意到必要的AddDbContext<ApplicationDbContext>和呼叫完成配置ASP.NET身份。AddIdentity<ApplicationUser, IdentityRole>:

还要注意,您在之前的快速入门中所做的大部分IdentityServer配置已经完成。该模板对客户端和资源使用内存中的样式,这些模板和资源均来自Config.cs。

最后,注意对的新调用AddAspNetIdentity<ApplicationUser>。 AddAspNetIdentity添加了集成层,以允许IdentityServer访问ASP.NET Identity用户数据库的用户数据。当IdentityServer必须将用户的声明添加到令牌中时,这是必需的。

请注意,必须在之前调用。AddIdentity<ApplicationUser, IdentityRole>AddIdentityServer

Config.cs

Config.cs包含硬编码的内存中客户端和资源定义。为了保持与先前快速入门相同的客户端和API,我们需要将配置数据从旧的IdentityServer项目复制到该项目中。现在执行此操作,然后Config.cs应如下所示:

public static class Config
{
    public static IEnumerable<IdentityResource> GetIdentityResources()
    {
        return new List<IdentityResource>
        {
            new IdentityResources.OpenId(),
            new IdentityResources.Profile(),
        };
    }

    public static IEnumerable<ApiResource> GetApis()
    {
        return new List<ApiResource>
        {
            new ApiResource("api1", "My API")
        };
    }

    public static IEnumerable<Client> GetClients()
    {
        return new List<Client>
        {
            new Client
            {
                ClientId = "client",

                // no interactive user, use the clientid/secret for authentication
                AllowedGrantTypes = GrantTypes.ClientCredentials,

                // secret for authentication
                ClientSecrets =
                {
                    new Secret("secret".Sha256())
                },

                // scopes that client has access to
                AllowedScopes = { "api1" }
            },
            // resource owner password grant client
            new Client
            {
                ClientId = "ro.client",
                AllowedGrantTypes = GrantTypes.ResourceOwnerPassword,

                ClientSecrets =
                {
                    new Secret("secret".Sha256())
                },
                AllowedScopes = { "api1" }
            },
            // OpenID Connect hybrid flow client (MVC)
            new Client
            {
                ClientId = "mvc",
                ClientName = "MVC Client",
                AllowedGrantTypes = GrantTypes.Hybrid,

                ClientSecrets =
                {
                    new Secret("secret".Sha256())
                },

                RedirectUris           = { "http://localhost:5002/signin-oidc" },
                PostLogoutRedirectUris = { "http://localhost:5002/signout-callback-oidc" },

                AllowedScopes =
                {
                    IdentityServerConstants.StandardScopes.OpenId,
                    IdentityServerConstants.StandardScopes.Profile,
                    "api1"
                },

                AllowOfflineAccess = true
            },
            // JavaScript Client
            new Client
            {
                ClientId = "js",
                ClientName = "JavaScript Client",
                AllowedGrantTypes = GrantTypes.Code,
                RequirePkce = true,
                RequireClientSecret = false,

                RedirectUris =           { "http://localhost:5003/callback.html" },
                PostLogoutRedirectUris = { "http://localhost:5003/index.html" },
                AllowedCorsOrigins =     { "http://localhost:5003" },

                AllowedScopes =
                {
                    IdentityServerConstants.StandardScopes.OpenId,
                    IdentityServerConstants.StandardScopes.Profile,
                    "api1"
                }
            }
        };
    }
}

此时,您不再需要旧的IdentityServer项目。

Program.cs文件和SeedData.cs

Program.cs中的Main比大多数ASP.NET核心项目略有不同。请注意,这是如何查找名为/ seed的命令行参数的,该参数用作将用户植入ASP.NET Identity数据库的标志。

查看SeedData该类的代码以查看如何创建数据库和创建第一个用户。

AccountController

该模板中要检查的最后一个代码是AccountController。它包含的登录和注销代码与之前的快速入门和模板略有不同。注意,使用ASP.NET Identity SignInManager<ApplicationUser>UserManager<ApplicationUser>ASP.NET Identity来验证凭据和管理身份验证会话。

其余的大部分代码与之前的快速入门和模板相同。

使用MVC客户端登录

此时,您应该运行所有现有的客户端和示例。一个例外是ResourceOwnerClient-密码需要更新为Pass123$password

启动MVC客户端应用程序,您应该能够单击“安全”链接进行登录。

image.png

您应该被重定向到ASP.NET Identity登录页面。 用您新创建的用户登录:

image.png

登录后,您会看到正常同意页面。 同意后,您将被重定向回MVC客户端应用程序,该应用程序应在其中列出用户的声明。

image.png

您还应该能够单击“使用应用程序身份调用API”来代表用户调用API:

image.png

现在,您正在使用IdentityServer中来自ASP.NET Identity的用户。

少了什么东西?

该模板中的其余大部分代码与我们提供的其他快速入门和模板相似。 您会注意到,此模板中缺少的一件事是用于用户注册,密码重置的UI代码,以及您可能希望从Visual Studio ASP.NET Identity模板获得的其他东西。

考虑到各种要求和使用ASP.NET Identity的不同方法,我们的模板特意不提供那些功能。 您应该知道ASP.NET Identity如何足够好地工作以将那些功能添加到您的项目中。 或者,您可以基于Visual Studio ASP.NET Identity模板创建一个新项目,并将在这些快速入门中了解到的IdentityServer功能添加到该项目。

Source code here

上一篇 下一篇

猜你喜欢

热点阅读