/// <summary> /// Methods for getting a token from ACS /// Updated 10/21, to use Active Directory Authn Library (ADAL) /// Method uses OAuth Authorization Code Grant flow (3-legged OAuth) /// ADAL package avaialble from https://www.nuget.org/packages/Microsoft.IdentityModel.Clients.ActiveDirectory/1.0.0 /// </summary> public static AADJWTToken GetAuthorizationToken(string tenantName, string appPrincipalId, Uri appUri) { string authString = String.Format(StringConstants.AzureADSTSURL, tenantName); AuthenticationContext authenticationContext = new AuthenticationContext(authString); try { AuthenticationResult authenticationResult = authenticationContext.AcquireToken(StringConstants.GraphPrincipalId.ToString(), appPrincipalId, appUri); if (authenticationResult != null) { AADJWTToken token = new AADJWTToken(); token.AccessToken = authenticationResult.AccessToken; token.TokenType = authenticationResult.AccessTokenType; token.ExpiresOn = authenticationResult.ExpiresOn.UtcTicks; token.AdalToken = authenticationResult; return token; } else return null; } catch (Exception e) { //Console.WriteLine("Exception: " + e.Message + " " + e.InnerException); return null; } }
/// <summary> /// Helper method for creating DirectoryService and adding header /// for subsequent requests for the service. It also adds helper properties for collections of subtypes of DirectoryObject /// such as users, contacts etc. /// </summary> public DirectoryDataService(string tenantName, AADJWTToken token) : this(new Uri(StringConstants.DirectoryServiceURL + tenantName)) { this.authenticationToken = token; // Register the event handler that adds the headers for HTTP requests including the Authorization header. this.BuildingRequest += new EventHandler<BuildingRequestEventArgs>(OnBuildingRequest); }