c# - 带有 Microsoft 标识的 JWT RsaSha256
问题描述
我正在使用 Google API 创建服务到服务应用程序的服务,但我遇到了身份验证问题。
也许是我对 RS256 协议缺乏了解,因为我已经查看了这里的问题并且不理解我做错了什么。过去使用 HmacSha256 使用了以下代码,但是当我尝试对 RSA 执行相同操作时,出现异常错误。
using System.IdentityModel.Tokens.Jwt;
using Microsoft.IdentityModel.Tokens;
public static string Generate(string user, string privatekey)
{
{
DateTime Expiry = DateTime.Today.AddMinutes(45);
int expiryTimeStamp = (int)(Expiry - new DateTime(1970, 1, 1)).TotalSeconds;
int iat = (int)(DateTime.Now - new DateTime(1970, 1, 1)).TotalSeconds;
var securityKey = new Microsoft.IdentityModel.Tokens.SymmetricSecurityKey(Encoding.UTF8.GetBytes(privatekey));
var credentials = new SigningCredentials(securityKey, SecurityAlgorithms.RsaSha256Signature);
var header = new JwtHeader(credentials);
var payload = new JwtPayload
{
{ "iss", user }, //Service user unique email
{ "scope", "https://www.googleapis.com/auth/admin.reports.usage.readonly" }, //Scope of data
{ "aud", "https://oauth2.googleapis.com/token" },
{ "exp", expiryTimeStamp },
{ "iat", iat },
};
var secToken = new JwtSecurityToken(header, payload);
var handler = new JwtSecurityTokenHandler();
var tokenString = handler.WriteToken(secToken);
return tokenString;
}
}
}
任何帮助得到这个工作将不胜感激!
谢谢!
解决方案
推荐阅读
- android - 在 CollapsingToolbarLayout 和带有滚动的底部工作表的情况下,Android 滚动行为冲突
- type-conversion - 将 HDF5 文件中的字符串数据转换为浮点格式
- java - 为什么它最终删除了0?
- c++ - “-flax-vector-conversions”对于 ARM 编译器到底意味着什么?
- html - 谷歌浏览器用蓝色虚线限制页面的边框
- reactjs - 如何在 data-grid material-ui 中使用延迟加载
- java - jcifs.smb.SmbException:连接失败:0.0.0.0<00>/192.168.43.103 jcifs.util.transport.TransportException java.net.SocketException
- javascript - 停止点击事件触发
- r - 如何从一个图中的多列创建点图?
- android - MapBox 中 PathOverlay 的替代方案是什么?