private static X509Certificate2 LoadCertificate(string certificateSubject) { X509Store x509Store = new X509Store(StoreLocation.LocalMachine); X509Certificate2 result; try { x509Store.Open(OpenFlags.ReadOnly); X509Certificate2 x509Certificate = CertificateStore.FindLatestCertificate(x509Store, X509FindType.FindBySubjectName, certificateSubject) ?? CertificateStore.FindLatestCertificate(x509Store, X509FindType.FindBySubjectDistinguishedName, certificateSubject); if (x509Certificate == null) { throw new InvalidOperationException("Unable to load certificate."); } result = x509Certificate; } finally { x509Store.Close(); } return(result); }
private string IssueTenantAccessToken(string tenantId) { string text = string.Format("{0}@{1}", this.settings.PartnerId, tenantId); string arg = string.Format("{0}/{1}", this.settings.AcsId, this.settings.AcsUrl.Authority); string text2 = string.Format("{0}@{1}", arg, tenantId); JsonWebSecurityToken jsonWebSecurityToken = new JsonWebSecurityToken(text, text2, DateTime.UtcNow, DateTime.UtcNow.AddDays(1.0), Enumerable.Empty <JsonWebTokenClaim>(), CertificateStore.GetSigningCredentials(this.settings.CertificateSubject)); string text3 = string.Format("{0}/{1}@{2}", this.settings.ServiceId, this.settings.ServiceHostName, tenantId); OAuth2AccessTokenRequest oauth2AccessTokenRequest = OAuth2MessageFactory.CreateAccessTokenRequestWithAssertion(jsonWebSecurityToken, text3); oauth2AccessTokenRequest.Scope = text3; OAuth2S2SClient oauth2S2SClient = new OAuth2S2SClient(); OAuth2AccessTokenResponse oauth2AccessTokenResponse = (OAuth2AccessTokenResponse)oauth2S2SClient.Issue(this.settings.AcsUrl.AbsoluteUri, oauth2AccessTokenRequest); return(oauth2AccessTokenResponse.AccessToken); }