asp.net-core – 使用ASP.NET Core后端服务器验证Google访问令牌

asp.net-core – 使用ASP.NET Core后端服务器验证Google访问令牌,第1张

概述我在客户端上有Angular2,在服务器端有ASP.NET Core.我使用 JavaScriptServices(aspnetcore-spa模板). 对于身份验证,我使用 OpenIddict,然后按照示例 here. 现在我在Controller类方法的服务器端,我想验证id_token,因为这是在this方面建议的: Important: Do not use the Google IDs 我在客户端上有Angular2,在服务器端有ASP.NET Core.我使用 JavaScriptServices(aspnetcore-spa模板).
对于身份验证,我使用 OpenIddict,然后按照示例 here.

现在我在Controller类方法的服务器端,我想验证ID_token,因为这是在this方面建议的:

important: Do not use the Google IDs returned by getID() or the user’s
profile information to communicate the currently signed in user to
your backend server. Instead,send ID tokens,which can be securely
valIDated on the server.

我还想通过ASP.NET核心身份在我的数据库中注册用户(保存电子邮件,个人资料…).

我想使用Google API client Library for .NET获取用户信息并存储refresh_token.几年前我设法用PHP做到了,但我无法用.NET来解决这个问题.
我下载了nuget软件包:Google.APIs,Google.APIs.OAuth2.v2,Google.APIs.Plus.v1.

我不确定我需要哪个nuget包,我应该使用哪个类,如何设置Google ServerKey以及如何从我从gapi.signin2 button获得的信息中获取用户信息.

简单来说:
如何使用Google .NET ClIEnt库验证.NET中的ID_token?

解决方法 我找到了解决方案 here.它很旧,但它确实有效.

var GoogleInitializer = new BaseClIEntService.Initializer();GoogleInitializer.APIKey = this.config["Authentication:Google:ServerKey"];Oauth2Service ser = new Oauth2Service(GoogleInitializer);Oauth2Service.TokeninfoRequest req = ser.Tokeninfo();req.Accesstoken = request.Accesstoken;  //access token received from Google SignIn buttonTokeninfo userinfo = await req.ExecuteAsync();

我没弄明白如何在服务器上获取显示名称和图片.但它可以在客户端完成:

onGoogleLoginSuccess(user: gAPI.auth2.GoogleUser){    console.log("basic profile",user.getBasicProfile());}

如果有人知道更新的解决方案或如何在服务器上检索基本用户配置文件,请分享.

另外我可以使用Google,但要小心,因为Google帐户不是Google帐户.我没有帐户并收到错误:

Google.APIs.Requests.RequestError Not Found [404] Errors [
Message[Not Found] Location[ – ] Reason[notFound] Domain[global] ]

在代码中:

var plusService = new PlusService(GoogleInitializer);Person me = await plusService.People.Get(userinfo.UserID).ExecuteAsync();

但是可以获取所有用户信息(图片,显示名称,名字,姓氏,生日……)

总结

以上是内存溢出为你收集整理的asp.net-core – 使用ASP.NET Core后端服务器验证Google访问令牌全部内容,希望文章能够帮你解决asp.net-core – 使用ASP.NET Core后端服务器验证Google访问令牌所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/langs/1239568.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-06-06
下一篇2022-06-06

发表评论

登录后才能评论

评论列表(0条)

    保存