首页 > 解决方案 > 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 发生更改,我以后可能会遇到麻烦。

标签: json.netopenid-connectasp.net-core-3.1google-openidconnect

解决方案


我认为您可以更轻松地使用 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"]);
    }
}

推荐阅读