asp.net - 根据角色要求某些用户使用更强的密码

标签 asp.net asp.net-mvc-3 security asp.net-membership authorization

我有一个 MVC 3 应用程序。关于安全,主要有两个区域。第一个主要是为了防止公众访问,但不是真正的敏感信息。密码强度可能很弱,因为这样做也没有太大的危害。

第二区(Area)被限制。用户必须申请访问。如果用户获得访问权限,它就会获得某个角色。因此,每个 Controller 方法都会根据该角色对用户进行自动授权。

我希望这些用户必须在下次登录时将密码更改为强密码,然后才能进一步访问受限制的内容。

示例:

User A applies for access. Access is granted. The password policy for that user is changed as long as it has access. They MUST change their password on the next logon, and they cannot change back to a weaker password as long as they have that role.



有没有什么安全的方法可以使用 ASP.NET 实现这一点?

更新

我实际上使用了 Chris 提出的解决方案并且它有效,但是为了处理密码本身的验证,我实际上也从 Micah 提出的解决方案中获得了一些灵感。然而,事实证明,覆盖 MembershipProvider.OnValidatingPassword 确实意味着还必须实现 10 多个抽象方法,我真的不需要解决这个问题。

在我看来,更好的解决方案是使用 Membership.ValidatingPassword 事件。我在 App_Start 中执行此操作,然后在事件处理程序中实现我自己的密码验证并解决了我的问题。

只是为了与您分享解决方案,我将其展示在此处,与 Chris 解决方案一起解决了我的问题,并希望对其他人也有帮助:
    void App_Start()
    {
        //To do custom validation on certain passwords set new event handler
        Membership.ValidatingPassword += Membership_ValidatingPassword;
    }

private void Membership_ValidatingPassword(object sender, ValidatePasswordEventArgs e)
    {
        //If the user is a new user, we let registration happen without strong password
        if (e.IsNewUser) return;


        MembershipUser membershipUser = Membership.GetUser(e.UserName);
        Guid userId = Guid.Parse(membershipUser.ProviderUserKey.ToString());

        //First check if the pwd is strong enough to be flagged, if so we flag it
        //using regex to validate the password (20 char, 2 uppercase so on)
        if (MyValidationClass.IsStrongPassword(e.Password, 20, 2, 4, 1))
        {
            //if the user does not already have a flag we set one
            MyValidationClass.SetStrongPasswordFlag(userId);
        }
        else
        {
            //If the user needs strong pwd, we cancel the operation and throw exception
            if (MyValidationClass.NeedsStrongPassword(e.UserName))
            {
                e.FailureInformation =
                    new MembershipPasswordException("Password does not satisfy reqirements!");
                e.Cancel = true;
            }
            else
            {
                MyValidationClass.RemoveStrongPasswordFlag(userId);
            }
        }
    }

最佳答案

您可以编写自己的授权属性来容纳两者。您只需要在应用程序的相关部分使用它:

例如:

public class HasChangedPasswordAttribute : AuthorizeAttribute
{      
    protected override bool AuthorizeCore(HttpContextBase httpContext)
    {
        UserRepository repo = new UserRepository();
        var user = repo.GetCurrentUser();
        bool hasSecurelyChangedPassword = user.HasSecurelyChangedPassword;
        return hasSecurelyChangedPassword;
    }

    protected override void HandleUnauthorizedRequest(AuthorizationContext filterContext)
    {
        filterContext.Result = new RedirectResult("/Account/ChangePassword");
    }
}

以上将检查用户是否安全地更改了他们的密码。如果不是,它会将他们重定向到一个新页面来更改他们的密码。一旦他们更改了它,将标志设置为已更改。

然后你可以像这样使用它:
[HasChangedPassword]
[Authorize(Roles="SuperRole")]
public ActionResult MySecureAction()
{
 ...
}

您显然可以将这两个属性合二为一,但为了显示示例,它们在上面是分开的。

关于asp.net - 根据角色要求某些用户使用更强的密码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9414143/

相关文章:

mysql - 当用户将项目添加到数据库时通知

security - 带有分析的 Grails 菜单

php - 如何验证服务器调用是从应用程序发出的?

asp.net - NLog 不记录日志

c# - 为什么在绑定(bind) asp :net Repeater to a Collection? 时,ItemDataBound 事件上的 e.Item.DataItem 为空

asp.net-mvc - 让多个 ASP.NET MVC3 项目共享一个通用布局

asp.net-mvc-3 - MVC3异常处理和customErrors问题

asp.net-mvc - 表单发布后如何保存 ViewBag 数据?

php - 如何防止 PHP 中的 SQL 注入(inject)?

asp.net - JavaScript 中的 ActiveXobject 错误