

OAuth 2.0 是一个授权协议,它允许软件应用代表(而不是充当)资源拥有者去访问资源拥有者的资源(如何让一个系统组件获取另一个系统组件的访问权限)




grant_type | 授权方式 | 授权前置条件 | 使用通信信道 | 说明 |
|---|---|---|---|---|
authorization_code/PKCE | 授权码模式 | 授权码 | 前端/后端 | 客户端通过code在后端与授权服务器进行交互获取令牌 |
implict(不建议使用) | 简化模式 | |||
password(不建议使用) | 密码模式 | 用户名/密码 | 后端 | 在客户端输入用户名和密码,由客户端向授权服务器获取令牌 |
client_credentials | 客户端模式 | 无 | 后端 | |
device_code | 设备码 | |||
refresh_token | 刷新token | 用refresh_token来换取新的token |



地址:https://github.com/skoruba/IdentityServer4.Admin
OAuth2.0 中的 access_token 就是酒店的房卡,谁都可以拥有房卡,有房卡就可以打开酒店的门,但是房卡上并没有当前使用房卡的用户信息,如果需要知道当前房卡所有人的信息需要单独再向酒店的前台去询问

Open ID Connect 1.0 是建立在 OAuth 2.0 之上的一个身份层
https://openid.net/specs/openid-connect-core-1_0.html#ImplicitAuthorizationEndpoint
安装模板
dotnet new -i IdentityServer4.Templates查看模板
dotnet new使用模板创建
dotnet new is4inmem -n QuickStart
已成功创建模板“IdentityServer4 with In-Memory Stores and Test Users”。打开项目,启动

点击进入登录页面,使用默认用户登录

登录结果

根据配置文件通过ClientCredentials的方式获取token
// m2m client credentials flow client
new Client
{
ClientId = "m2m.client",
ClientName = "Client Credentials Client",
AllowedGrantTypes = GrantTypes.ClientCredentials,
ClientSecrets = { new Secret("511536EF-F270-4058-80CA-1C89C192F69A".Sha256()) },
AllowedScopes = { "scope1" }
},
根据配置文件通过Code的方式获取token
// interactive client using code flow + pkce
new Client
{
ClientId = "interactive",
ClientSecrets = { new Secret("49C1A7E1-0C79-4A89-A3D6-A37998FB86B0".Sha256()) },
AllowedGrantTypes = GrantTypes.Code,
RequirePkce = false,
AllowPlainTextPkce = true,
RedirectUris = { "https://localhost:44300/signin-oidc" },
FrontChannelLogoutUri = "https://localhost:44300/signout-oidc",
PostLogoutRedirectUris = { "https://localhost:44300/signout-callback-oidc" },
AllowOfflineAccess = true,
AllowedScopes = { "openid", "profile", "scope2" }
},访问认证接口获取授权码
https://localhost:5001/connect/authorize?client_id=interactive&scope=openid&response_type=code&redirect_uri=https://localhost:44300/signin-oidc&nonce=xyz返回授权码
https://localhost:44300/signin-oidc?code=BC56FE53D39BD46A5D55D43F485E23D7FF6583FEDD7A2A0B7A2A3DFDF5C52935&scope=openid&session_state=SwfB-jWoQ16C67cm5c_ANqbVE1R50Krj55GuJuArEQ0.BE30A11CD461DC430C5121AEFB4A4E82
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。