0

我有一个已添加 Office 365 服务的 Asp.Net Mvc 应用程序。Azure Active Directory 已创建,并且用户名/应用程序已在 Azure 门户中正确填充。

我可以从应用程序登录并获取授权码。但是,当提交代码以获取 OAuth 令牌时,它会引发 AdalServiceException,并显示“远程服务器返回错误:(404) 未找到”。信息。

我正在使用 Owin OpenIdConnect 身份验证来通信 Office365 API。为此,我在 Owin StartUp 类中注入了我的身份验证流程,如下所示:

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);
                        }
                    }

                });
        }

在下面的语句中抛出异常。

AuthenticationResult result = authContext.AcquireTokenByAuthorizationCode(code,
                                    new Uri(HttpContext.Current.Request.Url.GetLeftPart(UriPartial.Path)),
                                    credential,
                                    AADAppSettings.AADGraphResourceId);
4

0 回答 0