json.net - Google OpenID 发现文档的更改有多敏感?
问题描述
我想要做什么
我正在尝试使用 Google 的说明实现Google OpenID Connect作为登录ASP.NET Core 3.1网站的一种方式:
https://developers.google.com/identity/protocols/oauth2/openid-connect#server-flow
在服务器流程的第 2 步(向 Google 发送身份验证请求)下,他们建议从其OpenID 发现文档中检索信息:
您应该使用元数据值从发现文档中检索基本 URI 。
authorization_endpoint
我目前正在尝试动态反序列化JSON
到 a Dictionary<string, string>
by using Newtonsoft.Json
。但这给了我一些问题(似乎无法反序列化 a JSON string array
),我正在考虑改变我的策略,model
为发现文档创建 a 并using System.Text.Json
反序列化。
现在我的问题是
Google 的发现文档对导致我必须更新我的DiscoveryDocument.cs model
.
困境
Newtonsoft.Json
即使谷歌决定删除一个随机密钥,一切仍然可以正常工作。
但是System.Text.Json
现在使用 对我来说是一种简单的方法,并且消除了对 Newtonsoft 库的依赖,但如果 Google 的 Discovery Document 发生更改,我以后可能会遇到麻烦。
解决方案
我认为您可以更轻松地使用 Microsoft.IdentityModel.Protocols 和 Microsoft.IdentityModel.Protocols.OpenIdConnect NuGet 包,并使用包含的解析器为您完成所有工作。文档中的项目非常标准化,但并非每个提供商都提供所有项目。
public class OpenIDSettings : IOpenIDSettings
{
public string Issuer { get; }
public string jwks_uri { get; }
public string authorization_endpoint { get; }
public string token_endpoint { get; }
public string userinfo_endpoint { get; }
public string end_session_endpoint { get; }
public string check_session_iframe { get; }
public string revocation_endpoint { get; }
public string introspection_endpoint { get; }
public string device_authorization_endpoint { get; }
public ICollection<string> scopes_supported { get; }
public ICollection<string> claims_supported { get; }
public OpenIDSettings(string endpoint)
{
var configurationManager = new ConfigurationManager<OpenIdConnectConfiguration>(
$"{endpoint}/.well-known/openid-configuration",
new OpenIdConnectConfigurationRetriever());
//If you get an exception here, then provider is not running or reachable
var document = configurationManager.GetConfigurationAsync().Result;
//Add the necessary code to populate the properties in this class
Issuer = document.Issuer;
jwks_uri = document.JwksUri;
authorization_endpoint = document.AuthorizationEndpoint;
token_endpoint = document.TokenEndpoint;
userinfo_endpoint = document.UserInfoEndpoint;
end_session_endpoint = document.EndSessionEndpoint;
check_session_iframe = document.CheckSessionIframe;
scopes_supported = document.ScopesSupported;
claims_supported = document.ClaimsSupported;
if (document.AdditionalData.ContainsKey("revocation_endpoint"))
revocation_endpoint = (string)(document.AdditionalData["revocation_endpoint"]);
if (document.AdditionalData.ContainsKey("introspection_endpoint"))
introspection_endpoint = (string)(document.AdditionalData["introspection_endpoint"]);
if (document.AdditionalData.ContainsKey("device_authorization_endpoint"))
device_authorization_endpoint = (string)(document.AdditionalData["device_authorization_endpoint"]);
}
}
推荐阅读
- node.js - 如何将函数结果带入另一个函数
- node.js - NodeJS SyntaxError:JSON 中位置 0 的意外标记
- discord - discord.py-rewrite - 通过 cogs 处理异常
- azure - 带有 AzureADB2C 的 SaaS 应用程序:客户端 SSO 的用户流?
- sails.js - How to fix error caused by userconfig probably by sails?
- bash - How can I invoke both BASH and CSH shells in a same script
- php - 使用php从MySQL中选择并打印数据,不起作用
- c# - 具有 2 个时间数据的 LinkedList
- html - CSS文件未在浏览器中更新
- android - Android资产,我如何从子文件夹中读取文件?