根据文档,中间件的顺序应该是这样的:
app.UseStaticFiles();
app.UseRouting();
app.UseAuthentication();
app.UseAuthorization();
Run Code Online (Sandbox Code Playgroud)
根据这篇文章(保护某些路由),我有中间件来保护静态文件。我遇到的问题是订单对我不起作用。如果用户已获得授权,我只能保护文件夹。所以,我需要的地方UseProtectFolder之前UseStaticFiles和之后UseAuthentication和UseAuthorization:
app.UseRouting();
app.UseAuthentication();
app.UseAuthorization();
app.UseProtectFolder(new ProtectFolderOptions
{
Path = "/Secret",
PolicyName = "Authenticated"
});
app.UseStaticFiles();
Run Code Online (Sandbox Code Playgroud)
但这不会返回任何静态文件。看起来UseRouting正在做一些使文件不可用的事情,返回 404,因为当我将顺序更改为此,移动UseRouting之后UseStaticFiles,它可以工作:
app.UseAuthentication();
app.UseAuthorization();
app.UseProtectFolder(new ProtectFolderOptions
{
Path = "/Secret",
PolicyName = "Authenticated"
});
app.UseStaticFiles();
app.UseRouting();
Run Code Online (Sandbox Code Playgroud)
所以实际的顺序变化UseAuthentication是放在之前UseRouting(甚至之前UseStaticFiles)。
从文档:
在 Startup.Configure 方法中添加中间件组件的顺序定义了对请求调用中间件组件的顺序以及响应的相反顺序。该顺序对于安全性、性能和功能至关重要。
我现在的问题是:按照记录的顺序,为什么UseAuthentication放在UseRouting?
是否有特殊原因或仅出于性能原因?并且通过在管道中更早地移动身份验证/授权,这是否会影响响应(相反的顺序)?
我刚刚更新到ASP.NET Core 3 Preview 5,现在,当我打开解决方案并尝试构建它时,在Configure()的Startup.cs文件中,在以下位置抛出错误“方法'UseRouting'的重载为1个参数”以下代码:
app.UseRouting(routes => {
routes.MapControllerRoute(
name: "default",
template: "{controller=Home}/{action=Index}/{id?}");
routes.MapRazorPages();
});
Run Code Online (Sandbox Code Playgroud)
我确实阅读了有关Microsoft文档的一些文档,并尝试将上述代码替换为:
app.UseEndpoints(endpoints => {
endpoints.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}");
endpoints.MapRazorPages();
});
Run Code Online (Sandbox Code Playgroud)
但是,在生成期间抛出System.InvalidOperationException并带有以下上下文:
'EndpointRoutingMiddleware与EndpointMiddleware设置的端点匹配,因此必须在EndpointMiddleware之前添加到请求执行管道中。请通过在应用程序启动代码中对“ Configure(...)”的调用内调用“ IApplicationBuilder.UseRouting”来添加EndpointRoutingMiddleware。
我尝试在ConfigureServices方法中替换以下行:
services.AddMvc()
.AddNewtonsoftJson();
Run Code Online (Sandbox Code Playgroud)
宽度:
services.AddControllersWithViews()
.AddNewtonsoftJson();
services.AddRazorPages();
Run Code Online (Sandbox Code Playgroud)
这不再引发错误,但加载完成后我的页面为空白。谁可以帮助我解决这个问题?
对于我的解决方案,我使用以下软件包:
<PackageReference Include="Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore" Version="3.0.0-preview5-19227-01" />
<PackageReference Include="Microsoft.AspNetCore.Identity.EntityFrameworkCore" Version="3.0.0-preview5-19227-01" />
<PackageReference Include="Microsoft.AspNetCore.Identity.UI" Version="3.0.0-preview5-19227-01" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.NewtonsoftJson" Version="3.0.0-preview5-19227-01" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="3.0.0-preview5.19227.1" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="3.0.0-preview5.19227.1">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.Extensions.Logging.Debug" Version="3.0.0-preview5.19227.9" />
<PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="2.2.3" />
Run Code Online (Sandbox Code Playgroud)
我的解决方案的TargetFramework是netcoreapp3.0
我需要处理以下形式的传入请求: //ohif/study/1.1/series 请注意前面的额外斜杠
我的控制器签名是:
[Route("ohif/study/{studyUid}/series")]
[HttpGet]
public IActionResult GetStudy(string studyUid)
Run Code Online (Sandbox Code Playgroud)
如果我将传入请求修改为 /ohif/study/1.1/series 它工作正常
但是,当我使用 //ohif/study/1.1/series 时,路线未被命中
另外我还尝试了: [Route("/ohif/study/{studyUid}/series")] 和 [Route("//ohif/study/{studyUid}/series")]
两者都失败了。不幸的是,我无法更改传入的请求,因为它来自外部应用程序。有什么技巧可以处理这条路线吗?我正在使用 .NET Core 3.0。
更新注意: 我已激活日志记录,并且看到 asp.net core 正在分析路由,我收到消息:没有找到记录器 Microsoft.AspNetCore 的请求路径“//ohif/study/1.1/series”的候选者。路由.EndpointRoutingMiddleware
asp.net model-view-controller asp.net-core-mvc asp.net-core-2.0