Skip to content

Instantly share code, notes, and snippets.

@chgeuer
Created February 15, 2016 20:09
Show Gist options
  • Save chgeuer/a77c85a63685c2daed26 to your computer and use it in GitHub Desktop.
Save chgeuer/a77c85a63685c2daed26 to your computer and use it in GitHub Desktop.
// problem ?
// Microsoft.IdentityModel.Clients.ActiveDirectory.3.9.302111717-alpha\lib\net45\Microsoft.IdentityModel.Clients.ActiveDirectory.dll
private static X509Certificate2 FromStore(string certThumbPrint)
{
var store = new X509Store(storeName: StoreName.My, storeLocation: StoreLocation.CurrentUser);
try
{
store.Open(OpenFlags.ReadOnly);
var cert = store.Certificates.Find(
findType: X509FindType.FindByThumbprint,
findValue: certThumbPrint,
validOnly: false)[0];
return cert;
}
catch { try { if (store != null) { store.Close(); } } catch (Exception) { ; } throw; }
}
public static string GetOAuthTokenFromAAD()
{
const string ADALServiceURL = "https://login.microsoftonline.com";
var adTenant = "deadbeef-.........";
var applicationId = "deadbeef-.......";
var CertificateThumbprint = "B8789A48A020FB1F5589C9ACAF63A4EBFFF5FA1C";
var authenticationContext = new AuthenticationContext($"{ ADALServiceURL}/{adTenant}");
X509Certificate2 cert = FromStore(CertificateThumbprint);
var certCred = new ClientAssertionCertificate(clientId: applicationId, certificate: cert);
AuthenticationResult token = authenticationContext.AcquireTokenAsync(
resource: "https://management.core.windows.net/",
clientCertificate: certCred).Result;
return token.AccessToken;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment