ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

中间件从身份验证中排除文件类型

2019-12-11 01:07:54  阅读:256  来源: 互联网

标签:asp-net-core middleware asp-net-core-2-0 c


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

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 = site/folder/68-09.pbf

本质上,这些文件是二进制文件,当用户将鼠标拖动到Geolocation中时,这些文件用于将对象渲染到开放的街道地图上,因此这些文件可以每秒渲染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文件,这是否可能,如果可以的话,有什么帮助吗?

解决方法:

将嵌套的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();
        }
    });
});

标签:asp-net-core,middleware,asp-net-core-2-0,c
来源: https://codeday.me/bug/20191210/2105299.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有