我想做什么
我正在尝试实现Google OpenID Connect,作为使用Google的说明登录ASP.NET Core 3.1网站的一种方式:
https://developers.google.com/identity/protocols/oauth2/openid-connect#server-流量
根据服务器流程的第2步(向谷歌发送身份验证请求(,他们建议从他们的OpenID发现文档中检索信息:
您应该使用
authorization_endpoint
元数据值从Discovery文档中检索基本URI。
我当前正尝试通过using Newtonsoft.Json
将JSON
动态反序列化为Dictionary<string, string>
。但它给我带来了一些问题(似乎无法反序列化JSON string array
(,我正在考虑将策略更改为为为Discovery Document创建model
,并创建要反序列化的using System.Text.Json
。
现在我的问题是
谷歌的发现文档对可能导致我必须更新DiscoveryDocument.cs model
的更改的敏感程度如何
困境
使用Newtonsoft.Json
方式,即使谷歌决定删除随机密钥,一切都会正常工作。
但现在使用System.Text.Json
对我来说是一条简单的出路,它消除了对Newtonsoft库的依赖,尽管如果谷歌的发现文档发生变化,我以后可能会遇到麻烦。
我认为使用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"]);
}
}