Google documentation에 설명 된대로 서비스 계정으로 승인 할 JWT를 만들려고합니다. System.IdentityModel.Tokens.Jwt을 사용하고 있습니다.System.IdentityModel.Tokens.Jwt를 사용하여 Google OAuth2 호환 알고리즘 RSA SHA-256으로 JWT를 생성하는 방법?
byte[] key = Convert.FromBase64String("...");
var certificate = new X509Certificate2(key, "notasecret");
DateTime now = DateTime.UtcNow;
TimeSpan span = now - UnixEpoch;
Claim[] claims =
{
new Claim("iss", "[email protected]"),
new Claim("scope", "https://www.googleapis.com/auth/plus.me"),
new Claim("aud", "https://accounts.google.com/o/oauth2/token"),
new Claim("iat", span.TotalSeconds.ToString()),
new Claim("exp", span.Add(TimeSpan.FromHours(1)).TotalSeconds.ToString())
};
JwtSecurityTokenHandler handler = new JwtSecurityTokenHandler();
var descriptor = new SecurityTokenDescriptor
{
SigningCredentials = new SigningCredentials(
new InMemorySymmetricSecurityKey(key),
"http://www.w3.org/2001/04/xmldsig-more#hmac-sha256",
"http://www.w3.org/2001/04/xmlenc#sha256"),
Subject = new ClaimsIdentity(claims)
};
JwtSecurityToken jwtSecurityToken = (JwtSecurityToken)handler.CreateToken(descriptor);
string json = handler.WriteToken(jwtSecurityToken);
출력 : 나는 다음과 같은 코드가
{ "typ" : "JWT" , "alg" : "HS256" }
구글이 명시 적으로 SHA-256 지원 상태 일 때 :
서비스 계정은 RSA SHA-256에 의존 알고리즘 및 JWT 토큰 형식
따르면 wtSecurityTokenHandler.InboundAlgorithmMap에 :
new SigningCredentials(
new InMemorySymmetricSecurityKey(key),
"http://www.w3.org/2001/04/xmldsig-more#rsa-sha256",
"http://www.w3.org/2001/04/xmlenc#sha256");
나는 예외 받고 있어요 :
System.InvalidOperationException: IDX10632: SymmetricSecurityKey.GetKeyedHashAlgorithm('http://www.w3.org/2001/04/xmldsig-more#rsa-sha256') threw an exception.
SymmetricSecurityKey: 'System.IdentityModel.Tokens.InMemorySymmetricSecurityKey'
SignatureAlgorithm: 'http://www.w3.org/2001/04/xmldsig-more#rsa-sha256', check to make sure the SignatureAlgorithm is supported.
는이 알고리즘을 지원하지 않습니다 마이크로 소프트 뜻을
RS256 => http://www.w3.org/2001/04/xmldsig-more#rsa-sha256
HS256 => http://www.w3.org/2001/04/xmldsig-more#hmac-sha256
그래서 내가 코드를 변경하는 경우 Google은 독점적으로 지원합니까?
아마도 서명 및 다이제스트 알고리즘에 내장 된 상수를 사용해보십시오. (http://msdn.microsoft.com/en-us/library/system.identitymodel.tokens.securityalgorithms%28v=vs.110%29.aspx) –
@Jeff : 안녕하세요, 죄송합니다. 사용자 의견에 대한 알림을 놓쳤습니다. 좋은 지적. 그러나 불행히도 여전히 작동하지 않습니다. – abatishchev