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 获取你的 TenantIdClientId (也被称为 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 获取你的 AuthorityClientId 。这是如何创建 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 中支持外部标识提供器的更多相关文章

  1. 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 ...

  2. [译]在Asp.Net Core 中使用外部登陆(google、微博...)

    原文出自Rui Figueiredo的博文<External Login Providers in ASP.NET Core> 摘要:本文主要介绍了使用外部登陆提供程序登陆的流程,以及身份 ...

  3. ASP.NET Core 使用外部登陆提供程序登陆的流程,以及身份认证的流程 (转载)

    阅读目录 在Asp.Net Core 中使用外部登陆(google.微博...) 中间件管道 The Authentication Middleware The Challenge 与认证中间件进行交 ...

  4. Net Core 使用外部登陆提供程序登陆的流程,以及身份认证的流程

    在Asp.Net Core 中使用外部登陆(google.微博...)   原文出自Rui Figueiredo的博文<External Login Providers in ASP.NET C ...

  5. 【译】在Asp.Net Core 中使用外部登陆(google、微博...)

    原文出自Rui Figueiredo的博文<External Login Providers in ASP.NET Core> (本文很长) 摘要:本文主要介绍了使用外部登陆提供程序登陆的 ...

  6. <Android基础> (七)内容提供器

    第七章 内容提供器 7.1 内容提供器(Content Provider) 主要应用于在不同的应用程序之间实现数据共享功能.允许一个程序访问另一个程序中的数据,同时还能保证被访数据的安全性. 7.2 ...

  7. Laravel 服务容器、服务提供器、契约实例讲解

        前言 刚开始看laravel服务容器.契约.服务提供器的确生涩难懂,不单单是概念繁多,而且实际的demo很难找(找是找到了,但难用啊),最后就隔一段时间看一遍,大概个十来遍,还真给看出个门道, ...

  8. 入职小白随笔之Android四大组件——内容提供器详解(Content Provider)

    Content Provider 内容提供器简介 内容提供器(Content Provider)主要用于在不同的应用程序之间 实现数据共享的功能,它提供了一套完整的机制,允许一个程序访问另一个程序中的 ...

  9. IdentityServer4 中文文档 -12- (快速入门)添加外部认证支持

    IdentityServer4 中文文档 -12- (快速入门)添加外部认证支持 原文:http://docs.identityserver.io/en/release/quickstarts/4_e ...

  10. 第22章 使用外部身份提供商登录 - Identity Server 4 中文文档(v1.0.0)

    ASP.NET Core有一种灵活的方式来处理外部身份验证.这涉及几个步骤. 注意 如果您使用的是ASP.NET Identity,则会隐藏许多基础技术细节.建议您还阅读Microsoft 文档并执行 ...

随机推荐

  1. 记录JDK8到JDK17各个版本的更新重点

    虽然大多数公司还是用的JDK8但是也要去了解和学习一下新得东西 ## JDK8新特性(2014年初)(LTS版本) 1.Lambda表达式 2.函数式编程 3.接口可以添加默认方法和静态方法,也就是定 ...

  2. kali安装和升级

    实验介绍: kali集成了世界上所有优秀的渗透测试工具 一:在VMware上安装 这里只详细介绍kali在VMware的安装,u盘和物理机上的安装不做详解 在kali官网下载kali镜像iso文件 下 ...

  3. Go语言对接微信支付与退款全流程指南

    目录: 一.准备工作 二.初始化微信支付客户端 三.实现支付功能 1. 付款时序图 2. 实现不同场景下的支付 WAP端支付 PC端支付 Android端支付 3. 解析支付回调 四.实现退款功能 退 ...

  4. Serilog文档翻译系列(七) - 应用设置、调试和诊断、开发接收器

    01.应用设置 Serilog 支持在 App.config 和 Web.config 文件中使用简单的 配置语法,以设置最低日志级别.为事件添加额外属性以及控制日志输出. Serilog 主要通过代 ...

  5. java工具篇-IDEA

    java的开发离不开好的开发工具,这就需要了解集成开发工具idea 背景黑白风格 设置方法File–>settings–>Appearance & Behavior–>App ...

  6. 从Windows 11 23H2升级至24H2后,Git操作提示文件所有权错误的3种有效解决方案

    从Windows 11 23H2升级至24H2后,Git操作提示文件所有权错误的3种有效解决方案 在升级至 Windows 11 24H2 后,使用 git add 等命令时,可能会遇到如下错误提示: ...

  7. 这十年我与广告不共戴天练就的十八般武艺 #PC去广告 #手机去广告

    背景 大家应该都体会过广告的苦恼,比如看着好看的电视,突然给播放广告,这时候痛苦系数飙升.随着社会进步,广告的载体,还有形式也越来越多,比如手机端各种APP启动广告,PC端软件弹窗,网站Banner等 ...

  8. jmeter使用beanshell完成签名计算,附与python代码对比

    签名计算过程: 1.ticket计算:时间戳加+随机数字拼接后md5加密 2.组装公共参数+ticket+时间戳+业务参数 beanshell代码实现: import java.util.*;impo ...

  9. 多元/多维高斯/正态分布概率密度函数推导 (Derivation of the Multivariate/Multidimensional Normal/Gaussian Density)

    各种维度正态分布公式: 一维正态分布 二维正态分布/多维正态分布 各向同性正态分布 注:即方差都是一样的,均值不一样,方差的值可以单独用标量表示. 多元/多维高斯/正态分布概率密度函数推导 (Deri ...

  10. 怎样在Linux 环境 (红帽 rhel 7.3) 安装 Python 3

    自己装的虚拟机(红帽 7),默认安装的python2.7,更新为python 3.8  自己做个记录,方便日后查看 注意:红帽的yum 需要注册才能使用,必须要替换yum,替换方法请参见:怎样替换 r ...