I created a vanilla ASP.NET MVC AAD Authenticated application in Visual Studio 2017. It includes the following:
app.UseOpenIdConnectAuthentication(
new OpenIdConnectAuthenticationOptions
{
ClientId = clientId,
Authority = Authority,
PostLogoutRedirectUri = postLogoutRedirectUri,
Notifications = new OpenIdConnectAuthenticationNotifications()
{
// If there is a code in the OpenID Connect response, redeem it for an access token and refresh token, and store those away.
AuthorizationCodeReceived = (context) =>
{
var code = context.Code;
ClientCredential credential = new ClientCredential(clientId, appKey);
string signedInUserID = context.AuthenticationTicket.Identity.FindFirst(ClaimTypes.NameIdentifier).Value;
AuthenticationContext authContext = new AuthenticationContext(Authority, new ADALTokenCache(signedInUserID));
AuthenticationResult result = authContext.AcquireTokenByAuthorizationCode(
code, new Uri(HttpContext.Current.Request.Url.GetLeftPart(UriPartial.Path)), credential, graphResourceId);
var graphUri = new Uri(AAD_GRAPH_URI);
var serviceRoot = new Uri(graphUri, tenantId);
this.aadClient = new ActiveDirectoryClient(serviceRoot, async () => await AcquireGraphAPIAccessToken(AAD_GRAPH_URI, authContext, credential));
return Task.FromResult(0);
}
}
});
For a while HttpContext.Current.Request.Url
returns https://localhost:44345/
as is listed in the browser, and configured in Visual Studio for IIS Express.
However after a while it starts returning http://127.0.0.1/
instead! This results in the AzureAD auth returning the production URL instead of the localhost development URL.
I could hard code the development URL, but it is supposed to be dynamic so that it just works wherever I deploy it.
Why is IIS Express returning http://127.0.0.1/
instead of https://localhost:44345/
on my development box? And how do I get it to return the correct value.
`