首页 > 解决方案 > 保护 .NET Core MVC 网站免受暴力登录尝试(通过内置方式)

问题描述

我正在使用OWasp 安全备忘单来检查/加强我创建的 .net 核心网站的安全性。

A2 节有一部分是关于防止暴力登录尝试的。

它指出一个解决方案是在登录操作上添加以下属性(但它适用于 MVC5)

[AllowXRequestsEveryXSecondsAttribute(
  Name = "LogOn",
  Message = "You have performed this action more than {x} times in the last {n} seconds.",
  Requests = 3,
  Seconds = 60)]

.net 核心脚手架将登录部分创建为一个page而不是一个 MVC 控制器,我似乎无法访问该属性(或制定一个类似的)。

显然,我可以使用诸如thisthis之类的帖子来推出我自己的解决方案,但我宁愿使用可用的标准工具。

如果可以的话,我宁愿不使用锁定功能,因为这会增加支持。

.NET Core 是否有我可以使用的内置功能?

标签: securityauthentication.net-coreasp.net-core-mvc

解决方案


几个月后以新的眼光回到这个问题,发现微软的一篇帖子,其中包括用于防止暴力攻击的帐户锁定,这将是一个安全层,虽然不像旧的 MVC5AllowXRequestsEveryXSecondsAttribute启用的那样提供全面的保护。

它会阻止针对一封电子邮件的多次尝试,但不会阻止针对多封电子邮件的多次尝试。

即它会阻止某人做

  • 用户名:user1@test.com,密码:Password121
  • 用户名:user1@test.com,密码:Password122
  • 用户名:user1@test.com,密码:Password123

但这不会阻止某人这样做:

  • 用户名:user1@test.com,密码:Password123
  • 用户名:user2@test.com,密码:Password123
  • 用户名:user3@test.com,密码:Password123

这是来自 Microsoft 的代码摘录(它们是关键options.Lockout部分):

public void ConfigureServices(IServiceCollection services)
{
    // Add framework services.
    services.AddDbContext<ApplicationDbContext>(options =>
        options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));

    services.AddIdentity<ApplicationUser, IdentityRole>()
        .AddEntityFrameworkStores<ApplicationDbContext>()
        .AddDefaultTokenProviders();

    services.AddMvc();

    services.Configure<IdentityOptions>(options =>
    {
        options.Lockout.MaxFailedAccessAttempts = 10;
        options.Lockout.DefaultLockoutTimeSpan = TimeSpan.FromMinutes(10);
    });

    // Add application services.
    services.AddTransient<IEmailSender, AuthMessageSender>();
    services.AddTransient<ISmsSender, AuthMessageSender>();
    services.Configure<SMSoptions>(Configuration);
}

更新(与核心团队讨论后)

我在 Github 上向 .NET Core 团队提出了这个问题。

AllowXRequestsEveryXSecondsAttributeOWasp 检查表中提到的属性来自外部库,他们认为可以将其转换为 .NET Core,但他们并不认为将某些内容作为框架的一部分包含在内是优先事项。他们的建议是研究 Azure 解决方案“AAD 或 B2C”或“Auth0”。


推荐阅读