博客
关于我
asp.net core IdentityServer4 实现 implicit(隐式许可)实现第三方登录
阅读量:505 次
发布时间:2019-03-06

本文共 3643 字,大约阅读时间需要 12 分钟。

前言

OAuth 2.0默认四种授权模式(GrantType)

  • 授权码模式(authorization_code)

本章主要介绍简化模式(implicit)

,不通过第三方应用程序的服务器,直接在浏览器中向认证服务器申请令牌,跳过了"授权码"这个步骤,因此得名。所有步骤在浏览器中完成,令牌对访问者是可见的,且客户端不需要认证。

认证步骤

  • 客户端携带客户端标识以及重定向URI到授权服务器;
  • 用户确认是否要授权给客户端;
  • 授权服务器得到许可后,跳转到指定的重定向地址,并将令牌也包含在了里面;
  • 客户端不携带上次获取到的包含令牌的片段,去请求资源服务器;
  • 资源服务器会向浏览器返回一个脚本;
  • 浏览器会根据上一步返回的脚本,去提取在C步骤中获取到的令牌;
  • 浏览器将令牌推送给客户端。

配置认证授权服务器

Package

PM> Install-package IdentityServer4 -version 2.5.3

创建一个类Config(配置要保护的资源,和可以访问的API的客户端服务器)

public class Config    {        ///         ///     定义身份资源        ///         /// 
public static IEnumerable
GetIdentityResources() { return new List
{ new IdentityResources.OpenId(), new IdentityResources.Profile(), new IdentityResources.Email() }; } ///
/// 定义授权客户端 /// ///
public static IEnumerable
GetClients() { return new List
{ new Client{ ClientId="mvc", ClientName="MyClient", AllowedGrantTypes=GrantTypes.Implicit, RedirectUris = { "http://localhost:5003/signin-oidc" },//跳转登录到的客户端的地址 PostLogoutRedirectUris = { "http://localhost:5003/signout-callback-oidc" },//跳转登出到的客户端的地址 AllowedScopes = new List
{ IdentityServerConstants.StandardScopes.OpenId, IdentityServerConstants.StandardScopes.Profile, IdentityServerConstants.StandardScopes.Email }, RequireConsent=false } }; } }
配置Startup

再走到ConfigureServices方法注入IdentityServer4服务

services.AddIdentityServer()                .AddDeveloperSigningCredential()                .AddInMemoryIdentityResources(Config.GetIdentityResources())                .AddInMemoryClients(Config.GetClients())                .AddTestUsers(TestUsers.Users);

在Configure方法中添加IdentityServer4服务中间件

app.UseIdentityServer();

新建客户端

配置Startup

再走到ConfigureServices方法注入IdentityServer4服务

JwtSecurityTokenHandler.DefaultInboundClaimTypeMap.Clear();            services.AddAuthentication(options =>            {                options.DefaultScheme = "Cookies";                options.DefaultChallengeScheme = "oidc";            })      .AddCookie("Cookies")      .AddOpenIdConnect("oidc", options =>      {          options.Authority = "http://localhost:5004";          options.RequireHttpsMetadata = false;          options.ClientId = "mvc";          options.SaveTokens = true;          options.GetClaimsFromUserInfoEndpoint = true;      });

在Configure方法中添加认证服务中间件

app.UseAuthentication();

Run

添加第三方快捷登录(github)

在授权服务器ConfigureServices注入

直接贴代码吧

public void ConfigureServices(IServiceCollection services)        {            services.AddMvc();            services.AddIdentityServer()                .AddDeveloperSigningCredential()                .AddInMemoryIdentityResources(Config.GetIdentityResources())                .AddInMemoryClients(Config.GetClients())                .AddTestUsers(TestUsers.Users);            services.AddAuthentication().AddGitHub(options =>            {                options.SignInScheme = IdentityServerConstants.ExternalCookieAuthenticationScheme;                options.ClientId = "your client";                options.ClientSecret = "your Secret";                         });        }

Run

登录成功后可以获取到声明的ClaimsIdentity

页面大家可以通过 进行下载

,或者通过命令dotnet new -i identityserver4.templates进行下载

github 可以到

注册完应用就会有应用编码和密钥了

概要

参考:

Demo:

转载地址:http://zshbz.baihongyu.com/

你可能感兴趣的文章
LiveGBS user/save 逻辑缺陷漏洞复现(CNVD-2023-72138)
查看>>
localhost:5000在MacOS V12(蒙特利)中不可用
查看>>
logstash mysql 准实时同步到 elasticsearch
查看>>
Luogu2973:[USACO10HOL]赶小猪
查看>>
mabatis 中出现< 以及> 代表什么意思?
查看>>
Mac book pro打开docker出现The data couldn’t be read because it is missing
查看>>
MAC M1大数据0-1成神篇-25 hadoop高可用搭建
查看>>
mac mysql 进程_Mac平台下启动MySQL到完全终止MySQL----终端八步走
查看>>
Mac OS 12.0.1 如何安装柯美287打印机驱动,刷卡打印
查看>>
MangoDB4.0版本的安装与配置
查看>>
Manjaro 24.1 “Xahea” 发布!具有 KDE Plasma 6.1.5、GNOME 46 和最新的内核增强功能
查看>>
mapping文件目录生成修改
查看>>
MapReduce程序依赖的jar包
查看>>
mariadb multi-source replication(mariadb多主复制)
查看>>
MariaDB的简单使用
查看>>
MaterialForm对tab页进行隐藏
查看>>
Member var and Static var.
查看>>
memcached高速缓存学习笔记001---memcached介绍和安装以及基本使用
查看>>
memcached高速缓存学习笔记003---利用JAVA程序操作memcached crud操作
查看>>
Memcached:Node.js 高性能缓存解决方案
查看>>