gpt4 book ai didi

c# - 从身份验证中排除文件类型的中间件

转载 作者:行者123 更新时间:2023-11-30 16:38:14 26 4
gpt4 key购买 nike

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

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

本质上,这些文件是二进制文件,用于在用户将鼠标拖动到地理位置时将对象渲染到开放的街道 map 上,因此这些文件每秒可以渲染 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();
}
});
});

关于c# - 从身份验证中排除文件类型的中间件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55517383/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com