热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

C#学习教程:validationGoogleId令牌分享

validationGoogleId令牌我正在使用ASP.NETCore为Android客户端提供API。Android以Google帐户登录,并将ID令牌JWT传递给API作为持

validationGoogle Id令牌

我正在使用ASP.NET Core为Android客户端提供API。 Android以Google帐户登录,并将ID令牌JWT传递给API作为持票人令牌。 我有应用程序工作,它确实通过了身份validation检查,但我不认为它正在validation令牌签名。

根据Google的文档,我可以将此url称为https://www.googleapis.com/oauth2/v3/tokeninfo?id_token=XYZ123 ,但我无法在服务器端找到相应的挂钩来执行此操作。 另外根据Google文档,我可以以某种方式使用客户端访问API来执行此操作,而无需每次都调用服务器。

我的配置代码:

app.UseJwtBearerAuthentication( new JwtBearerOptions() { Authority = "https://accounts.google.com", Audience = "hiddenfromyou.apps.googleusercontent.com", TokenValidatiOnParameters= new TokenValidationParameters() { ValidateAudience = true, ValidIssuer = "accounts.google.com" }, RequireHttpsMetadata = false, AutomaticAuthenticate = true, AutomaticChallenge = false, }); 

如何让JWTBearer中间件validation签名? 我已经接近放弃使用MS中间件并自己动手了。

您可以通过几种不同的方式validation服务器端ID令牌的完整性:

  1. “手动” – 不断下载Google的公钥,validation签名,然后是每个字段,包括iss one; 主要优势(虽然我认为是一个小优势)我在这里看到的是,您可以最大限度地减少发送给Google的请求数量。
  2. “自动” – 在Google端点上执行GET以validation此令牌https://www.googleapis.com/oauth2/v3/tokeninfo?id_token={0}
  3. 使用Google API客户端库 – 就像官方的一样。

以下是第二个看起来如何:

 private const string GoogleApiTokenInfoUrl = "https://www.googleapis.com/oauth2/v3/tokeninfo?id_token={0}"; public ProviderUserDetails GetUserDetails(string providerToken) { var httpClient = new MonitoredHttpClient(); var requestUri = new Uri(string.Format(GoogleApiTokenInfoUrl, providerToken)); HttpResponseMessage httpResponseMessage; try { httpRespOnseMessage= httpClient.GetAsync(requestUri).Result; } catch (Exception ex) { return null; } if (httpResponseMessage.StatusCode != HttpStatusCode.OK) { return null; } var respOnse= httpResponseMessage.Content.ReadAsStringAsync().Result; var googleApiTokenInfo = JsonConvert.DeserializeObject(response); if (!SupportedClientsIds.Contains(googleApiTokenInfo.aud)) { Log.WarnFormat("Google API Token Info aud field ({0}) not containing the required client id", googleApiTokenInfo.aud); return null; } return new ProviderUserDetails { Email = googleApiTokenInfo.email, FirstName = googleApiTokenInfo.given_name, LastName = googleApiTokenInfo.family_name, Locale = googleApiTokenInfo.locale, Name = googleApiTokenInfo.name, ProviderUserId = googleApiTokenInfo.sub }; } 

GoogleApiTokenInfo类:

 public class GoogleApiTokenInfo { ///  /// The Issuer Identifier for the Issuer of the response. Always https://accounts.google.com or accounts.google.com for Google ID tokens. ///  public string iss { get; set; } ///  /// Access token hash. Provides validation that the access token is tied to the identity token. If the ID token is issued with an access token in the server flow, this is always /// included. This can be used as an alternate mechanism to protect against cross-site request forgery attacks, but if you follow Step 1 and Step 3 it is not necessary to verify the /// access token. ///  public string at_hash { get; set; } ///  /// Identifies the audience that this ID token is intended for. It must be one of the OAuth 2.0 client IDs of your application. ///  public string aud { get; set; } ///  /// An identifier for the user, unique among all Google accounts and never reused. A Google account can have multiple emails at different points in time, but the sub value is never /// changed. Use sub within your application as the unique-identifier key for the user. ///  public string sub { get; set; } ///  /// True if the user's e-mail address has been verified; otherwise false. ///  public string email_verified { get; set; } ///  /// The client_id of the authorized presenter. This claim is only needed when the party requesting the ID token is not the same as the audience of the ID token. This may be the /// case at Google for hybrid apps where a web application and Android app have a different client_id but share the same project. ///  public string azp { get; set; } ///  /// The user's email address. This may not be unique and is not suitable for use as a primary key. Provided only if your scope included the string "email". ///  public string email { get; set; } ///  /// The time the ID token was issued, represented in Unix time (integer seconds). ///  public string iat { get; set; } ///  /// The time the ID token expires, represented in Unix time (integer seconds). ///  public string exp { get; set; } ///  /// The user's full name, in a displayable form. Might be provided when: /// The request scope included the string "profile" /// The ID token is returned from a token refresh /// When name claims are present, you can use them to update your app's user records. Note that this claim is never guaranteed to be present. ///  public string name { get; set; } ///  /// The URL of the user's profile picture. Might be provided when: /// The request scope included the string "profile" /// The ID token is returned from a token refresh /// When picture claims are present, you can use them to update your app's user records. Note that this claim is never guaranteed to be present. ///  public string picture { get; set; } public string given_name { get; set; } public string family_name { get; set; } public string locale { get; set; } public string alg { get; set; } public string kid { get; set; } } 

根据这个github 问题 ,您现在可以使用GoogleJsonWebSignature.ValidateAsync方法来validationGoogle签名的JWT。 只需将idToken字符串传递给方法即可。

 var validPayload = await GoogleJsonWebSignature.ValidateAsync(idToken); Assert.NotNull(validPayload); 

如果它不是有效的,它将返回null

请注意,要使用此方法,您需要直接安装Google.Apis.Auth nuget。

谷歌在openId connect的文档中说明

出于调试目的,您可以使用Google的tokeninfo端点。 假设您的ID令牌的值是XYZ123。

您不应该使用该端点来validation您的JWT。

validationID令牌需要几个步骤:

  1. validationID令牌是否由颁发者正确签名。 Google发布的令牌使用在发现文档的jwks_uri字段中指定的URI中找到的证书之一进行签名。
  2. validationID令牌中的iss值是否等于https://accounts.google.com或accounts.google.com。
  3. validationID令牌中的aud值是否等于应用程序的客户端ID。
  4. validationID令牌的到期时间(exp)是否未通过。
  5. 如果您在请求中传递了hd参数,请validationID令牌是否具有与您的G Suite托管域匹配的高清版权声明。

有一个关于如何在这里validation它们的官方示例项目。 很遗憾,我们尚未将此添加到Google .Net客户端库中。 它已被记录为一个问题

所以,我发现,因为OpenIDConnect规范有一个/.well-known/ url,其中包含validation令牌所需的信息。 这包括访问签名的公钥。 JWT中间件形成来自权威机构的.well-已知url,检索信息,然后继续自行validation。

这个问题的简短回答是validation已经在中间件中发生,没有什么可做的了。

上述就是C#学习教程:validationGoogle Id令牌分享的全部内容,如果对大家有所用处且需要了解更多关于C#学习教程,希望大家多多关注—编程笔记


推荐阅读
  • Iamtryingtomakeaclassthatwillreadatextfileofnamesintoanarray,thenreturnthatarra ... [详细]
  • Spring源码解密之默认标签的解析方式分析
    本文分析了Spring源码解密中默认标签的解析方式。通过对命名空间的判断,区分默认命名空间和自定义命名空间,并采用不同的解析方式。其中,bean标签的解析最为复杂和重要。 ... [详细]
  • 搭建Windows Server 2012 R2 IIS8.5+PHP(FastCGI)+MySQL环境的详细步骤
    本文详细介绍了搭建Windows Server 2012 R2 IIS8.5+PHP(FastCGI)+MySQL环境的步骤,包括环境说明、相关软件下载的地址以及所需的插件下载地址。 ... [详细]
  • 本文介绍了OC学习笔记中的@property和@synthesize,包括属性的定义和合成的使用方法。通过示例代码详细讲解了@property和@synthesize的作用和用法。 ... [详细]
  • 在说Hibernate映射前,我们先来了解下对象关系映射ORM。ORM的实现思想就是将关系数据库中表的数据映射成对象,以对象的形式展现。这样开发人员就可以把对数据库的操作转化为对 ... [详细]
  • CentOS 7部署KVM虚拟化环境之一架构介绍
    本文介绍了CentOS 7部署KVM虚拟化环境的架构,详细解释了虚拟化技术的概念和原理,包括全虚拟化和半虚拟化。同时介绍了虚拟机的概念和虚拟化软件的作用。 ... [详细]
  • web.py开发web 第八章 Formalchemy 服务端验证方法
    本文介绍了在web.py开发中使用Formalchemy进行服务端表单数据验证的方法。以User表单为例,详细说明了对各字段的验证要求,包括必填、长度限制、唯一性等。同时介绍了如何自定义验证方法来实现验证唯一性和两个密码是否相等的功能。该文提供了相关代码示例。 ... [详细]
  • 本文介绍了如何使用Express App提供静态文件,同时提到了一些不需要使用的文件,如package.json和/.ssh/known_hosts,并解释了为什么app.get('*')无法捕获所有请求以及为什么app.use(express.static(__dirname))可能会提供不需要的文件。 ... [详细]
  • 导出功能protectedvoidbtnExport(objectsender,EventArgse){用来打开下载窗口stringfileName中 ... [详细]
  • Spring常用注解(绝对经典),全靠这份Java知识点PDF大全
    本文介绍了Spring常用注解和注入bean的注解,包括@Bean、@Autowired、@Inject等,同时提供了一个Java知识点PDF大全的资源链接。其中详细介绍了ColorFactoryBean的使用,以及@Autowired和@Inject的区别和用法。此外,还提到了@Required属性的配置和使用。 ... [详细]
  • 如何查询zone下的表的信息
    本文介绍了如何通过TcaplusDB知识库查询zone下的表的信息。包括请求地址、GET请求参数说明、返回参数说明等内容。通过curl方法发起请求,并提供了请求示例。 ... [详细]
  • 本文记录了在vue cli 3.x中移除console的一些采坑经验,通过使用uglifyjs-webpack-plugin插件,在vue.config.js中进行相关配置,包括设置minimizer、UglifyJsPlugin和compress等参数,最终成功移除了console。同时,还包括了一些可能出现的报错情况和解决方法。 ... [详细]
  • 本文介绍了如何使用JSONObiect和Gson相关方法实现json数据与kotlin对象的相互转换。首先解释了JSON的概念和数据格式,然后详细介绍了相关API,包括JSONObject和Gson的使用方法。接着讲解了如何将json格式的字符串转换为kotlin对象或List,以及如何将kotlin对象转换为json字符串。最后提到了使用Map封装json对象的特殊情况。文章还对JSON和XML进行了比较,指出了JSON的优势和缺点。 ... [详细]
  • VueCLI多页分目录打包的步骤记录
    本文介绍了使用VueCLI进行多页分目录打包的步骤,包括页面目录结构、安装依赖、获取Vue CLI需要的多页对象等内容。同时还提供了自定义不同模块页面标题的方法。 ... [详细]
  • 本文介绍了一个React Native新手在尝试将数据发布到服务器时遇到的问题,以及他的React Native代码和服务器端代码。他使用fetch方法将数据发送到服务器,但无法在服务器端读取/获取发布的数据。 ... [详细]
author-avatar
长不大的二楞子
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有