Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/321.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# UseWindowsAzureActiveDirectoryBeareAuthentication中间件不会在不';t存在:应用程序返回404而不是401_C#_Authentication_Asp.net Web Api_Owin_Azure Active Directory - Fatal编程技术网

C# UseWindowsAzureActiveDirectoryBeareAuthentication中间件不会在不';t存在:应用程序返回404而不是401

C# UseWindowsAzureActiveDirectoryBeareAuthentication中间件不会在不';t存在:应用程序返回404而不是401,c#,authentication,asp.net-web-api,owin,azure-active-directory,C#,Authentication,Asp.net Web Api,Owin,Azure Active Directory,我正在web api项目中使用WindowsAzureActiveDirectoryBeareAuthenticationOptions中间件,我的Startup.cs的重要部分如下所示: public static void ConfigureApp(IAppBuilder appBuilder) { HttpConfiguration config = new HttpConfiguration(); config.MapHttpAttributeRoutes();

我正在web api项目中使用WindowsAzureActiveDirectoryBeareAuthenticationOptions中间件,我的Startup.cs的重要部分如下所示:

public static void ConfigureApp(IAppBuilder appBuilder)
{    
   HttpConfiguration config = new HttpConfiguration();

   config.MapHttpAttributeRoutes();

   appBuilder.UseWindowsAzureActiveDirectoryBearerAuthentication(
        new WindowsAzureActiveDirectoryBearerAuthenticationOptions
        {
               Tenant = "xx-xx-xx",
               TokenValidationParameters = new System.IdentityModel.Tokens.TokenValidationParameters
                {
                    ValidAudience = "yy-yy-yy",
                    ValidateAudience = true
                }
         });

   config.Filters.Add(new AadAuthorizeAttribute());
   appBuilder.UseWebApi(config);
}
app.Use(async (ctx, next) =>
{
    if (ctx.Authentication.User.Identity.IsAuthenticated == false)
    {
        ctx.Response.StatusCode = 401;
        return;
    }
    await next();
});
问题是,如果我尝试访问(不存在),我会得到一个404,而我本应该得到一个401(因为来自浏览器的请求没有任何承载令牌等,并且未经验证)。如果我去一条存在的路线,我会得到401。我认为这是因为AadAuthorizeAttribute触发中间件执行,而当webapi找不到控制器/操作时,不会发生这种情况


在使用这个简单的中间件时,即使路由不存在,我如何触发任何请求的身份验证(最好不要编写我自己的)?

始终运行身份验证中间件。但它不会抛出401,那不是它的工作。它只检查标识,并在找到任何标识时将其添加到请求中

你需要的是这样的东西:

public static void ConfigureApp(IAppBuilder appBuilder)
{    
   HttpConfiguration config = new HttpConfiguration();

   config.MapHttpAttributeRoutes();

   appBuilder.UseWindowsAzureActiveDirectoryBearerAuthentication(
        new WindowsAzureActiveDirectoryBearerAuthenticationOptions
        {
               Tenant = "xx-xx-xx",
               TokenValidationParameters = new System.IdentityModel.Tokens.TokenValidationParameters
                {
                    ValidAudience = "yy-yy-yy",
                    ValidateAudience = true
                }
         });

   config.Filters.Add(new AadAuthorizeAttribute());
   appBuilder.UseWebApi(config);
}
app.Use(async (ctx, next) =>
{
    if (ctx.Authentication.User.Identity.IsAuthenticated == false)
    {
        ctx.Response.StatusCode = 401;
        return;
    }
    await next();
});

将此放在您的身份验证中间件之后,它将为任何未经身份验证的请求发回401,无论是否转到有效路径。

我明白了,但这篇文章中提到的答案是否不正确?它提到“控制器中的[Authorize]装饰或我们指定的任何方法都会触发Owin安全处理程序来验证令牌并生成声明。”是的,这个答案不正确。OWIN中间件可以在每个请求上运行,并且还具有停止执行和立即返回响应的能力,如图所示。唯一的一点是,只有当身份验证中间件在活动模式下运行时,才会发生这种行为
AuthenticationMode=AuthenticationMode.Active
。如果不进行更改,这是默认设置。这意味着它尝试在每个请求中查找身份信息,如果找到有效信息,则创建ClaimsPrincipal。