如何使用MembershipValidatePasswordEventHandler?

发布于 2024-12-25 19:09:26 字数 269 浏览 3 评论 0原文

在使用 asp 创建新用户时,有什么方法可以对密码选择进行一些限制吗?网会员。?
条件如
密码强度:

  • 至少 8 个字符,并强制执行字符复杂性。即: 不包含用户的帐户名
  • 长度至少为八个字符 包含
  • 以下四个类别中三个类别的字符:

英文大写字符(A 到 Z)
英文小写字符(a 到 z)
10 基数(0 到 9)
非字母字符(例如 !、$、#、%)

Is there any way to put some restriction on password selection while creating new user with asp. net Membership.??

Condition Like
Password strength:

  • Minimum of 8 characters, with character complexity enforced. ie: Not contain the user's account name
  • Be at least eight characters in length
  • Contain characters from three of the following four categories:

English uppercase characters (A through Z)
English lowercase characters (a through z)
Base 10 digits (0 through 9)
Non-alphabetic characters (for example, !, $, #, %)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

中性美 2025-01-01 19:09:26

在这种情况下,您需要对 Web.config 进行一些修改。找到以下部分并进行相应更改

<membership>
  <providers>
    <clear/>
    <add name="AspNetSqlMembershipProvider" type="System.Web.Security.SqlMembershipProvider" connectionStringName="ApplicationServices"
         enablePasswordRetrieval="false" enablePasswordReset="true" requiresQuestionAndAnswer="false" requiresUniqueEmail="false"
         maxInvalidPasswordAttempts="5" minRequiredPasswordLength="6" minRequiredNonalphanumericCharacters="0" passwordAttemptWindow="10"
         applicationName="/" />
  </providers>
</membership>

更新

首先,更新 web.config 中的会员配置部分,如下所示

  <membership>
  <providers>
    <clear/>
    <add name="AspNetSqlMembershipProvider" type="System.Web.Security.SqlMembershipProvider" connectionStringName="ApplicationServices"
         enablePasswordRetrieval="false" enablePasswordReset="true" passwordAttemptWindow="10"
         applicationName="/" />
  </providers>
</membership>

然后,在您的帐户控制器中,替换您的注册操作(用 [HttpPost 装饰的操作) ])使用以下代码

[HttpPost]
public ActionResult Register(RegisterModel model)
{
    Membership.ValidatingPassword += new MembershipValidatePasswordEventHandler(OnValidatePassword);
    if (ModelState.IsValid)
    {
        // Attempt to register the user
        MembershipCreateStatus createStatus;
        Membership.CreateUser(model.UserName, model.Password, model.Email, null, null, true, null, out createStatus);

        if (createStatus == MembershipCreateStatus.Success)
        {
            FormsAuthentication.SetAuthCookie(model.UserName, false /* createPersistentCookie */);
            return RedirectToAction("Index", "Home");
        }
        else
        {
            ModelState.AddModelError("", ErrorCodeToString(createStatus));
        }
    }

    // If we got this far, something failed, redisplay form
    return View(model);
 }

最后在注册操作下方添加 OnValidatePassword 的代码

public void OnValidatePassword(object sender,
                          ValidatePasswordEventArgs args)
{
    System.Text.RegularExpressions.Regex r =
          new System.Text.RegularExpressions.Regex(@"(?=.{8,})(?=(.*\d){1,})(?=(.*\W){1,})");

    if (!r.IsMatch(args.Password))
    {
        args.FailureInformation =
              new HttpException("Password must be at least 8 characters long and " +
                                "contain at least one number and one special character.");
        args.Cancel = true;
    }
}

希望这可以帮助您实现您想要的!

In this case, you need to make some modifications to your Web.config. Find the following section and change it accordingly

<membership>
  <providers>
    <clear/>
    <add name="AspNetSqlMembershipProvider" type="System.Web.Security.SqlMembershipProvider" connectionStringName="ApplicationServices"
         enablePasswordRetrieval="false" enablePasswordReset="true" requiresQuestionAndAnswer="false" requiresUniqueEmail="false"
         maxInvalidPasswordAttempts="5" minRequiredPasswordLength="6" minRequiredNonalphanumericCharacters="0" passwordAttemptWindow="10"
         applicationName="/" />
  </providers>
</membership>

UPDATED

First, update your membership config section in web.config like following

  <membership>
  <providers>
    <clear/>
    <add name="AspNetSqlMembershipProvider" type="System.Web.Security.SqlMembershipProvider" connectionStringName="ApplicationServices"
         enablePasswordRetrieval="false" enablePasswordReset="true" passwordAttemptWindow="10"
         applicationName="/" />
  </providers>
</membership>

Then, In your Account Controller, replace your Registration action(the one decorated with [HttpPost]) with the following code

[HttpPost]
public ActionResult Register(RegisterModel model)
{
    Membership.ValidatingPassword += new MembershipValidatePasswordEventHandler(OnValidatePassword);
    if (ModelState.IsValid)
    {
        // Attempt to register the user
        MembershipCreateStatus createStatus;
        Membership.CreateUser(model.UserName, model.Password, model.Email, null, null, true, null, out createStatus);

        if (createStatus == MembershipCreateStatus.Success)
        {
            FormsAuthentication.SetAuthCookie(model.UserName, false /* createPersistentCookie */);
            return RedirectToAction("Index", "Home");
        }
        else
        {
            ModelState.AddModelError("", ErrorCodeToString(createStatus));
        }
    }

    // If we got this far, something failed, redisplay form
    return View(model);
 }

And finally add the code for OnValidatePassword below the Registration Action

public void OnValidatePassword(object sender,
                          ValidatePasswordEventArgs args)
{
    System.Text.RegularExpressions.Regex r =
          new System.Text.RegularExpressions.Regex(@"(?=.{8,})(?=(.*\d){1,})(?=(.*\W){1,})");

    if (!r.IsMatch(args.Password))
    {
        args.FailureInformation =
              new HttpException("Password must be at least 8 characters long and " +
                                "contain at least one number and one special character.");
        args.Cancel = true;
    }
}

Hope this can help you achieve what you want !!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文