【问题标题】:HD parametrer for google oauth using dotnet core 2.0使用 dotnet core 2.0 的 google oauth 的高清参数
【发布时间】:2018-05-12 14:09:12
【问题描述】:

我想将hosting domain parameter 添加到我的 OAuth 工作流程中,以限制对我的应用程序的登录访问,但我没有找到任何有关 dotnet core 的文档。

到目前为止,这是我所做的:

services.AddAuthentication().AddGoogle(g =>
{
    g.ClientId = Configuration["google-client-id"];
    g.ClientSecret = Configuration["google-client-secret"];
    g.ClaimActions.MapJsonSubKey(PlatformKeys.GoogleAuthImageUrl, "image", "url");
});

如何将该参数添加到配置中?
这是自定义声明吗?

【问题讨论】:

    标签: c# oauth google-api .net-core


    【解决方案1】:

    我创建了一个基于 OAuthHandler 的自定义 google 处理程序

    public class CustomGoogleHandler : OAuthHandler<CustomGoogleOptions>
        {
            public CustomGoogleHandler(IOptionsMonitor<CustomGoogleOptions> options, ILoggerFactory logger, UrlEncoder encoder, ISystemClock clock)
                : base(options, logger, encoder, clock)
            {
            }
    
            protected override async Task<AuthenticationTicket> CreateTicketAsync(ClaimsIdentity identity, AuthenticationProperties properties, OAuthTokenResponse tokens)
            {
                // code omited for simplicity
            }
    
            protected override string BuildChallengeUrl(AuthenticationProperties properties, string redirectUri)
            {
                Dictionary<string, string> dictionary = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase)
                {
                    {"response_type", "code"},
                    {"client_id", Options.ClientId},
                    {"redirect_uri", redirectUri}
                };
                AddQueryString(dictionary, properties, "scope", FormatScope());
                AddQueryString(dictionary, properties, "access_type", Options.AccessType);
                AddQueryString(dictionary, properties, "hd", Options.HostedDomain);
                AddQueryString(dictionary, properties, "approval_prompt");
                AddQueryString(dictionary, properties, "prompt");
                AddQueryString(dictionary, properties, "login_hint");
                AddQueryString(dictionary, properties, "include_granted_scopes");
                string str = Options.StateDataFormat.Protect(properties);
                dictionary.Add("state", str);
                return QueryHelpers.AddQueryString(Options.AuthorizationEndpoint, dictionary);
            }
    
            private static void AddQueryString(IDictionary<string, string> queryStrings, AuthenticationProperties properties, string name, string defaultValue = null)
            {
                // code omited for simplicity
            }
        }
    

    我刚刚将自定义域属性添加到我的自定义 google 选项中,如下所示:

    public class CustomGoogleOptions : GoogleOptions
        {
    
            /// <summary>
            /// Support for HostedDomain option
            /// https://developers.google.com/identity/protocols/OpenIDConnect#hd-param
            /// </summary>
            public string HostedDomain { get; set; }        
    
        }
    

    我希望这会有所帮助!

    【讨论】:

      猜你喜欢
      • 2012-09-07
      • 2018-02-11
      • 1970-01-01
      • 2020-11-30
      • 2021-05-15
      • 2016-09-14
      • 1970-01-01
      • 2017-10-27
      • 2018-12-02
      相关资源
      最近更新 更多