【问题标题】:Middleware to exclude file type from authentication从身份验证中排除文件类型的中间件
【发布时间】:2019-04-04 13:36:57
【问题描述】:

我有这个中间件,它会检查用户是否已针对所有传入的请求进行身份验证。

app.Use(async (context, next) =>
{
    if (!context.User.Identity.IsAuthenticated 
        && context.Request.Path != "/Home/Index"
        && context.Request.Path != "/Home/Login")
    {
        await context.ChallengeAsync();
    }
    else
    {
        await next();
    }
});

但是,有一种文件类型 (PBF) 不需要安全。请求类似于:

context.Request.Path = 站点/文件夹/68-09.pbf

本质上,这些文件是二进制文件,当用户将鼠标拖到地理位置时,这些文件用于将对象渲染到打开的街道地图上,因此这些文件可以每秒渲染 100 次!因此,我想避免在中间件中检查它们以加快网站速度。

我试过这个:

app.UseWhen(context => !context.Request.Path.Value.Contains(".pbf"), appBuilder =>
{
    app.Use(async (context, next) =>
    {
        if (!context.User.Identity.IsAuthenticated
            && context.Request.Path != "/Home/Index"
            && context.Request.Path != "/Home/Login")
        {
            await context.ChallengeAsync();
        }
        else
        {
            await next();
        }
    });
});

但它并没有避免 PBF 文件,这可能吗,如果可以,有什么帮助吗?

【问题讨论】:

    标签: c# asp.net-core asp.net-core-2.0 middleware


    【解决方案1】:

    将嵌套的app.Use()更改为appBuilder.Use()

    asp.UseWhen(context => !context.Request.Path.Value.Contains(".pbf"), appBuilder =>
    {
        appBuilder.Use(async (context, next) =>
        {
            if (!context.User.Identity.IsAuthenticated
                && context.Request.Path != "/Home/Index"
                && context.Request.Path != "/Home/Login")
            {
                await context.ChallengeAsync();
            }
            else
            {
                await next();
            }
        });
    });
    

    【讨论】:

      猜你喜欢
      • 2017-06-14
      • 1970-01-01
      • 2019-03-15
      • 1970-01-01
      • 2011-04-21
      • 1970-01-01
      • 2016-05-10
      • 2017-10-14
      • 1970-01-01
      相关资源
      最近更新 更多