I am creating a service to service application with Google API's and I'm having issues authenticating.
Perhaps it's my lack of understanding of the RS256 protocol as I have looked through the questions on here and not understanding what I'm doing wrong. The following code has been used in the past using HmacSha256, but when I try to do the same with RSA, I get exception errors.
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;
}
}
}
Any help getting this working would be really appreciated!
Thanks!