Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/313.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# HttpActionContext未设置AzureAD身份验证后的声明_C#_Asp.net_Azure Active Directory_Asp.net Web Api2_Microsoft Identity Platform - Fatal编程技术网

C# HttpActionContext未设置AzureAD身份验证后的声明

C# HttpActionContext未设置AzureAD身份验证后的声明,c#,asp.net,azure-active-directory,asp.net-web-api2,microsoft-identity-platform,C#,Asp.net,Azure Active Directory,Asp.net Web Api2,Microsoft Identity Platform,我有一个使用ASP.NET(.NET Framework 4.8)和ASP.NET web API 2、SQL SERVER 2016开发的web应用程序。我将Azure AD与OpenIDConnect(授权代码流)一起使用 在这个实现中,有一个容器web应用程序(ASP.NET),它承载anguarjs(前端)、web.config以及相关的bin和Global.asx文件。此外,它也是包含所有API的API项目的占位符。API项目有自己单独的web.config文件,此API项目仅由前端w

我有一个使用ASP.NET(.NET Framework 4.8)和ASP.NET web API 2、SQL SERVER 2016开发的web应用程序。我将Azure AD与OpenIDConnect(授权代码流)一起使用

在这个实现中,有一个容器web应用程序(ASP.NET),它承载anguarjs(前端)、web.config以及相关的bin和Global.asx文件。此外,它也是包含所有API的API项目的占位符。API项目有自己单独的web.config文件,此API项目仅由前端web应用程序使用。此web应用使用Azure AD(OIDC)授权代码流进行身份验证,并具有自己的自定义授权

下面是整个身份验证流程:

testapp作为单个站点托管,API项目作为应用程序托管在使用IIS 10的同一站点内,如下所示:

API项目在此实现中使用与testapp相同的应用程序池

Azure AD中有一个注册(总的来说是testpp和API)。对于此实现,Azure AD中没有单独注册API项目

现在导航到URL:我被重定向到Azure AD登录页面,然后提供我的详细信息和登录,然后可以查看cookie和id_令牌的设置,包括声明,但是在这种情况下,当对API进行调用时,我看到上下文对象未设置,并且我看到没有维护声明。我在API项目级别有单独的身份验证和授权过滤器,每当angularjs前端应用程序调用WebAPI时,就会触发该过滤器。此外,我还看到了另一个问题,如如果我直接在浏览器中导航到URL:https//testapp.com/services/api/test/method,则不会触发Azure AD登录流

下面是身份验证和授权过滤器:

public static class WebApiConfig
{
    public static void Register(HttpConfiguration config)
    {
      // Web API configuration and services
      config.Filters.Add(new TestAuthenticationAttribute());
      config.Filters.Add(new TestAuthorizationAttribute());
    }
}
TestAuthenticationAttribute.cs:

public class TestAuthenticationAttribute : ActionFilterAttribute, IAuthenticationFilter
 {
        private HttpAuthenticationContext _context;
        public Task AuthenticateAsync(HttpAuthenticationContext context, System.Threading.CancellationToken cancellationToken)
        {
            _context = context;
            return Task.FromResult(0);
        }

        public Task ChallengeAsync(HttpAuthenticationChallengeContext context, System.Threading.CancellationToken cancellationToken)
        {
            return Task.FromResult(context.Result);
        }
    }
public class TestAuthorizationAttribute : AuthorizeAttribute
{
        protected override bool IsAuthorized(HttpActionContext actionContext)
        {
            // retrieve principal and check authZ
            IPrincipal principal;
            if (!SecurityHelper.IsSecurityDisabled)
                principal = actionContext.RequestContext.Principal as IClaimsPrincipal;
            else
                principal = actionContext.RequestContext.Principal as System.Security.Claims.ClaimsPrincipal;
            if (principal != null && principal.Identity != null && principal.Identity.IsAuthenticated)
                return true;
            else
                return false;
        }

        protected override void HandleUnauthorizedRequest(HttpActionContext actionContext)
        {
            var response = actionContext.Request.CreateErrorResponse(HttpStatusCode.Unauthorized, "unauthorized");
            response.Headers.WwwAuthenticate.Add(new AuthenticationHeaderValue("Federation", "permission required."));
            actionContext.Response = response;
        }
    }
TestAuthorizationAttribute.cs:

public class TestAuthenticationAttribute : ActionFilterAttribute, IAuthenticationFilter
 {
        private HttpAuthenticationContext _context;
        public Task AuthenticateAsync(HttpAuthenticationContext context, System.Threading.CancellationToken cancellationToken)
        {
            _context = context;
            return Task.FromResult(0);
        }

        public Task ChallengeAsync(HttpAuthenticationChallengeContext context, System.Threading.CancellationToken cancellationToken)
        {
            return Task.FromResult(context.Result);
        }
    }
public class TestAuthorizationAttribute : AuthorizeAttribute
{
        protected override bool IsAuthorized(HttpActionContext actionContext)
        {
            // retrieve principal and check authZ
            IPrincipal principal;
            if (!SecurityHelper.IsSecurityDisabled)
                principal = actionContext.RequestContext.Principal as IClaimsPrincipal;
            else
                principal = actionContext.RequestContext.Principal as System.Security.Claims.ClaimsPrincipal;
            if (principal != null && principal.Identity != null && principal.Identity.IsAuthenticated)
                return true;
            else
                return false;
        }

        protected override void HandleUnauthorizedRequest(HttpActionContext actionContext)
        {
            var response = actionContext.Request.CreateErrorResponse(HttpStatusCode.Unauthorized, "unauthorized");
            response.Headers.WwwAuthenticate.Add(new AuthenticationHeaderValue("Federation", "permission required."));
            actionContext.Response = response;
        }
    }
在上面的调试中,我看到HttpActionContext没有设置任何声明,因此没有设置任何主体对象,最后我看到了未经授权的响应

如果我尝试访问任何其他URL,比如,我可以看到Azure广告登录流被触发。总的来说,我可以看到对API项目的调用失败,HTTP状态代码为:401(未经授权)

在ADFS实现的情况下,对API的调用看起来不错。我正在将身份验证模块从ADFS迁移到Azure AD,在那里我看到了问题

有人能帮我解决这个问题吗。

我看到对api的调用:根本不会触发Azure AD登录流。api作为应用程序托管在同一个站点下。我看到对api的调用:根本不会触发Azure AD登录流。这些API作为应用程序托管在同一站点下。