public string Protect(AuthenticationTicket data) { if (data == null) { throw new ArgumentNullException("data"); } string publicKey = data.Properties.Dictionary.ContainsKey(PublicKeyPropertyKey) ? data.Properties.Dictionary[PublicKeyPropertyKey] : null; if (string.IsNullOrWhiteSpace(publicKey)) { throw new InvalidOperationException("AuthenticationTicket.Properties does not include audience"); } AudienceManager manager = new AudienceManager(); AudienceModel audience = manager.Find(publicKey); string symmetricKeyAsBase64 = audience.PrivateKey; var keyByteArray = TextEncodings.Base64Url.Decode(symmetricKeyAsBase64); var signingKey = new HmacSigningCredentials(keyByteArray); var issued = data.Properties.IssuedUtc; var expires = data.Properties.ExpiresUtc; var token = new JwtSecurityToken(_issuer, publicKey, data.Identity.Claims, issued.Value.UtcDateTime, expires.Value.UtcDateTime, signingKey); var handler = new JwtSecurityTokenHandler(); var jwt = handler.WriteToken(token); return(jwt); }
public override Task ValidateClientAuthentication(OAuthValidateClientAuthenticationContext context) { string clientId = string.Empty; string clientSecret = string.Empty; string symmetricKeyAsBase64 = string.Empty; if (!context.TryGetBasicCredentials(out clientId, out clientSecret)) { context.TryGetFormCredentials(out clientId, out clientSecret); } if (context.ClientId == null) { context.SetError("invalid_clientId", "client_Id is not set"); return(Task.FromResult <object>(null)); } AudienceManager manager = new AudienceManager(); AudienceModel audience = manager.Find(context.ClientId); if (audience == null) { context.SetError("invalid_clientId", string.Format("Invalid client_id '{0}'", context.ClientId)); return(Task.FromResult <object>(null)); } context.Validated(); return(Task.FromResult <object>(null)); }