gpt4 book ai didi

asp.net - Google OpenId Connect 迁移 : getting the openid_id in ASP. NET 应用程序

转载 作者:行者123 更新时间:2023-12-02 16:48:18 25 4
gpt4 key购买 nike

我已经阅读了大量的 Google 文档和 SO Q/A,但没有运气。我想知道是否有人按照 Google 的建议成功使用了 OpenId 到 OpenId Connect 迁移。

这就是我们以前所做的:

IAuthenticationResponse response = _openid.GetResponse();
if (response != null) {
//omitted for brevity
} else {
IAuthenticationRequest req = _openid.CreateRequest("https://www.google.com/accounts/o8/id");
req.AddExtension(new ClaimsRequest
{
Country = DemandLevel.Request,
Email = DemandLevel.Request,
Gender = DemandLevel.Require,
PostalCode = DemandLevel.Require,
TimeZone = DemandLevel.Require
});
req.RedirectToProvider();
}

这是使用几年前的 DotNetOpenAuth 版本完成的。由于 Google 已弃用 OpenId 身份验证,我们正在尝试转向 OpenID Connect。这里的关键问题是:我能否使用最新版本的 DotNetOpenAuth 库或通过任何其他方式获得 OpenId 标识符(以 https://www.google.com/accounts/o8/id?id=xyz 的形式)?

我已经尝试了最新的 DotNetOpenAuth,我可以让它工作,但它给了我一个新的 ID(这是预期的)。我还通过使用此 URL 尝试了 Javascript 方式(为了可读性换行):

https://accounts.google.com/o/oauth2/auth?
scope=openid%20profile%20email
&openid.realm=http://localhost/palkkac/
&client_id=//here is the client id I created in google developer console
&redirect_uri=http://localhost/palkkac/someaspxpagehere
&response_type=id_token%20token

我检查(使用 Fiddler)我们当前使用旧的 DotNetOpenAuth 代码发送的领域值,它是 http://localhost/palkkac/。我在上面的网址中放置了相同的领域。重定向网址以领域值开头,但并不完全相同。

当我重定向到一个解析 id_token 并解密它的简单页面(使用 https://www.googleapis.com/oauth2/v1/tokeninfo?id_token=zyx 端点)时,我得到这个:

audience    "client id is here"
email "mikkark@gmail.com"
expires_in 3597
issued_at //some numbers here
issued_to "client id is here"
issuer "accounts.google.com"
user_id "here is a sequence of numbers, my id in the OpenID Connect format that is"
verified_email true

因此,这里没有您期望找到的 openid_id 字段的迹象,尽管消息的整个结构似乎与 Google 文档不同,例如没有标题为 sub 的字段。我想知道我是否实际上使用了错误的端点、参数或其他东西?

我一直在阅读的是迁移指南:https://developers.google.com/accounts/docs/OpenID。我跳过了第 2 步,因为它似乎是一个可选步骤。在步骤 3 中讨论了 openid_id 字段,我希望首先将其作为概念验证。

我们在 Google 上注册了该应用程序,以便创建客户端 ID 等。现在 Google 开发控制台中还列出了许多允许的重定向 URL 以及 JavaScript 来源。如果这些可能会扰乱系统,请告诉我,我会将其发布到此处以供审核。

旁注:我们应该将应用程序移到严格的防火墙环境后面,在该环境中我们需要打开端 Eloquent 能在服务器端执行此操作。因此,最好使用客户端 Javascript 解决方案来访问 Google,结合 HTTPS 并将结果重定向到服务器(除非存在其他问题)。

关于同一问题还有其他资源,尽管所有这些资源似乎在服务器端使用不同的库来完成这项工作,并且似乎没有人尝试使用 Javascript:

  • 在这里(https://stackoverflow.com/questions/22842475/migration-google-openid-to-openid-connect-openid-id-does-not-match)我认为问题通过将领域设置为与旧的 OpenId2.0 流程中的相同来解决。这对我来说似乎不起作用。
  • 超过 here openid_id 字段也丢失了,但这里的问题更多的是如何使用 DotNetOpenAuth 以外的库向 Google 请求 id_token。
  • 以及 here让 Google 返回 openid_id 字段似乎也存在类似问题。

最佳答案

您可以使用 GoogleAuthentication owin 中间件。

app.UseGoogleAuthentication(new GoogleOAuth2AuthenticationOptions
{
SignInAsAuthenticationType = signAs,
AuthenticationType = "Google",
ClientId = "xxx.apps.googleusercontent.com",
ClientSecret = "xx",
CallbackPath = PathString.FromUriComponent("/oauth2callback"),
Provider = new GoogleOAuth2AuthenticationProvider
{
OnApplyRedirect = context =>
{
context.Response.Redirect(context.RedirectUri + "&openid.realm=https://mydomain.com/"); // DotNetOpenAuth by default add a trailing slash, it must be exactly the same as before
}
},
BackchannelHttpHandler = new MyWebRequestHandler()
}

然后,添加一个名为 MyWebRequestHandler 的新类:

public class MyWebRequestHandler : WebRequestHandler
{
protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
{
var httpResponse = await base.SendAsync(request, cancellationToken);
if (request.RequestUri == new Uri("https://www.googleapis.com/plus/v1/people/me")) return httpResponse;

var configuration = await OpenIdConnectConfigurationRetriever.GetAsync("https://accounts.google.com/.well-known/openid-configuration", cancellationToken); // read the configuration to get the signing tokens (todo should be cached or hard coded)

// google is unclear as the openid_id is not in the access_token but in the id_token
// as the middleware dot not expose the id_token we need to parse it again
var jwt = httpResponse.Content.ReadAsStringAsync().Result;
JObject response = JObject.Parse(jwt);
string idToken = response.Value<string>((object)"id_token");

JwtSecurityTokenHandler tokenHandler = new JwtSecurityTokenHandler();

try
{
SecurityToken token;
var claims = tokenHandler.ValidateToken(idToken, new TokenValidationParameters()
{
ValidAudience = "xxx.apps.googleusercontent.com",
ValidIssuer = "accounts.google.com",
IssuerSigningTokens = configuration.SigningTokens
}, out token);

var claim = claims.FindFirst("openid_id");
// claim.Value will contain the old openid identifier
if (claim != null) Debug.WriteLine(claim.Value);
}
catch (Exception ex)
{
Debug.WriteLine(ex.ToString());
}
return httpResponse;
}
}

如果您像我一样发现这并不简单,请通过投票支持此问题来提供帮助https://katanaproject.codeplex.com/workitem/359

关于asp.net - Google OpenId Connect 迁移 : getting the openid_id in ASP. NET 应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26730568/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com