I'm attempting to retrieve the Azure Authorisation code so that I can extract the access token in order to make calls to the Azure Billing API (RateCard and Usage APIs).
I'm following the oauth protocol explained by Microsoft here. My implementation is shown below. Note the redirect_uri in the params is the uri which Azure AD will redirect back to after it authenticates. However when I attempt to execute this I get the following cors error: "XMLHttpRequest cannot load "https://login.microsoftonline.com/imonlineservices.com/oauth2/authorize?res...ure.com&redirect_uri=http%3a%2f%2flocalhost%3a13333%2f&response_mode=query". No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:13333' is therefore not allowed access." as shown in Figure 2.
As I understand it this is telling me that there is no "Access-Control-Allow-Origin" header present on "login.microsoftonline.com...." which causes my request to fail due to the same-origin policy.
Now although I understand this error, I'm still confused as going by the explanation given by the link mentioned above, this should work? I'm not sure where exactly I'm going wrong here.
Method responsible for attempting to retrieve Auth code:
public void GetAuthorizationCode(string ClientId)
{
var @params = new NameValueCollection
{
{"response_type", "code"},
{ "client_id", ClientId},
{"resource", "https://management.azure.com"},
{ "redirect_uri", "http://localhost:13333/"}
};
var queryString = System.Web.HttpUtility.ParseQueryString(string.Empty);
queryString.Add(@params);
Response.Redirect(String.Format("https://login.windows.net/common/oauth2/authorize?{0}", queryString));
}
UPDATE
When pasting the redirect request directly into the browser this ..."https://login.windows.net/common/oauth2/authorize?{0}", queryString".. login.windows.net returns the response (code) back to my application as a query string parameter as shown in Figure 1. below, which indicates to me that all the necessary Azure configuration has been completed correctly and error simply lies with how I'm attempting to retrieve this code.
Figure 1: Successful redirect from azure to my application with code in query string.