【发布时间】:2023-04-07 16:49:01
【问题描述】:
我正在将 Auth0 添加到简单的项目中,并试图了解中间件的工作原理。
在我的 Startup.cs 中有这段代码
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory, IOptions<AuthSettings> auth0Settings)
{
loggerFactory.AddConsole(Configuration.GetSection("Logging"));
loggerFactory.AddDebug();
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseStaticFiles();
// Add the cookie middleware
app.UseCookieAuthentication(new CookieAuthenticationOptions
{
AutomaticAuthenticate = true,
AutomaticChallenge = true
});
// Add the OIDC middleware
var options = new OpenIdConnectOptions("Auth0")
{
// here there are some configurations
// .....................
};
options.Scope.Clear();
options.Scope.Add("openid");
options.Scope.Add("name");
options.Scope.Add("email");
options.Scope.Add("picture");
app.UseOpenIdConnectAuthentication(options);
app.UseMvc(routeBuilder =>
{
routeBuilder.MapRoute("Default", "{controller=Home}/{action=Index}");
});
}
如果我在我们的示例中正确理解了 ASP.NET Core 中的中间件的概念,如果存在 cookie 并且可以通过它完成身份验证
app.UseCookieAuthentication(new CookieAuthenticationOptions
{
AutomaticAuthenticate = true,
AutomaticChallenge = true
});
OpenId 中间件不会被执行。
app.UseOpenIdConnectAuthentication(options);
有人可以解释一下 OpenId 中间件是如何知道它不应该被执行的吗?
在底部我们有
app.UseMvc(routeBuilder =>
{
routeBuilder.MapRoute("Default", "{controller=Home}/{action=Index}");
});
它怎么知道它应该总是被执行,但是如果我们请求一些静态文件,我们不使用 mvc。
【问题讨论】:
标签: c# asp.net-core middleware