小编use*_*479的帖子

ASP .NET Core 3.1 MVC 中特定路由的自定义中间件(或授权)

在我的 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 区域的自定义中间件?

c# asp.net routing middleware asp.net-core

8
推荐指数
2
解决办法
1万
查看次数

App.UseSession() vs App.UseAuthentication(CookieAuthenticationDefaults.AuthenticationScheme) vs App.UseCookiePolicy()

如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    });\n
Run Code Online (Sandbox Code Playgroud)\n\n

并在 Startup.Configure 中

\n\n
App.UseSession()\n
Run Code Online (Sandbox Code Playgroud)\n\n

还可以通过身份验证中间件使用没有身份的 cookie 身份验证,如此处所述https://learn.microsoft.com/en-us/aspnet/core/security/authentication/cookie?view=aspnetcore-3.1

\n\n
services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme)\n    .AddCookie(options =>\n    {\n        ...\n    });\n
Run Code Online (Sandbox Code Playgroud)\n\n

我的问题是,如果我在 Startup.Configure 中同时使用

\n\n
App.UseSession()\nApp.UseAuthentication()\n
Run 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\n
App.UseCookiePolicy()\n
Run Code Online (Sandbox Code Playgroud)\n\n

预先感谢您的任何答复!

\n

c# session session-cookies asp.net-core

6
推荐指数
0
解决办法
3328
查看次数

在 JavaScript 中使用 map() 合并(压缩)两个不同长度的数组

我试图围绕 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)

javascript arrays zip dictionary

2
推荐指数
1
解决办法
1197
查看次数