微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

我在客户端上有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 @R_790_4045@ion 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 button
Tokeninfo 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();

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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐