0

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);
Sajad Deyargaroo
  • 1,149
  • 1
  • 7
  • 20
  • can you post the values you are passing to the AuthenticationContext constructor and the call to AcquireTokenByAuthorizationCode? – vibronet Jul 31 '15 at 17:55
  • new AuthenticationContext("https://login.microsoftonline.com/9b0315c5-0ac6-44a7-****-************", new NaiveSessionCache(signedInUserID)); signedInUserID is the value of ClaimTypes.NameIdentifier – Sajad Deyargaroo Aug 03 '15 at 08:46
  • authContext.AcquireTokenByAuthorizationCode(, new Uri("https://localhost:44312/", new ClientCredential(, "https://graph.windows.net/"); ClientId and ClientSecret are added in the Web.config automatically when we add the connected service. – Sajad Deyargaroo Aug 03 '15 at 08:52
  • The call to the auth context constructor lacks "HTTPS://" – vibronet Aug 03 '15 at 09:20
  • Also, the uri in call to acquiretoken looks odd. Are those actual values or cut&paste issues? – vibronet Aug 03 '15 at 09:23
  • All the URIs are valid, I am actually trying to run the Property Management app downloaded from below repository, without making any changes. https://github.com/OfficeDev/Property-Inspection-Code-Sample – Sajad Deyargaroo Aug 05 '15 at 03:10
  • Looking at the readme, it would appear you do need to update the URLs and other properties in order to run the demo in your environment- hence I am not sure what do you mean with "without making any changes". – vibronet Aug 05 '15 at 03:48

0 Answers0