asp.net - 使用 ASP.NET 成员身份将 2FA 添加到 .NET Web 应用程序

标签 asp.net authentication webforms asp.net-membership two-factor-authentication

谁能告诉我有关使用旧的 .NET 成员(member)系统实现双因素身份验证的任何教程?

我有一个遗留的 Web 表单应用程序,我想将 2FA 添加到其中,但我找到的所有教程都是关于在较新的 ASP.NET Identity 系统中实现它的。

不幸的是,目前无法从成员(member)系统升级到身份系统。

最佳答案

您将需要编写自定义成员资格提供程序并找到一种将多个凭据传递给 MembershipProvider 的方法,可以作为额外参数或使用新方法。由于 MembershipProvider 仅在每个应用程序域中创建一次,并且可以处理多个用户请求,因此最好不要在验证密码和第二个因素之间拆分调用。

我想出的解决方案实现了一个额外的接口(interface),并且在验证传递回服务器的凭据时检查提供者是否支持此支持。

首先,定义一个接受多种因素(例如密码和OTP)的接口(interface)

public interface I2FAMembershipProvider
{
    bool ValidateUser2FA(string username, string firstFactor, string secondFactor);
}

然后在现有的 MembershipProvider 中实现这个附加接口(interface)

public sealed class SampleProvider: MembershipProvider, I2FAMembershipProvider
{
    //Validate 2FA requests with both factors simultaneously
    public bool ValidateUser2FA(string username, string firstFactor, string secondFactor)
    {
        //... implementation here
    }

    //Traditional single factor authentication
    public override bool ValidateUser(string username, string passcode)
    {
        //... implementation here
    }

最后,在将凭据回发到服务器时验证凭据时,检查提供者是否支持此接口(interface):

if (Membership.Provider is I2FAMembershipProvider)
{
    //Validation using enhanced interface
    if (((I2FAMembershipProvider) Membership.Provider).ValidateUser2FA(username, password, passcode))
    {
        FormsAuthentication.RedirectFromLoginPage(username, false);
    }
}
else
{
    //Traditional membership provider validation
    if (Membership.ValidateUser(username, password))
    {
        FormsAuthentication.RedirectFromLoginPage(username, false);
    }
}

关于asp.net - 使用 ASP.NET 成员身份将 2FA 添加到 .NET Web 应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51510757/

相关文章:

c# - 为什么 Redis C# 客户端方法 .getById() 返回 null?

c# - 为什么我们需要私有(private)构造函数?

authentication - IIS/冷融合 : authentication request on anonymous content

c# 存储用于网页的字符串的最佳位置

c# - 插入新记录时 MySQL 的 Subsonic 3.0.0.4 ActiveRecord 模板错误

asp.net - IIS 重写规则将 https 重定向到 http 不起作用

java - 当用户登录时,它会导航到主页

php - 最容易配置为在现有 CMS 中工作的 PHP 论坛?

c# - ASP.NET Web 应用程序审计日志

javascript - AngularJS 将数据发布到 ASP.NET WebForms