I have an Asp.Net Mvc app to which I have added Office 365 service. Azure Active Directory has been created and Username/Applications have been populated properly in the Azure portal.
I am able to login from the app and get the Authorization code. However when code is submitted to get the OAuth token, it throws AdalServiceException with "The remote server returned an error: (404) Not Found." message.
I'm using Owin OpenIdConnect authentication for communicating Office365 API's. For this I'm injecting my authentication flow in Owin StartUp class, as shown below:
public void ConfigureAuth(IAppBuilder app)
{
app.SetDefaultSignInAsAuthenticationType(CookieAuthenticationDefaults.AuthenticationType);
app.UseCookieAuthentication(new CookieAuthenticationOptions());
app.UseOpenIdConnectAuthentication(
new OpenIdConnectAuthenticationOptions
{
ClientId = AADAppSettings.ClientId,
Authority = AADAppSettings.Authority,
TokenValidationParameters = new System.IdentityModel.Tokens.TokenValidationParameters
{
ValidateIssuer = false
},
Notifications = new OpenIdConnectAuthenticationNotifications()
{
AuthorizationCodeReceived = (context) =>
{
var code = context.Code;
ClientCredential credential = new ClientCredential(AADAppSettings.ClientId, AADAppSettings.AppKey);
string tenantID = context.AuthenticationTicket.Identity.FindFirst("http://schemas.microsoft.com/identity/claims/tenantid").Value;
string signedInUserID = context.AuthenticationTicket.Identity.FindFirst(ClaimTypes.NameIdentifier).Value;
AuthenticationContext authContext = new AuthenticationContext(string.Format("{0}/{1}", AADAppSettings.AuthorizationUri, tenantID), new NaiveSessionCache(signedInUserID));
AuthenticationResult result = authContext.AcquireTokenByAuthorizationCode(code,
new Uri(HttpContext.Current.Request.Url.GetLeftPart(UriPartial.Path)),
credential,
AADAppSettings.AADGraphResourceId);
AuthenticationHelper.SetToken(result.AccessToken);
return Task.FromResult(0);
},
RedirectToIdentityProvider = (context) =>
{
string appBaseUrl = context.Request.Scheme + "://" + context.Request.Host + context.Request.PathBase;
context.ProtocolMessage.RedirectUri = appBaseUrl + "/";
context.ProtocolMessage.PostLogoutRedirectUri = appBaseUrl;
return Task.FromResult(0);
},
AuthenticationFailed = (context) =>
{
// Suppress the exception
context.HandleResponse();
return Task.FromResult(0);
}
}
});
}
Exception is thrown at below statement.
AuthenticationResult result = authContext.AcquireTokenByAuthorizationCode(code,
new Uri(HttpContext.Current.Request.Url.GetLeftPart(UriPartial.Path)),
credential,
AADAppSettings.AADGraphResourceId);