第16章 使用ASP.NET Core Identity - Identity Server 4 中文文档(v1.0.0)
注意
对于任何先决条件(例如模板),首先要查看概述。
IdentityServer旨在提供灵活性,其中一部分允许您为用户及其数据(包括账户密码)使用所需的任何数据库。如果您从新的用户数据库开始,那么ASP.NET Identity是您可以选择的一个选项。本快速入门显示了如何在IdentityServer中使用ASP.NET Identity。
本快速入门使用ASP.NET Identity的方法是为IdentityServer Host创建一个新项目。这个新项目将取代我们在之前的快速入门中构建的先前IdentityServer项目。这个新项目的原因是由于使用ASP.NET身份时UI资产的差异(主要是登录和注销的差异)。此解决方案中的所有其他项目(针对客户端和API)将保持不变。
注意
本快速入门假设您熟悉ASP.NET Identity的工作原理。如果不是,建议您先了解它。
16.1 ASP.NET身份的新项目
第一步是为您的解决方案添加ASP.NET Identity的新项目。我们提供了一个模板,其中包含ASP.NET Identity与IdentityServer所需的最小UI资产。您最终将删除IdentityServer的旧项目,但是您需要迁移一些项目。
首先创建一个将使用ASP.NET Identity的新IdentityServer项目:
cd quickstart/src
dotnet new is4aspid -n IdentityServerAspNetIdentity
当提示“seed”用户数据库时,选择“Y”表示“是”。这将使用我们的“alice”和“bob”用户填充用户数据库。他们的密码是“Pass123$”。
注意
该模板使用Sqlite作为用户的数据库,并在模板中预先创建EF迁移。如果您希望使用其他数据库提供程序,则需要更改代码中使用的提供程序并重新创建EF迁移。
16.2 检查新项目
在您选择的编辑器中打开新项目,并检查生成的代码。一定要看看:
16.2.1 IdentityServerAspNetIdentity.csproj
请注意对IdentityServer4.AspNetIdentity的引用。此NuGet包中包含IdentityServer的ASP.NET Identity集成组件。
16.2.2 Startup.cs
在ConfigureServices中,注意必要的AddDbContext<ApplicationDbContext>和调用以配置ASP.NET Identity。AddIdentity<ApplicationUser, IdentityRole>
另请注意,您在之前的快速入门中完成的大部分相同IdentityServer配置已经完成。模板使用内存中的样式用于客户端和资源,这些样式来自Config.cs。
最后,注意添加新的AddAspNetIdentity<ApplicationUser>。 AddAspNetIdentity添加集成层以允许IdentityServer访问ASP.NET Identity用户数据库的用户数据。当IdentityServer必须将用户的声明添加到令牌时,这是必需的。
16.2.3 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项目。
16.2.4 Program.cs文件和SeedData.cs
Program.cs中的Main比大多数ASP.NET Core项目略有不同。请注意这是如何查找名为/ seed的命令行参数,该参数用作为ASP.NET Identity数据库中的用户设定种子的标志。
查看SeedData类的代码,了解如何创建数据库以及创建第一个用户。
16.2.5 AccountController
要在此模板中检查的最后一个代码是AccountController。这包含与之前的快速入门和模板略有不同的登录和注销代码。请注意使用ASP.NET Identity SignInManager<ApplicationUser>和UserManager<ApplicationUser>来自ASP.NET Identity来验证凭据和管理身份验证会话。
其余大部分代码与之前的快速入门和模板相同。
16.3 使用MVC客户端登录
此时,您应该运行所有现有客户端和模板。一个例外是ResourceOwnerClient -密码将需要更新,更新password为Pass123$。
启动MVC客户端应用程序,您应该能够单击“安全”链接以登录。
您应该被重定向到ASP.NET Identity登录页面。使用新创建的用户登录:
登录后,您会看到正常的同意页面。获得同意后,您将被重定向回MVC客户端应用程序,其中应列出您的用户声明。
您还应该能够单击“使用应用程序标识调用API”来代表用户调用API:
现在,您正在使用IdentityServer中的ASP.NET身份用户。
16.4 少了什么东西?
此模板中的大部分代码与我们提供的其他快速入门和模板类似。您将注意到此模板中缺少的一件事是用户注册,密码重置以及您可能期望从Visual Studio ASP.NET Identity模板中获得的其他内容的UI代码。
鉴于需求的多样性和使用ASP.NET Identity的不同方法,我们的模板明确地不提供这些功能。您应该知道ASP.NET Identity如何充分地将这些功能添加到项目中。或者,您可以基于Visual Studio ASP.NET Identity模板创建新项目,并将您在这些快速入门中了解到的IdentityServer功能添加到该项目中。
第16章 使用ASP.NET Core Identity - Identity Server 4 中文文档(v1.0.0)的更多相关文章
- Identity Server 4 中文文档(v1.0.0) 目录
欢迎来到IdentityServer4 第一部分 简介 第1章 背景 第2章 术语 第3章 支持和规范 第4章 打包和构建 第5章 支持和咨询选项 第6章 演示服务器和测试 第7章 贡献 第二部分 快 ...
- 第15章 使用EntityFramework Core进行配置和操作数据 - Identity Server 4 中文文档(v1.0.0)
IdentityServer旨在实现可扩展性,其中一个可扩展点是用于IdentityServer所需数据的存储机制.本快速入门展示了如何配置IdentityServer以使用EntityFramewo ...
- 第66章 视频 - Identity Server 4 中文文档(v1.0.0)
第66章 视频 66.1 2019 January [NDC] - 使用ASP.NET Core 2.2和3.0保护Web应用程序和API 1月[NDC] - 为基于OpenID Connect / ...
- 第65章 博客帖子 - Identity Server 4 中文文档(v1.0.0)
第65章 博客帖子 65.1 团队帖子 65.1.1 2019 IdentityServer中的范围和声明设计 尝试使用IdentityServer4的设备流程 OAuth2中隐含流的状态 另一种保护 ...
- 第64章 学习 - Identity Server 4 中文文档(v1.0.0)
以下是一些在线,远程和课堂培训选项,以了解有关ASP.NET Core Identity和IdentityServer4的更多信息. 64.1 现代应用程序的身份和访问控制(使用ASP.NET Cor ...
- 第41章 CORS - Identity Server 4 中文文档(v1.0.0)
第41章 CORS IdentityServer中的许多端点将通过基于JavaScript的客户端的Ajax调用进行访问.鉴于IdentityServer最有可能托管在与这些客户端不同的源上,这意味着 ...
- 第25章 退出外部身份提供商 - Identity Server 4 中文文档(v1.0.0)
当用户注销 IdentityServer并且他们使用外部身份提供程序登录时,可能会将其重定向到注销外部提供程序.并非所有外部提供商都支持注销,因为它取决于它们支持的协议和功能. 要检测是否必须将用户重 ...
- 第22章 使用外部身份提供商登录 - Identity Server 4 中文文档(v1.0.0)
ASP.NET Core有一种灵活的方式来处理外部身份验证.这涉及几个步骤. 注意 如果您使用的是ASP.NET Identity,则会隐藏许多基础技术细节.建议您还阅读Microsoft 文档并执行 ...
- 第21章 登录 - Identity Server 4 中文文档(v1.0.0)
为了使IdentityServer能够代表用户发出令牌,该用户必须登录IdentityServer. 21.1 Cookie身份验证 使用由ASP.NET Core中的cookie身份验证处理程序管理 ...
随机推荐
- Jquery weui picker 支持label和value
万年没更新了. 最近用jquery weui. 在使用picker时需要一些问题. 就是让picker 显示label, 但是取值的时候取value用于存储. 官网例子如下 Jquery-weui 官 ...
- npm install命令详解
-S,–save 安装包信息将加到dependencies(生产阶段的依赖) npm install --save 或 npm install -S -D, –save-dev 安装包信息将加到dev ...
- react-native 打包成apk 文件
用android studio 打包成apk 文件 js build 执行: react-native bundle --platform android --dev false --entry-fi ...
- xgboost 多gpu支持 编译
xgboost 多gpu支持 编译 Ubuntu 18.04.2Linux 4.15.0-46-genericgcc (Ubuntu 7.3.0-27ubuntu1~18.04) 7.3.0 cuda ...
- [Swift]LeetCode493. 翻转对 | Reverse Pairs
Given an array nums, we call (i, j) an important reverse pair if i < j and nums[i] > 2*nums[j] ...
- [Swift]LeetCode951. 翻转等价二叉树 | Flip Equivalent Binary Trees
For a binary tree T, we can define a flip operation as follows: choose any node, and swap the left a ...
- 两分钟搞懂UiAutomator、UiAutomator2、Bootstrap的关系
很多同学经过一段时间的学习之后都明白了Appium的基本原理,但是越学习到后面发现出现的很多陌生名词无法弄清楚其具体作用,今天这篇文章的目的就是为了让大家来弄懂三个高频名词:UiAutomator.U ...
- nfs服务启动失败:Failed to start NFS status monitor for NFSv2/3 locking..
今天碰到个问题,服务器重启后,nfs服务就启动不了了,关闭都关不了.查看系统日志报下面的错: Aug 10 17:08:53 prod-r3-slt-s-01 systemd: Started Pre ...
- 网络协议 11 - Socket 编程(下):眼见为实耳听为虚
系列文章传送门: 网络协议 1 - 概述 网络协议 2 - IP 是怎么来,又是怎么没的? 网络协议 3 - 从物理层到 MAC 层 网络协议 4 - 交换机与 VLAN:办公室太复杂,我要回学校 网 ...
- json对象和json字符串
Javascript字符串与JSON字符串的最大区别在于,JSON字符串必须使用双引号(单引号会导致语法错误) 与Javascript的对象字面量相比,JSON对象有两个地方不一样.首先,没有声明变量 ...