The following code was written using VS in Windows and it seems to run well inside Windows.
List<Claim> claimsList;
InMemorySymmetricSecurityKey key;
// fill claims and key with data
var token = new JwtSecurityToken
(
claims: claimsList,
signingCredentials: new SigningCredentials(key, SecurityAlgorithms.HmacSha256Signature, SecurityAlgorithms.Sha256Digest)
);
var tokenHandler = new JwtSecurityTokenHandler();
string tokenStr = tokenHandler.WriteToken(token);
When testing on MonoDevelop in Ubuntu it fails on tokenHandler.WriteToken(token)
with the following message:
System.InvalidOperationException : IDX10632: SymmetricSecurityKey.GetKeyedHashAlgorithm( 'http://www.w3.org/2001/04/xmldsig-more#hmac-sha256' ) threw an exception.
SymmetricSecurityKey: 'System.IdentityModel.Tokens.InMemorySymmetricSecurityKey'
SignatureAlgorithm: 'http://www.w3.org/2001/04/xmldsig-more#hmac-sha256', check to make sure the SignatureAlgorithm is supported.
Exception: 'System.NotSupportedException: Specified method is not supported.
at System.IdentityModel.Tokens.InMemorySymmetricSecurityKey.GetKeyedHashAlgorithm (System.String algorithm) <0x40200b40 + 0x0008b> in <filename unknown>:0
at System.IdentityModel.Tokens.SymmetricSignatureProvider..ctor (System.IdentityModel.Tokens.SymmetricSecurityKey key, System.String algorithm) <0x40200350 + 0x000c7> in <filename unknown>:0 '.
----> System.NotSupportedException : Specified method is not supported.
I haven't found much on Google or StackOverflow about this, and haven't thought of much else that I could try. Does it depend on some external resource or package which needs to be installed? I have made sure OpenSSL is installed.
This seems to be related however I am not using Katana or OpenID and this is not an issue to do with symmetric vs asymmetric.
This appears to be a bug/deficiency in Mono. It was reported back in 2015, but unfortunately as of now (September 2017), it has still not been fixed.