Last active
July 25, 2024 02:56
-
-
Save shibayan/79d59e7e54c2993b89d0c37075072800 to your computer and use it in GitHub Desktop.
Generate "Sign in with Apple" client_secret using .NET Core (C#)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.IdentityModel.Tokens.Jwt; | |
using System.Security.Claims; | |
using System.Security.Cryptography; | |
using Microsoft.IdentityModel.Tokens; | |
namespace ConsoleApp | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
const string aud = "https://appleid.apple.com"; | |
const string kid = "0000000000"; // Key ID | |
const string iss = "0000000000"; // Team ID | |
const string sub = "ServiceId"; // Client ID (Service ID) | |
const string privateKey = ""; // Private Key (Base64 Encode) | |
var ecdsa = ECDsa.Create(); | |
ecdsa.ImportPkcs8PrivateKey(Convert.FromBase64String(privateKey), out _); | |
var securityKey = new ECDsaSecurityKey(ecdsa) | |
{ | |
KeyId = kid | |
}; | |
var descriptor = new SecurityTokenDescriptor | |
{ | |
Issuer = iss, | |
Audience = aud, | |
Subject = new ClaimsIdentity(new[] { new Claim("sub", sub) }), | |
NotBefore = DateTime.UtcNow, | |
Expires = DateTime.UtcNow.AddDays(180), | |
SigningCredentials = new SigningCredentials(securityKey, SecurityAlgorithms.EcdsaSha256) | |
}; | |
var handler = new JwtSecurityTokenHandler(); | |
var token = handler.CreateJwtSecurityToken(descriptor); | |
Console.WriteLine(handler.WriteToken(token)); | |
} | |
} | |
} |
@mehmetutkuk I have same requirement. Did you find any cross platform code? If yes can you please share? Appreciate your help!! Thanks in advance!!!
@venujdv Updated. Try it.
Thank you @shibayan . It works if we create from local. If we publish into Azure Function App, it is throwing error like "400-Bad Request" "The system cannot find the file specified." Any idea? Thanks!
@venujdv did you figured out what is the problem? I have the same one and not sure what i can do
@OleksandrOsipchuk I have added "WEBSITE_LOAD_CERTIFICATES" in Azure function app configuration with dummy value. It worked. Please see below link and update here if it works. So that may it will help others in feature. Thanks!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This solution only for windows platform because of the "Windows CNG". I'm looking for cross platform way.