Skip to content

Instantly share code, notes, and snippets.

@njmube
Forked from pdoran/OAuthTokenUtility.cs
Last active August 29, 2015 14:23
Show Gist options
  • Save njmube/574ec0e20307dfe05496 to your computer and use it in GitHub Desktop.
Save njmube/574ec0e20307dfe05496 to your computer and use it in GitHub Desktop.
public class OAuthTokenUtility {
private readonly RSACryptoServiceProvider _PublicProvider;
private readonly RSACryptoServiceProvider _PrivateProvider;
public OAuthTokenUtility(X509Certificate2 certificate) {
_PublicProvider = (RSACryptoServiceProvider)certificate.PublicKey.Key;
_PrivateProvider = (RSACryptoServiceProvider)Certificate.PrivateKey;
}
public string Token(string username, double numberOfMinutesToExpireIn, IEnumerable<string> tokens) {
var authServerAccessToken = new AuthorizationServerAccessToken();
authServerAccessToken.Lifetime = TimeSpan.FromMinutes(2);
authServerAccessToken.ResourceServerEncryptionKey = _PublicProvider;
authServerAccessToken.AccessTokenSigningKey = _PrivateProvider;
var result = new AccessTokenResult(authServerAccessToken);
var accessToken = result.AccessToken;
var applyAuthorizationMethod = accessToken.GetType()
.GetMethods(System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic)
.FirstOrDefault(f => f.Name == "ApplyAuthorization" && f.GetParameters().Count() == 3);
var serializeMethod = accessToken.GetType()
.GetMethod("Serialize", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic);
applyAuthorizationMethod.Invoke(accessToken, new Object[] { tokens, username, TimeSpan.FromMinutes(numberOfMinutesToExpireIn) });
var output = serializeMethod.Invoke(accessToken, new Object[] { }) as string;
return output;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment