/// <summary> /// This method returns the CID of the reseller given the Microsoft ID and the sales agent token /// </summary> /// <param name="microsoftId">Microsoft ID of the reseller</param> /// <param name="sa_Token">sales agent token of the reseller</param> /// <returns>Reseller cid that is required to use the partner apis</returns> public static string GetCid(string resellerMicrosoftId, AuthorizationToken sa_Token) { var request = (HttpWebRequest)WebRequest.Create(string.Format("https://api.cp.microsoft.com/customers/get-by-identity?provider=AAD&type=tenant&tid={0}", resellerMicrosoftId)); request.Method = "GET"; request.Accept = "application/json"; request.Headers.Add("api-version", "2015-03-31"); request.Headers.Add("x-ms-correlation-id", Guid.NewGuid().ToString()); request.Headers.Add("x-ms-tracking-id", Guid.NewGuid().ToString()); request.Headers.Add("Authorization", "Bearer " + sa_Token.AccessToken); try { //Utilities.PrintWebRequest(request, string.Empty); var response = request.GetResponse(); using (var reader = new StreamReader(response.GetResponseStream())) { var responseContent = reader.ReadToEnd(); //Utilities.PrintWebResponse((HttpWebResponse)response, responseContent); JObject crestResponse = JObject.Parse(responseContent); JToken idResponse = crestResponse["id"]; return(idResponse.ToString()); } } catch (WebException webException) { using (var reader = new StreamReader(webException.Response.GetResponseStream())) { var responseContent = reader.ReadToEnd(); Utilities.PrintErrorResponse((HttpWebResponse)webException.Response, responseContent); } } return(string.Empty); }
/// <summary> /// This method returns the CID of the reseller given the Microsoft ID and the sales agent token /// </summary> /// <param name="microsoftId">Microsoft ID of the reseller</param> /// <param name="sa_Token">sales agent token of the reseller</param> /// <returns>Reseller cid that is required to use the partner apis</returns> public static string GetCid(string resellerMicrosoftId, AuthorizationToken sa_Token) { var request = (HttpWebRequest)WebRequest.Create(string.Format("https://api.cp.microsoft.com/customers/get-by-identity?provider=AAD&type=tenant&tid={0}", resellerMicrosoftId)); request.Method = "GET"; request.Accept = "application/json"; request.Headers.Add("api-version", "2015-03-31"); request.Headers.Add("x-ms-correlation-id", Guid.NewGuid().ToString()); request.Headers.Add("x-ms-tracking-id", Guid.NewGuid().ToString()); request.Headers.Add("Authorization", "Bearer " + sa_Token.AccessToken); try { //Utilities.PrintWebRequest(request, string.Empty); var response = request.GetResponse(); using (var reader = new StreamReader(response.GetResponseStream())) { var responseContent = reader.ReadToEnd(); //Utilities.PrintWebResponse((HttpWebResponse)response, responseContent); JObject crestResponse = JObject.Parse(responseContent); JToken idResponse = crestResponse["id"]; return idResponse.ToString(); } } catch (WebException webException) { using (var reader = new StreamReader(webException.Response.GetResponseStream())) { var responseContent = reader.ReadToEnd(); Utilities.PrintErrorResponse((HttpWebResponse)webException.Response, responseContent); } } return string.Empty; }
/// <summary> /// Common routine to retrieve tokens for reseller /// </summary> /// <param name="defaultDomain">default domain of the reseller</param> /// <param name="appId">appid that is registered for this application in Azure Active Directory (AAD)</param> /// <param name="key">Key for this application in Azure Active Directory</param> /// <param name="resellerMicrosoftId">Microsoft Id of the reseller</param> private static void GetTokens(string defaultDomain, string appId, string key, string resellerMicrosoftId) { // Get Active Directory token first adAuthorizationToken = Reseller.GetAD_Token(defaultDomain, appId, key); // Using the ADToken get the sales agent token saAuthorizationToken = Reseller.GetSA_Token(adAuthorizationToken); // Get the Reseller Cid, you can cache this value resellerCid = Reseller.GetCid(resellerMicrosoftId, saAuthorizationToken); }
/// <summary> /// Get the latest sales agent token given the AD Authorization Token /// </summary> /// <param name="adAuthorizationToken">AD Authorization Token</param> /// <returns>Latest sales agent token</returns> public static AuthorizationToken GetSA_Token(AuthorizationToken adAuthorizationToken) { CSPTokenResponse saToken = GetSA_Token(adAuthorizationToken.AccessToken); AuthorizationToken saAuthorizationToken = new AuthorizationToken(saToken.access_token, Convert.ToInt64(saToken.expires_in)); return saAuthorizationToken; }
/// <summary> /// Get the latest AD token given the reseller domain and client credentials /// </summary> /// <param name="domain">domain of the reseller</param> /// <param name="clientId">clientID of the application</param> /// <param name="clientSecret">client secret of the application, also refered to as key</param> /// <returns>Latest AD Authorization token</returns> public static AuthorizationToken GetAD_Token(string domain, string clientId, string clientSecret) { AzureTokenResponse adToken = GetADToken(domain, clientId, clientSecret); AuthorizationToken adAuthorizationToken = new AuthorizationToken(adToken.access_token, Convert.ToInt64(adToken.expires_in)); return adAuthorizationToken; }