这几天学习IdentityServer4,感觉内容有点乱,也可能自己水平有限吧。但为了巩固学习的内容,也打算自己理一下思路。

首先IdentityServer解决什么问题?

下图是我们的一个程序的组织形式

详情可以看看官网的描述:https://identityserver4.readthedocs.io/en/latest/intro/big_picture.html
我的理解是:IdentityServer就是解决多点登录及API授权、WEB授权的问题

第一个例子

我们将重现官网上的第一个范例来学习相关概念,但与官网的不同,我打算一开始就将服务端从一个MVC网站开始。官网的第一个范例:https://identityserver4.readthedocs.io/en/latest/quickstarts/1_client_credentials.html

下面截图和代码来自VS.NET2019+asp.net core 3.0

新建服务端

新增asp.net core Web应用程序,项目名称IdentityMvc。因为还要后面加上测试的客户端,所以解决方案我使用了另外的一个名称OpenIdConnect

利用nuget添加(安装)引用

IdentityServer4

将端口修改一下,授权服务的端口我们使用44300。打开Properties\launchSettings.json文件

新增Config.cs文件

using IdentityServer4.Models;
using System.Collections.Generic; namespace IdentityMvc
{
public static class Config
{
public static IEnumerable<IdentityResource> GetIdentityResources()
{
return new IdentityResource[]
{
new IdentityResources.OpenId()
};
} 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" }
}
};
}
}
}

  

修改startup.cs文件

在ConfigureServices(IServiceCollection services)文件添加以下代码

var builder = services.AddIdentityServer()
.AddInMemoryIdentityResources(Config.GetIdentityResources())
.AddInMemoryApiResources(Config.GetApiResources())
.AddInMemoryClients(Config.GetClients());

  

在Configure(IApplicationBuilder app, IWebHostEnvironment env)方法,添加app.UseIdentityServer();

public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseExceptionHandler("/Home/Error");
// The default HSTS value is 30 days. You may want to change thi
app.UseHsts();
}
app.UseIdentityServer();//添加这一句 app.UseHttpsRedirection();
app.UseStaticFiles();
//...省略下方代码

  

至此,保护API的服务端就做好了。我们可以点击调试运行,IDE会打开IE并访问home页。home页一般能正常打开,但如何测试授权服务是否正常呢,可以在地址栏添加.well-known/openid-configuration,应能看到类似的内容

上图的地址的端口可能会有所不同。如果openid-configuration页面看到是空白的话,估计我们少加入了app.UseIdentityServer()方法。

好了,授权服务端就这样的了。接着就是需要一个API的服务程序,和一个调用API的客户端。

IdentityServer4入门一的更多相关文章

  1. IdentityServer4入门二

    在 IdentityServer4入门一 我们准备好了一个认证的服务端,这里做一个需要保护的API服务 首先,向解决方案新增一个项目.我们同样使用入门一的方式新增一个asp.net core Web程 ...

  2. IdentityServer4入门三:授权模式

    在入门一.入门二我们实现了一个完整的API保护的过程.需要保护的API只需在其Controler上应用[Authorize]特性,来显式指定受保护的资源.而我们实现的这个例子,所应用的模式叫“Clie ...

  3. Asp.net Core IdentityServer4 入门教程(一):概念解析

    目录 1.IdentityServer4 是什么 2.什么是OpenID和OAuth 2.0协议 3.IdentityServer4 可以用来做什么 其他 1.IdentityServer4 是什么 ...

  4. IdentityServer4入门五:错误处理

    在访问ClientMvc的保护页面时,会跳转到IdentityMvc页面,这时会出现类似下图的错误界面,让人无从入手. 如果你尝试按文字所说的内容去处理.你发现项目已正确设置.其实上面的内容是固定的, ...

  5. IdentityServer4入门四:应用Implicit模式保护网站(下)

    为认证服务端增加数据库支持 我计划使用一个名为Admin的表,放在一个已有的数据库里.所以我需要定义Admin类和在配置里预先加上数据库连接 新增类:Admin.cs public class Adm ...

  6. IdentityServer4入门四:应用Implicit模式保护网站(上)

    我们先新增一个网站,名为“ClientMvc",也是asp.net core Web应用程序(模型视图控制器) 使用nuget安装以下引用 Microsoft.AspNetCore.Auth ...

  7. IdentityServer4 中文文档 -16- (快速入门)使用 EntityFramework Core 存储配置数据

    IdentityServer4 中文文档 -16- (快速入门)使用 EntityFramework Core 存储配置数据 原文:http://docs.identityserver.io/en/r ...

  8. IdentityServer4 中文文档 -15- (快速入门)添加 JavaScript 客户端

    IdentityServer4 中文文档 -15- (快速入门)添加 JavaScript 客户端 原文:http://docs.identityserver.io/en/release/quicks ...

  9. IdentityServer4 中文文档 -14- (快速入门)使用 ASP.NET Core Identity

    IdentityServer4 中文文档 -14- (快速入门)使用 ASP.NET Core Identity 原文:http://docs.identityserver.io/en/release ...

随机推荐

  1. bat实现监控进程守护程序-保证平台服务的稳定执行

    背景是平台所在的服务器经常因异常原因导致当前机器所在的服务关闭....直接贴代码吧. ********************************************************* ...

  2. mongoose整理笔记

    一:参考学习网址 npm: https://www.npmjs.com/package/mongoose 官网API:http://mongoosejs.com/docs/guide.html 二:在 ...

  3. Cryptography -- 密码学

    Introduction to Cryptography Cryptography enables you to store sensitive information or transmit it ...

  4. 自定义centos

    目录 自定义centos 1. 为什么要自定义centos 2. 自定义centos步骤 自定义centos 1. 为什么要自定义centos 在使用官网的 centos镜像,只有200m,很小,但是 ...

  5. leetcode-21.合并有序链表 · List

    题面 合并两个排序链表. 算法 创建结果链表头*res,*p指向头,当两个链表节点都不为空时,比较节点值,值小的挂在p后面,二者(p和小者)顺次后移.知道某条链表空,跳出while循环.接着,直接将不 ...

  6. linux文件系统初学

    Linux磁盘分区和目录 Linux发行版之间的差别很小,差别主要表现在系统管理的特色工具以及软件包管理方式的不同. Windows的文件结构是多个并列的树状结构,最顶部是不同的磁盘(分区),如C,D ...

  7. swoole httpserver学习

    文件 HttpServer.php <?php /** * Created by PhpStorm. * User: mac * Date: 2019/9/13 * Time: 21:00 */ ...

  8. java入门学习总结_03

    1.键盘录入 2.分支结构 键盘录入 概述 1.键盘录入:在程序运行的过程中,可以让用户录入一些数据,存储在内存的变量中,在后续的程序运行过程中,可以使用这些数据. 2.步骤: 第一步:导包,在类声明 ...

  9. computed和watch的使用场景

    转载地址:https://blog.csdn.net/yuwenshi12/article/details/78561372 从作用机制和性质上看待methods,watch和computed的关系 ...

  10. 第一课 IP通信

       我们的专业课:<IP通信>开课了.    在第一节课,我们初步了解了关于通信的知识,涨了知识,下面我就说一下第一节课所学所感.    在学习这门课的时候,需要我们认真预习,认真听课, ...