-
-
Save njmube/574ec0e20307dfe05496 to your computer and use it in GitHub Desktop.
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
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