从 web.config 读取会员资格部分

发布于 2024-09-05 03:15:16 字数 935 浏览 3 评论 0原文

我创建了一个自定义 MembershipProvider 类,到目前为止一切顺利,但是,我不确定如何从 web.config 文件中读取配置设置。

我尝试从 Google 和 Stackoverflow 进行搜索,似乎有人也遇到了我的问题 问题并询问,但没有给出答案。

这应该是一件简单的事情,但我在 Web 开发方面非常陌生,因此从 web.config 读取设置对我来说似乎太技术性了。

这是我的设置:

 <membership defaultProvider="CustomMembershipProvider">
  <providers>
    <clear/>
    <add name="CustomMembershipProvider"
         type="Test.Models.CustomMembershipProvider,Test"
         passwordFormat="Hashed"
         connectionStringName="ApplicationServices"
         minRequiredPasswordLength="8"
         minRequiredNonalphanumericCharacters="0"
         maxInvalidPasswordAttempts="5"
         enablePasswordReset="false"
         enablePasswordRetrieval="false"
         requiresQuestionAndAnswer="false"
         applicationName="/"/>
  </providers>
</membership>

我想阅读 minRequiredPasswordLength 设置,请协助。

I have created a custom MembershipProvider class, so far so good, however, I am not sure how to read the configuration settings from the web.config file.

I tried to search from Google and Stackoverflow, seems like someone also experiencing my
problem and asked, but no answer has been given.

It should be a simple thing, but I am very new in web development, so reading settings from web.config seems away too technical to me.

Here is my settings:

 <membership defaultProvider="CustomMembershipProvider">
  <providers>
    <clear/>
    <add name="CustomMembershipProvider"
         type="Test.Models.CustomMembershipProvider,Test"
         passwordFormat="Hashed"
         connectionStringName="ApplicationServices"
         minRequiredPasswordLength="8"
         minRequiredNonalphanumericCharacters="0"
         maxInvalidPasswordAttempts="5"
         enablePasswordReset="false"
         enablePasswordRetrieval="false"
         requiresQuestionAndAnswer="false"
         applicationName="/"/>
  </providers>
</membership>

I would like to read the minRequiredPasswordLength setting, please assist.

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

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

发布评论

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

评论(3

魔法唧唧 2024-09-12 03:15:16

这是代码中的解决方案:

        MembershipSection membershipSection = (MembershipSection)WebConfigurationManager.GetSection("system.web/membership");
        string defaultProvider = membershipSection.DefaultProvider;
        ProviderSettings providerSettings = membershipSection.Providers[defaultProvider];
        string connectionStringName = providerSettings.Parameters["connectionStringName"];
        string connectionUsername = providerSettings.Parameters["connectionUsername"];
        string connectionPassword = providerSettings.Parameters["connectionPassword"];
        string connectionString = WebConfigurationManager.ConnectionStrings[connectionStringName].ConnectionString;

here is the solution in code :

        MembershipSection membershipSection = (MembershipSection)WebConfigurationManager.GetSection("system.web/membership");
        string defaultProvider = membershipSection.DefaultProvider;
        ProviderSettings providerSettings = membershipSection.Providers[defaultProvider];
        string connectionStringName = providerSettings.Parameters["connectionStringName"];
        string connectionUsername = providerSettings.Parameters["connectionUsername"];
        string connectionPassword = providerSettings.Parameters["connectionPassword"];
        string connectionString = WebConfigurationManager.ConnectionStrings[connectionStringName].ConnectionString;
骑趴 2024-09-12 03:15:16

由于这被设置为您的默认提供程序,因此应该足以:

int i = Membership.MinRequiredPasswordLength;

并且这将返回一个 int 指定所需的最小密码长度。

As this is set as your default provider it should be enough to:

int i = Membership.MinRequiredPasswordLength;

And that would return an int specifying the minimum required password length.

江湖彼岸 2024-09-12 03:15:16

如果您在自己的类中重写了 System.Web.Security.MembershipProvider ,则只需调用 System.Web.Security.Membership 即可按照 Robban 的建议获取 web.config 成员资格设置方法。但是,这些调用将定向到您的成员资格提供程序类,因此您需要提供一些实现。

假设您已重写 MembershipProvider 类并在配置文件中添加了一个部分,如上面的原始问题所示。对 int i = Membership.MinRequiredPasswordLength 的调用将定向到您的实现。这可能看起来像这样:

   public override int MinRequiredPasswordLength
    {
        get { return _minRequiredPasswordLength; }
    }

MSDN 在此处提供了一个完整的示例。该示例向您展示如何读取配置文件以设置本地属性,例如 _minRequiredPasswordLength

If you have overridden the System.Web.Security.MembershipProvider in your own class you can get the web.config membership settings as Robban suggests, just by calling the System.Web.Security.Membership methods. However, these calls will be directed to your membership provider class, so you will need to provide some implementation.

Supposing you have overridden the MembershipProvider class and added a section in the config file, as in the original question above. A call to int i = Membership.MinRequiredPasswordLength will be directed to YOUR implementation. This might look like this:

   public override int MinRequiredPasswordLength
    {
        get { return _minRequiredPasswordLength; }
    }

MSDN gives a complete example here. The example shows you how to read the config file to set the local properties like _minRequiredPasswordLength.

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