Membership.Providers 属性

定义

获取一个用于 ASP.NET 应用程序的成员资格提供程序的集合。Gets a collection of the membership providers for the ASP.NET application.

public:
 static property System::Web::Security::MembershipProviderCollection ^ Providers { System::Web::Security::MembershipProviderCollection ^ get(); };
public static System.Web.Security.MembershipProviderCollection Providers { get; }
member this.Providers : System.Web.Security.MembershipProviderCollection
Public Shared ReadOnly Property Providers As MembershipProviderCollection

属性值

一个为 ASP.NET 应用程序配置的成员资格提供程序的 MembershipProviderCollectionA MembershipProviderCollection of the membership providers configured for the ASP.NET application.

示例

下面的代码示例列出了为应用程序以及其各自的类型启用的提供程序。The following code example lists the providers enabled for an application and their respective types.

<%@ Page Language="C#" %>
<%@ Import Namespace="System.Web.Security" %>
<%@ Import Namespace="System.Configuration.Provider" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
<title>List Enabled Providers</title>
</head>
<body>

<%
foreach (ProviderBase p in Membership.Providers)
  Response.Write(p.Name + ", " + p.GetType() + "<br />");
%>

</body>
</html>
<%@ Page Language="VB" %>
<%@ Import Namespace="System.Web.Security" %>
<%@ Import Namespace="System.Configuration.Provider" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
<title>List Enabled Providers</title>
</head>
<body>

<%
For Each p As ProviderBase In Membership.Providers
  Response.Write(p.Name & ", " & p.GetType().ToString() & "<br />")
Next
%>

</body>
</html>

注解

Providers属性引用为应用程序启用的所有成员资格提供程序,包括添加到应用程序的 web.config 文件中的提供程序和所有应用程序的 machine.config 文件。The Providers property references all of the membership providers enabled for an application, including providers added in the Web.config file for the application and the Machine.config file for all applications. 你可以使用应用程序配置中成员资格部分的 "提供程序" 元素来控制哪些成员资格提供程序可用于应用程序。You can control which membership providers are available for an application using the providers element of the membership section in the configuration for your application. 例如,下面的示例显示了一个应用程序的 web.config 文件中的成员资格部分,该应用程序SqlMembershipProvider移除在AspNetSqlProvider计算机配置文件中指定的实例(), SqlMembershipProvider并将一个名为SqlProvider作为应用程序的默认成员资格提供程序。For example, the following sample shows the membership section in the Web.config file for an application that removes the SqlMembershipProvider instance (AspNetSqlProvider) specified in the machine configuration file and adds a SqlMembershipProvider instance named SqlProvider as the default membership provider for the application.

<configuration>  
  <connectionStrings>  
    <add name="SqlServices" connectionString="Data Source=MySqlServer;Integrated Security=SSPI;Initial Catalog=aspnetdb;" />  
  </connectionStrings>  
  <system.web>  
    <membership defaultProvider="SqlProvider" userIsOnlineTimeWindow="20">  
      <providers>  
        <remove name="AspNetSqlProvider" />  
          <add name="SqlProvider"  
            type="System.Web.Security.SqlMembershipProvider"  
            connectionStringName="SqlServices"  
            enablePasswordRetrieval="false"  
            enablePasswordReset="true"  
            requiresQuestionAndAnswer="true"  
            passwordFormat="Hashed"  
            applicationName="/" />  
      </providers>  
    </membership>  
  </system.web>  
</configuration>  

指定成员资格元素(ASP.NET 设置架构)部分时,必须指定defaultProvider属性。When specifying the membership Element (ASP.NET Settings Schema) section, you must specify the defaultProvider attribute. 如果未在 web.config 中指定成员身份元素(ASP.NET 设置架构)部分,则将使用计算机配置中的值,并SqlMembershipProvider将名AspNetSqlProvider为的实例建立为defaultProviderIf you do not specify a membership Element (ASP.NET Settings Schema) section in your Web.config, the values from the machine configuration are used and the SqlMembershipProvider instance named AspNetSqlProvider is established as the defaultProvider.

可以通过按名称为成员资格提供程序编制索引并Providers将其强制转换为所需的类型,从集合中获取对提供程序的强类型引用。You can obtain a strongly typed reference to a provider from the Providers collection by indexing the membership provider by name and casting it as the desired type.

适用于

另请参阅