ASP .Net 会员资格(控制在哪里从配置窗口创建新用户?)

发布于 2024-12-26 01:17:15 字数 1173 浏览 2 评论 0原文

这是我的 Web.config 文件

<membership>
        <providers>
            <clear />
            <add name="AspNetSqlMembershipProvider" type="System.Web.Security.SqlMembershipProvider" connectionStringName="ApplicationServices" maxInvalidPasswordAttempts="5" minRequiredPasswordLength="8"  minRequiredNonalphanumericCharacters="0" passwordStrengthRegularExpression="^(?=.*[a-z])(?=.*[A-Z])(?=.*[0-9])|(?=.*[a-z])(?=.*[A-Z])(?=.*[!%,.;:])|(?=.*[a-z])(?=.*[0-9])(?=.*[!%,.;:])|(?=.*[A-Z])(?=.*[0-9])(?=.*[!%,.;:])$" passwordAttemptWindow="10" applicationName="/" />
        </providers>
    </membership>


在这里我想用几个条件限制新用户密码::


密码强度:

  1. 包含以下四个类别中三个类别的字符: 英文大写字符(A 到 Z)
    英文小写字符(a 到 z)
    10 基数(0 到 9)
    非字母字符(例如 !、$、#、%)
    -
  2. 密码不包含用户的帐户名


正如您所看到的,我在 web.config 部分中使用了 passwordStrengthRegularExpression 属性来匹配第一个条件(会员提供的)。
需要解决方案来实现第二个条件。


简而言之:: 我需要自定义成员资格以匹配条件(强制执行字符复杂性。即:密码不包含用户的帐户名)

更新::
我按照埃兰加在我的申请中建议的去做了。现在我还想在会员创建用户失败时显示适当的消息(在几个自定义条件下)。

Here is my Web.config file

<membership>
        <providers>
            <clear />
            <add name="AspNetSqlMembershipProvider" type="System.Web.Security.SqlMembershipProvider" connectionStringName="ApplicationServices" maxInvalidPasswordAttempts="5" minRequiredPasswordLength="8"  minRequiredNonalphanumericCharacters="0" passwordStrengthRegularExpression="^(?=.*[a-z])(?=.*[A-Z])(?=.*[0-9])|(?=.*[a-z])(?=.*[A-Z])(?=.*[!%,.;:])|(?=.*[a-z])(?=.*[0-9])(?=.*[!%,.;:])|(?=.*[A-Z])(?=.*[0-9])(?=.*[!%,.;:])$" passwordAttemptWindow="10" applicationName="/" />
        </providers>
    </membership>

Here i want to restrict new user password with few conditions::

Password strength:

  1. 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, !, $, #, %)
    -
  2. password Not contain the user's account name

As you can see there is passwordStrengthRegularExpression attribute i used in web.config section to match 1st condition( that Membership provide).
Need solution to achieve second condition.

In short:: I need to customize membership to match condition(character complexity enforced. ie:password Not contain the user's account name)

Updated::

I did what Eranga suggested me in my application . Now i also want to Display appropriate message when Membership createUser fails(on several customized condition).

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

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

发布评论

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

评论(2

忘你却要生生世世 2025-01-02 01:17:15

您可以从默认提供程序进行子类化并处理 ValidatingPassword 事件。如果验证不符合您的第二个条件,您可以在此处取消验证。

public class MyMembershipProvider : SqlMembershipProvider 
{
    public MyMembershipProvider()
    {
        ValidatingPassword += ValidatePassword;
    }

    void ValidatePassword(object sender, ValidatePasswordEventArgs e)
    {
         if (e.Password.Contains(e.UserName))
         {
             e.Cancel = true;
             e.FailureInformation = new Exception("blah blah");
         }
    }
}

您的 Web.Config 文件应包含以下内容。确保为 type 属性提供自定义会员资格类别的全名。

   <membership>
        <providers>
            <clear />
            <add name="MyMembershipProvider" type="MyNamespace.MyMembershipProvider" connectionStringName="ApplicationServices" maxInvalidPasswordAttempts="5" minRequiredPasswordLength="8"  minRequiredNonalphanumericCharacters="0" passwordStrengthRegularExpression="^(?=.*[a-z])(?=.*[A-Z])(?=.*[0-9])|(?=.*[a-z])(?=.*[A-Z])(?=.*[!%,.;:])|(?=.*[a-z])(?=.*[0-9])(?=.*[!%,.;:])|(?=.*[A-Z])(?=.*[0-9])(?=.*[!%,.;:])$" passwordAttemptWindow="10" applicationName="/" />
        </providers>
    </membership>

You can sub class from your default provider and handle the ValidatingPassword event. There you can cancel the validation if it does not meet your second criteria.

public class MyMembershipProvider : SqlMembershipProvider 
{
    public MyMembershipProvider()
    {
        ValidatingPassword += ValidatePassword;
    }

    void ValidatePassword(object sender, ValidatePasswordEventArgs e)
    {
         if (e.Password.Contains(e.UserName))
         {
             e.Cancel = true;
             e.FailureInformation = new Exception("blah blah");
         }
    }
}

Your Web.Config file should include the following. Make sure you give the full name of your custom membership class for the type attribute.

   <membership>
        <providers>
            <clear />
            <add name="MyMembershipProvider" type="MyNamespace.MyMembershipProvider" connectionStringName="ApplicationServices" maxInvalidPasswordAttempts="5" minRequiredPasswordLength="8"  minRequiredNonalphanumericCharacters="0" passwordStrengthRegularExpression="^(?=.*[a-z])(?=.*[A-Z])(?=.*[0-9])|(?=.*[a-z])(?=.*[A-Z])(?=.*[!%,.;:])|(?=.*[a-z])(?=.*[0-9])(?=.*[!%,.;:])|(?=.*[A-Z])(?=.*[0-9])(?=.*[!%,.;:])$" passwordAttemptWindow="10" applicationName="/" />
        </providers>
    </membership>
最好是你 2025-01-02 01:17:15

它不是 MVC,但您可以查看这个覆盖静态事件的示例。另外,由于这基本上与您之前问过的问题相同,所以有更多细节,也许您可以更新原来的问题?

It's not MVC, but you can look at this example of overriding the static event. Also, since this is basically the same question you asked before, with a little bit more detail, perhaps you could have just updated the original question?

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