Owin, pass custom query parameters in Authentication Request

↘锁芯ラ 提交于 2019-11-30 04:58:55

You're almost there! What's left is overriding built-in GoogleOAuth2AuthenticationProvider and here is the example how to do it:

class CustomGoogleAuthProvider : GoogleOAuth2AuthenticationProvider
{
    public CustomGoogleAuthProvider()
    {
        OnApplyRedirect = (GoogleOAuth2ApplyRedirectContext context) =>
        {
            IDictionary<string, string> props = context.OwinContext.Authentication.AuthenticationResponseChallenge.Properties.Dictionary;

            string newRedirectUri = context.RedirectUri;

            string[] paramertsToPassThrough = new[] { "login_hint", "hd", "anything" };

            foreach (var param in paramertsToPassThrough)
            {
                if (props.ContainsKey(param))
                {
                    newRedirectUri += string.Format("&{0}={1}", param, HttpUtility.UrlEncode(props[param]));
                }
            }

            context.Response.Redirect(newRedirectUri);
        };
    }
}

OWIN middleware registration:

app.UseGoogleAuthentication(new Microsoft.Owin.Security.Google.GoogleOAuth2AuthenticationOptions()
{
    // other config ...
    Provider = new CustomGoogleAuthProvider(),
});

The result (by the way with current version (3.0.1) of Google OAuth middleware login_hint flows from Authentication parameters out-of-the-box):

Craig Broadman

So, having struggled with a similar type of issue, brockallen sent me some code that gives me what I need using identity server 3....

class CustomGoogleAuthProvider : GoogleOAuth2AuthenticationProvider
{
    public CustomGoogleAuthProvider()
    {
        OnApplyRedirect = (GoogleOAuth2ApplyRedirectContext context) =>
        {
            var signinId = context.OwinContext.Request.Query["signin"];
            var msg = context.OwinContext.Environment.GetSignInMessage(signinId);
            var hint = msg.LoginHint;

            var newRedirectUri = context.RedirectUri;
            newRedirectUri += string.Format("&login_hint={0}", HttpUtility.UrlEncode(hint));

            context.Response.Redirect(newRedirectUri);
        };
    }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!