问题描述
我们有我们自己的OpenID提供商连接。我们想通过自定义查询参数使用Owin中间件认证的要求。我们无法找到如何实现这一点使用的 Microsoft.Owin.Security.OpenIdConnect 的组装方式。即使我们无法找到如何将标准要求参数添加到认证请求(例如 login_hint 的参数)。
We have our own OpenID Connect Provider. We want to pass custom query parameter in Authentication request using Owin middleware. And we cannot find the way how to implement this using Microsoft.Owin.Security.OpenIdConnect assembly. Even We cannot find how to add a standard request parameter to Authentication Request (e.g. "login_hint parameter").
例如谷歌有的 login_hint 的和高清的参数(https://developers.google.com/accounts/docs/OAuth2Login#sendauthrequest),我们希望有几乎相同的参数,但我们甚至无法找到如何使用这些Owin参数发送到谷歌试图此code:
For example Google has "login_hint" and "hd" parameters (https://developers.google.com/accounts/docs/OAuth2Login#sendauthrequest), and we want to have almost the same parameters. But we even cannot find how to send these parameters to Google using Owin. Tried this code:
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 ", "[email protected]");
properties.Dictionary.Add("hd", "hd");
ctx.Authentication.Challenge(properties, provider);
return new HttpUnauthorizedResult();
}
但没有将生成认证请求的URL的 login_hint 的和高清的参数。
会很感激的任何帮助,以解决这一问题。
Will be very grateful for any help to resolve this problem.
推荐答案
您快到了!剩下的是什么才是硬道理内置的 GoogleOAuth2AuthenticationProvider
和这里的例子,如何做到这一点:
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中间件注册:
OWIN middleware registration:
app.UseGoogleAuthentication(new Microsoft.Owin.Security.Google.GoogleOAuth2AuthenticationOptions()
{
// other config ...
Provider = new CustomGoogleAuthProvider(),
});
结果(通过与谷歌的OAuth中间件的当前版本(3.0.1)的方式从login_hint认证参数流出的最箱):
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):
这篇关于Owin,传递认证请求自定义查询参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!