06 IdentityServer4 中支持外部标识提供器
06. IdentityServer4 External Providers .NET Core 3.1
January 24, 2020| deblokt| in category Tutorials .Net Core 3.17 comments
You can find the project [here](https://github.com/Deblokt/IdentityServer4Demos.NETCore31/tree/master/06. IdentityServer4 External Providers).
标准协议
All Identity Providers are supported using standard protocols like OpenID Connect, OAuth2, SAML2 and WS-Federation. This could be Okta, it could be Auth0, could be proprietary IdP of a client, could be another IdentityServer4. Take a look at the list of out-of-the-box extensions for “AuthenticationBuilder” for big providers like Azure AD, Microsoft Account, Google, Facebook, Twitter, etc here https://docs.microsoft.com/en-us/dotnet/api/microsoft.aspnetcore.authentication.authenticationbuilder?view=aspnetcore-2.2
所有的标识提供者都支持使用标准协议,例如 OpenID Connect,OAuth2,SAML2 以及 WS-Federation。它们可以是 Okta,也可以是 Auth0,还可以是专属于某个客户端的 IdP,甚至可以是另外一个 IdentityServer4。检查一下该开箱即用的 AuthenticationBuilder 列表,对于大型的提供者,例如 Azure AD,Microsoft Account,Goolge,Facebook,Twitter 等等。列表地址:https://docs.microsoft.com/en-us/dotnet/api/microsoft.aspnetcore.authentication.authenticationbuilder?view=aspnetcore-2.2
Setting up the usual OpenID Connect OIDC middleware is enough for most of the providers to get you going. Almost all providers nowadays provide OIDC, some as a second option alongside SAML2 and/or WS-Fed.
对于使用而言,设置通用的 OpenID Connect OIDC 中间件就足够了,几乎所有的提供者当前支持 OIDC,有些还同时支持 SAML2 和/或者 WS-Fed。
As IdentityServer4 is OIDC Identity Provider you can actually set up one IdentityServer4 instance to be an external provider for another IdentityServer4 instance using OIDC middleware. As long as there is a single root node, all Identity Servers connected this way can achieve SSO.
由于 IdentityServer4 就是一个 OIDC 的标识提供者,所以,你确实可以使用 OIDC 中间件,将一个 IdentityServerr4 设置为一个另一个 IdentityServer4 的外部提供者。通过将它作为单一的根节点,所有通过该方式连接到它的 Identity 服务器都可以实现 SSO。
Azure AD Example
I will continue from my last tutorial. Open the “Quickstart” solution in Visual Studio.
Open the “Startup.cs” in project root and navigate right above the “AddIdentityServer” service registration. Add the authentication middleware for AzureAD like so:
我们可以继续上一教程,在 Visual Studio 中打开 Quickstart 解决方法。
打开项目中的 Startup.cs 文件,找到 AddIdentityServer 服务注册的上边,针对 AzureAD 添加验证中间件的注册,如下所示:
services.AddAuthentication()
.AddOpenIdConnect(
"azuread",
"Azure AD",
options => Configuration.Bind("AzureAd", options));
services.Configure<OpenIdConnectOptions>(
"azuread",
options => {
options.Scope.Add("openid");
options.Scope.Add("profile");
options.Events = new OpenIdConnectEvents() {
OnRedirectToIdentityProviderForSignOut = context => {
context.HandleResponse();
context.Response.Redirect("/Account/Logout");
return Task.FromResult(0);
}
};
});
Now open the “appsettings.json” in project root and modify it to add the Azure AD configuration we are using and binding in “Startup” like so:
然后,打开项目中的 appsettings.json文件,修改并添加我们在 Startup 中使用的 Azure AD 配置,如下所示:
{
"ConnectionStrings": {
"DefaultConnection": "Server=(localdb)\\MSSQLLocalDB;Database=IdentityServerQuickstart.NetCore3.1;Trusted_Connection=True;MultipleActiveResultSets=true" },
"AzureAd": {
// Authority/MetadataAddress format (https://{instance}/{tenantId}/...
"Authority": "https://login.microsoftonline.com/0366c849-xxxx-xxxx-xxxx-adcc0ccf2170/oauth2/v2.0/",
"MetadataAddress": "https://login.microsoftonline.com/0366c849-xxxx-xxxx-xxxx-adcc0ccf2170/.well-known/openid-configuration",
"ClientId": "7adeb3b0-xxxx-xxxx-xxxx-a6bc5aa756da",
"CallbackPath": "/signin-oidc"
}
}
注意 您必须通过 Azure portal 获取你的
TenantId和ClientId(也被称为 ApplicationId)。这里是如何在 Azure AD 中创建应用的官方文档: https://docs.microsoft.com/en-us/azure/active-directory/develop/quickstart-register-app提示 对于应用注册,你将需要
ReturnUrl。对于本演示,该 return URL 是 http://localhost:5000/signin-oidc
Okta Example
Open the “Startup.cs” in project root and navigate right below the “AddAzureAD” and add:
打开项目根目录中的 Startup.cs ,导航到 AddAuzreAD 的下边,并添加:
.AddOpenIdConnect("okta", "Okta", options => Configuration.Bind("Okta", options));
Also add the OpenIdConnectOptions service configuration like so:
还需要添加 `OpenIdConnectOptions 服务的配置,如下所示:
services.Configure<OpenIdConnectOptions>(
"okta", options => {
options.Scope.Add("openid");
options.Scope.Add("profile");
options.Events = new OpenIdConnectEvents() {
OnRedirectToIdentityProviderForSignOut
= context => {
context.HandleResponse();
context.Response.Redirect("/Account/Logout");
return Task.FromResult(0);
}
};
});
So full code including Azure Ad and Okta looks like so:
所以,完整地包含对 Azure Ad 和 Okta 支持的代码如下所示:
services.AddAuthentication()
.AddOpenIdConnect(
"azuread",
"Azure AD",
options => Configuration.Bind("AzureAd", options)
)
.AddOpenIdConnect(
"okta",
"Okta",
options => Configuration.Bind("Okta", options)
);
services.Configure<OpenIdConnectOptions>(
"azuread",
options => {
options.Scope.Add("openid");
options.Scope.Add("profile");
options.Events = new OpenIdConnectEvents() {
OnRedirectToIdentityProviderForSignOut =
context => {
context.HandleResponse();
context.Response.Redirect("/Account/Logout");
return Task.FromResult(0);
}
};
});
services.Configure<OpenIdConnectOptions>(
"okta",
options => {
options.Scope.Add("openid");
options.Scope.Add("profile");
options.Events = new OpenIdConnectEvents() {
OnRedirectToIdentityProviderForSignOut = context => {
context.HandleResponse();
context.Response.Redirect("/Account/Logout");
return Task.FromResult(0);
}
};
});
Now open the “appsettings.json” in project root and modify it to add the Okta configuration we are using and binding in “Startup” like so:
现在,打开项目根目录中的 appsettings.json 文件,修改并添加我们在 Startup 中使用的 Okta 的配置信息。
"Okta": {
"Authority": "https://dev-xxxxxx-admin.oktapreview.com",
"ClientId": "0oakhxxxxxxxxxxaX0h7",
"CallbackPath": "/signin-oidc-okta"
}
注意 您必须从 Okta 获取你的
Authority和ClientId。这是如何创建 Okta 应用的官方文档: https://developer.okta.com/docs/guides/add-an-external-idp/microsoft/register-app-in-okta/提示 你需要为应用注册提供 ReturnUrl,对于本演示,该 return URL 是: http://localhost:5000/signin-oidc-okta
Modify the user auto-provisioning process
调整用户的自动配置处理
Because we added the “IsEnable” custom property in the previous tutorial the auto-provisioned user will by default have value “false” (disabled user) and the external provider login will fail. We need to slightly modify the automatic user creation process for external providers to set the “IsEnabled” flag to “true”. Navigate to “Quickstart/Account/ExternalController.cs” and open it.
Find the “AutoProvisionUserAsync” method and modify the line that instantiates new user. We need to modify it to set the “IsEnabled” user property to “true” like so:
由于我们在上一教程中,为自动配置用户添加了一个自定义的 IsEnable 属性,默认值为 false (禁用用户),所以外部提供者的登录会失败。我们需要略微调整对于外部提供者的自动用户创建处理,将该 IsEnabled 标志设置为 true。找到 “Quickstart/Account/ExternalController.cs” 然后打开它。
var user = new ApplicationUser{
UserName = Guid.NewGuid().ToString(),
Email = email,
IsEnabled = true
};
Now run the IdentityServer4 and try to sign in with Azure AD or Okta. If the local user exists with the same username or email as the external user (from Azure AD or Okta in our example) the matching process will link the external user with local user and the new local user will not be created. For other scenarios (no match) the auto-provisioning process will create a new local user and link it with the external user. I logged in using Okta and the new local user was auto-provisioned. Notice that my name was automatically populated from the claims provided by Okta. These are the claims of the external user now set to the local user.
现在,运行 IdentityServer4 ,然后尝试通过 Azure AD 或者 okta 登录。如果对于外部用户 (来自 Azure Ad 或者 Okta) 存在有相同的用户名或者电子邮件的本地用户,匹配过程将链接外部用户到本地用户,且不会创建新的本地用户。对于其它场景 (不匹配的用户),自动匹配过程将创建一个新的本地用户并链接到外部用户。我使用 Okta 用户登录,自动匹配过程将会自动处理。注意,我的名字将会自动从来自 Okta 提供的声明中填充。来自外部用户的声明现在设置到本地用户。

很简单
Now that was super easy, wasn’t it? Adding any standard Identity Provider shouldn’t pose any challenge as the method is pretty much the same. In my next tutorial I will start tackling one of the important features which are Multi-Factor Authentication MFA aka 2FA if there are two factors. Stay fresh!
You can find the project [here](https://github.com/Deblokt/IdentityServer4Demos.NETCore31/tree/master/06. IdentityServer4 External Providers).
超级简单,是不是?添加任何标准的标识提供者的难度不会超过这些方法。在下一教程中,我将开始另一个重要的特性:多因子验证 MFA,如果只有两个因子,也被称为 2FA。
Support
For direct assistance schedule a technical meeting with Ivan to talk about your requirements. For a general overview of our services and a live demo schedule a meeting with Maja.
https://deblokt.com/2020/01/24/06-identityserver4-external-providers-net-core-3-1/
06 IdentityServer4 中支持外部标识提供器的更多相关文章
- Solr 06 - Solr中配置使用IK分词器 (配置schema.xml)
目录 1 配置中文分词器 1.1 准备IK中文分词器 1.2 配置schema.xml文件 1.3 重启Tomcat并测试 2 配置业务域 2.1 准备商品数据 2.2 配置商品业务域 2.3 配置s ...
- [译]在Asp.Net Core 中使用外部登陆(google、微博...)
原文出自Rui Figueiredo的博文<External Login Providers in ASP.NET Core> 摘要:本文主要介绍了使用外部登陆提供程序登陆的流程,以及身份 ...
- ASP.NET Core 使用外部登陆提供程序登陆的流程,以及身份认证的流程 (转载)
阅读目录 在Asp.Net Core 中使用外部登陆(google.微博...) 中间件管道 The Authentication Middleware The Challenge 与认证中间件进行交 ...
- Net Core 使用外部登陆提供程序登陆的流程,以及身份认证的流程
在Asp.Net Core 中使用外部登陆(google.微博...) 原文出自Rui Figueiredo的博文<External Login Providers in ASP.NET C ...
- 【译】在Asp.Net Core 中使用外部登陆(google、微博...)
原文出自Rui Figueiredo的博文<External Login Providers in ASP.NET Core> (本文很长) 摘要:本文主要介绍了使用外部登陆提供程序登陆的 ...
- <Android基础> (七)内容提供器
第七章 内容提供器 7.1 内容提供器(Content Provider) 主要应用于在不同的应用程序之间实现数据共享功能.允许一个程序访问另一个程序中的数据,同时还能保证被访数据的安全性. 7.2 ...
- Laravel 服务容器、服务提供器、契约实例讲解
前言 刚开始看laravel服务容器.契约.服务提供器的确生涩难懂,不单单是概念繁多,而且实际的demo很难找(找是找到了,但难用啊),最后就隔一段时间看一遍,大概个十来遍,还真给看出个门道, ...
- 入职小白随笔之Android四大组件——内容提供器详解(Content Provider)
Content Provider 内容提供器简介 内容提供器(Content Provider)主要用于在不同的应用程序之间 实现数据共享的功能,它提供了一套完整的机制,允许一个程序访问另一个程序中的 ...
- IdentityServer4 中文文档 -12- (快速入门)添加外部认证支持
IdentityServer4 中文文档 -12- (快速入门)添加外部认证支持 原文:http://docs.identityserver.io/en/release/quickstarts/4_e ...
- 第22章 使用外部身份提供商登录 - Identity Server 4 中文文档(v1.0.0)
ASP.NET Core有一种灵活的方式来处理外部身份验证.这涉及几个步骤. 注意 如果您使用的是ASP.NET Identity,则会隐藏许多基础技术细节.建议您还阅读Microsoft 文档并执行 ...
随机推荐
- std::variant快速上手
std::variant 是 C++17 引入的一种类型安全的联合体,用来存储多个可能类型中的一种值,且保证使用时的类型安全.相比于传统的 union,std::variant 不仅能够存储不同类型的 ...
- Android应用启动全流程分析(源码深度剖析)
目录 1.前言 2.大纲 3. Input触控事件处理流程 3.1 系统机制分析 3.2 结合Systrace分析 4. 应用进程的创建与启动 4.2 创建应用进程 4.2.1 AMS 发送socke ...
- 如何实现高效运维?来谈谈性能优化那些事(含直播回顾 Q&A)
数据库性能问题,常常是困扰DBA高效运维的难题之一.如何多角度地帮助DBA,找到"数据库慢"的原因,保证系统高效.稳定.安全地运行? 2021年10月14日,云和恩墨技术顾问,拥有 ...
- 怎么使用云桌面(云电脑)?ToDesk新手入门教程
在当今数字化时代,个人用户对于电脑性能的需求日益提升,而云电脑(又可称为云桌面)作为一种新型的电脑配备模式,正在逐渐进入人们的视野. 对于很多新手来说,可能是第一次接触到云电脑软件,今天小社长就以To ...
- 学习JavaScript第五天
文章目录 1.HTML DOM 1.1 表单相关元素 ① form 元素 ② 文本输入框类和文本域(input 和 textarea) ③ select 元素 1.2 表格相关元素 ① table 元 ...
- CSS动画(动态导航栏)
1.项目简介 一个具有创意的导航菜单不仅能为你的大作业增色,还能展示你的技术实力.本文将分享一系列常用于期末大作业的CSS动画导航效果,这些效果不仅外观酷炫,而且易于实现.我们提供了一键复制的代码,让 ...
- DRF-Authention组件源码分析及扩展
drf 认证组件 1.认证组件源码执行流程 在该图中,我把与认证组件无关的代码都删除了,只留下了认证的代码,方便解析.每行注释的开头数字即代表了执行顺序 注意事项: 第5步中的self.authent ...
- Java EasyExcel 导出报内存溢出如何解决
大家好,我是 V 哥.使用EasyExcel进行大数据量导出时容易导致内存溢出,特别是在导出百万级别的数据时.你有遇到过这种情况吗,以下是V 哥整理的解决该问题的一些常见方法,分享给大家,欢迎一起讨论 ...
- npm link的作用——避免频繁发布更新
web-components 里面的组件库 修改频繁 可以使用link 创建链接,引用放就不需要每次都重新发布重新安装更新了 功能 在本地开发npm模块的时候,我们可以使用npm link命令,将n ...
- 关于template标签用法总结(含vue中的用法总结)
一.html5中的template标签html中的template标签中的内容在页面中不会显示.但是在后台查看页面DOM结构存在template标签.这是因为template标签天生不可见,它设置了d ...