c# - JWT Fire base 验证 ID
问题描述
我已经复制了一个库来验证基于这个respo的 firebase 令牌。我的应用程序像后端一样使用 azure 函数,因此在用户登录后,每个操作都会使用令牌发送到 azure 函数,并且 azure 函数将验证该令牌然后响应结果。图书馆从这里获取公钥。我创建了一个天蓝色函数来测试这个库。我的请求:
var client = new RestClient("http://localhost:7071/api/test-connection");
client.Timeout = -1;
var request = new RestRequest(Method.GET);
request.AddHeader("x-requested-with", "XMLHttpRequest");
request.AddHeader("Authorization", "Bearer <My Firebase Token>");
IRestResponse response = client.Execute(request);
Console.WriteLine(response.Content);
我遇到了异常
IDX10516: Signature validation failed. Unable to match key:
kid: 'System.String'.
Exceptions caught:
'System.Text.StringBuilder'.
token: 'System.IdentityModel.Tokens.Jwt.JwtSecurityToken'. Valid Lifetime: 'System.Boolean'. Valid Issuer: 'System.Boolean'
我花了两天多的时间研究原因和解决方案。一周前我的代码运行良好。问题是由我从谷歌获得的公钥引起的吗?
解决方案
I was getting the same error, and was having a really hard time debugging it.
Based on this answer, I added the following code to my Startup.cs
:
if (env.IsDevelopment())
{
IdentityModelEventSource.ShowPII = true;
}
Once I made this change and reproduced the error, I was able to see the actual value for kid
(instead of kid: 'System.String'
in my error message, I saw kid: 'ABCDEF'
- not the real value, but hopefully you get the idea).
Next, I compared that value to the kid
values listed here - and sure enough, that value was not present, hence the "unable to match key" error.
In my case, it turns out that I was not obtaining the token from Firebase correctly. I was making a request to https://identitytoolkit.googleapis.com/v1/accounts:signInWithPassword
, but from the documentation I found this:
returnSecureToken
boolean
- Should always be true.
I was not setting this property in my request. After using a token obtained when setting this property correctly, I stopped getting the "IDX10516: Signature validation failed. Unable to match key" error in my C# code.
推荐阅读
- ios - 使用 swift 在 iOS 上本地保存和检索照片及其所有元数据
- spring-boot - PlatformApplicationDisabledException
- python - 在 python 上使用 mysql 时出现错误,例如 from flask.ext.mysql import MySQL
- javascript - 如何切换 ExtJs 网格操作列图标和工具提示?
- ios - 通过 UIActivityViewController 将文件导出到 iCloud Drive 时,文件名中不显示文件扩展名?
- reactjs - 模态不适用于反应 js,点击编辑按钮
- java - 使用java Streams计算文件中的所有单词
- google-bigquery - 如何将数据从谷歌标签管理器 (GTM) 发送到谷歌 bigquery
- android - Flutter 异常:尝试运行应用程序时出现的 firebase 异常
- c# - Mono 说证书无效 OpenSSL 说证书有效