在我的 ASP .NET Core 3.1 MVC 应用程序中,我像这样使用端点路由
app.UseEndpoints(endpoints =>
{
endpoints.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}");
endpoints.MapControllerRoute(
name: "access",
pattern: "access/",
defaults: new { controller = "Home", action = "Access" });
});
Run Code Online (Sandbox Code Playgroud)
因此,浏览到 /access,启动 Access 操作,应用程序会在其中检查用户是否符合某些访问要求。
if (access checks...)
{
return View();
}
Run Code Online (Sandbox Code Playgroud)
现在我更喜欢在自定义中间件(或可能是自定义授权属性)中进行此检查,而不是在控制器中进行检查。所以我的问题是,我应该如何重写 UseEndPoints 调用,以包含 /access 区域的自定义中间件?
如https://learn.microsoft.com/en-us/aspnet/core/fundamentals/app-state?view=aspnetcore-3.1#session-state中所述,可以将会话添加到自己的 Web 应用程序中,如下所示在开始.配置服务
\n\n services.AddSession(options =>\n {\n options.IdleTimeout = TimeSpan.FromSeconds(10);\n options.Cookie.HttpOnly = true;\n options.Cookie.IsEssential = true;\n });\nRun Code Online (Sandbox Code Playgroud)\n\n并在 Startup.Configure 中
\n\nApp.UseSession()\nRun Code Online (Sandbox Code Playgroud)\n\n还可以通过身份验证中间件使用没有身份的 cookie 身份验证,如此处所述https://learn.microsoft.com/en-us/aspnet/core/security/authentication/cookie?view=aspnetcore-3.1
\n\nservices.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme)\n .AddCookie(options =>\n {\n ...\n });\nRun Code Online (Sandbox Code Playgroud)\n\n我的问题是,如果我在 Startup.Configure 中同时使用
\n\nApp.UseSession()\nApp.UseAuthentication()\nRun Code Online (Sandbox Code Playgroud)\n\n将使用哪些 Cookie 设置?services.AddSession 中的 Cookie 设置是否完全无关(因为身份验证中间件也使用会话 Cookie 来跟踪用户,对吧?或者我完全错了)?或者它们只是同时运行的两个不同的会话/服务?
\n\n我知道 Startup.Configure (HTTP 管道)是顺序敏感的,正如我的 Microsoft“将中间件添加到应用程序处理管道是顺序敏感\xe2\x80\x94it 仅影响在管道中注册的下游组件”所述。因此,我的第二个问题是,如果我将 App.UseCookiePolicy(options) 放在上面的前面,它会覆盖设置吗?
\n\nApp.UseCookiePolicy()\nRun Code Online (Sandbox Code Playgroud)\n\n预先感谢您的任何答复!
\n我试图围绕 map() 方法,在这种情况下使用它来组合(压缩)两个不同长度的数组。我已经检查了以前关于 JavaScript 压缩的问题,但它们主要涉及等长数组。
我有两个数组:
const countries = ['US', 'FR', 'IT']
const area = [100, 105, 110, 115, 120, 125, 130]
let merge = countries.map(function (c) {
area.map(function (a) {
return c + a
// This returns an array of length 3 (prints country + all areas into one array position)
// However if I create a third array and use push(c + a) here instead then length is 21 (which is what I am trying to achieve).
}) …Run Code Online (Sandbox Code Playgroud) asp.net-core ×2
c# ×2
arrays ×1
asp.net ×1
dictionary ×1
javascript ×1
middleware ×1
routing ×1
session ×1
zip ×1