从身份验证中排除文件类型的中间件
Middleware to exclude file type from authentication
我有这个中间件,它会检查用户是否针对所有传入的请求进行了身份验证。
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
本质上,这些文件是二进制文件,用于在用户将鼠标拖动到地理位置时将对象渲染到开放的街道地图上,因此这些文件每秒可以渲染 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();
}
});
});
我有这个中间件,它会检查用户是否针对所有传入的请求进行了身份验证。
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
本质上,这些文件是二进制文件,用于在用户将鼠标拖动到地理位置时将对象渲染到开放的街道地图上,因此这些文件每秒可以渲染 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();
}
});
});