Ali*_*sei 15 c# asp.net-mvc owin katana openid-connect
我们有自己的OpenID Connect Provider.我们希望使用Owin中间件在身份验证请求中传递自定义查询参数.我们无法找到使用Microsoft.Owin.Security.OpenIdConnect程序集实现此方法的方法.甚至我们也找不到如何向Authentication Request添加标准请求参数(例如" login_hint parameter").
例如,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();
}
Run Code Online (Sandbox Code Playgroud)
但是生成身份验证请求网址时将不使用" login_hint "和" hd "参数.
非常感谢任何帮助解决这个问题.
Eug*_*kov 12
你快到了!剩下的是覆盖内置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);
};
}
}
Run Code Online (Sandbox Code Playgroud)
OWIN中间件注册:
app.UseGoogleAuthentication(new Microsoft.Owin.Security.Google.GoogleOAuth2AuthenticationOptions()
{
// other config ...
Provider = new CustomGoogleAuthProvider(),
});
Run Code Online (Sandbox Code Playgroud)
结果(顺便提一下当前版本(3.0.1)的Google OAuth中间件login_hint会从身份验证参数开箱即用):
