【问题标题】:Owin, pass custom query parameters in Authentication RequestOwin,在Authentication Request中传递自定义查询参数
【发布时间】:2014-09-17 09:24:43
【问题描述】:

我们有自己的 OpenID Connect Provider。我们想使用 Owin 中间件在身份验证请求中传递自定义查询参数。而且我们找不到如何使用 Microsoft.Owin.Security.OpenIdConnect 程序集来实现这一点。甚至我们也找不到如何在 Authentication Request 中添加标准请求参数(例如“login_hint 参数”)。

例如,Google 有“login_hint”和“hd”参数(https://developers.google.com/accounts/docs/OAuth2Login#sendauthrequest),我们希望有几乎相同的参数。但我们甚至找不到如何使用 Owin 将这些参数发送给 Google。试过这段代码:

var googleOptions = new GoogleOAuth2AuthenticationOptions()
{
    ClientId = "...",
    ClientSecret = "...",
};
app.UseGoogleAuthentication(googleOptions);

...

public ActionResult ExternalLogin(string provider)
{
    var ctx = Request.GetOwinContext();
    var properties = new AuthenticationProperties();
    properties.Dictionary.Add("login_hint ", "myemail@gmail.com");
    properties.Dictionary.Add("hd", "hd");
    ctx.Authentication.Challenge(properties, provider);
    return new HttpUnauthorizedResult();
}

但是会生成没有“login_hint”和“hd”参数的认证请求url。

非常感谢您提供解决此问题的任何帮助。

【问题讨论】:

  • 您的密钥末尾有空格是否有原因,因为 login_hint 末尾有空格?如果没有空格,我似乎无法将其添加到ctx.Authentication.Challenge,但该空格永远不会被解析出来,它会创建一个带有空格的重定向,我认为这不起作用。

标签: c# asp.net-mvc owin katana openid-connect


【解决方案1】:

你快到了!剩下的是覆盖内置的GoogleOAuth2AuthenticationProvider,下面是如何做到这一点的示例:

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 中间件注册:

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

结果(顺便说一下,Google OAuth 中间件 login_hint 的当前版本 (3.0.1) 来自现成的身份验证参数):

【讨论】:

  • 是否有等效的覆盖将 login_hint 传递给 Azure B2C?
  • @Brendan,不知道这个,建议看看你正在使用的身份验证中间件的实现
【解决方案2】:

因此,在遇到类似类型的问题后,brockallen 向我发送了一些代码,这些代码为我提供了使用身份服务器 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);
        };
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-19
    • 2020-07-09
    • 1970-01-01
    • 2018-08-29
    • 2017-02-07
    相关资源
    最近更新 更多