SqlMembershipProvider Class (System.Web.Security)

Switch View :
ScriptFree
.NET Framework Class Library
SqlMembershipProvider Class

Manages storage of membership information for an ASP.NET application in a SQL Server database.

Inheritance Hierarchy

System.Object
  System.Configuration.Provider.ProviderBase
    System.Web.Security.MembershipProvider
      System.Web.Security.SqlMembershipProvider

Namespace:  System.Web.Security
Assembly:  System.Web (in System.Web.dll)
Syntax

Visual Basic
Public Class SqlMembershipProvider _
	Inherits MembershipProvider
C#
public class SqlMembershipProvider : MembershipProvider
Visual C++
public ref class SqlMembershipProvider : public MembershipProvider
F#
type SqlMembershipProvider =  
    class
        inherit MembershipProvider
    end

The SqlMembershipProvider type exposes the following members.

Constructors

  Name Description
Public method SqlMembershipProvider Initializes a new instance of the SqlMembershipProvider class.
Top
Properties

  Name Description
Public property ApplicationName Gets or sets the name of the application to store and retrieve membership information for. (Overrides MembershipProvider.ApplicationName.)
Public property Description Gets a brief, friendly description suitable for display in administrative tools or other user interfaces (UIs). (Inherited from ProviderBase.)
Public property EnablePasswordReset Gets a value indicating whether the SQL Server membership provider is configured to allow users to reset their passwords. (Overrides MembershipProvider.EnablePasswordReset.)
Public property EnablePasswordRetrieval Gets a value indicating whether the SQL Server membership provider is configured to allow users to retrieve their passwords. (Overrides MembershipProvider.EnablePasswordRetrieval.)
Public property MaxInvalidPasswordAttempts Gets the number of invalid password or password-answer attempts allowed before the membership user is locked out. (Overrides MembershipProvider.MaxInvalidPasswordAttempts.)
Public property MinRequiredNonAlphanumericCharacters Gets the minimum number of special characters that must be present in a valid password. (Overrides MembershipProvider.MinRequiredNonAlphanumericCharacters.)
Public property MinRequiredPasswordLength Gets the minimum length required for a password. (Overrides MembershipProvider.MinRequiredPasswordLength.)
Public property Name Gets the friendly name used to refer to the provider during configuration. (Inherited from ProviderBase.)
Public property PasswordAttemptWindow Gets the time window between which consecutive failed attempts to provide a valid password or password answers are tracked. (Overrides MembershipProvider.PasswordAttemptWindow.)
Public property PasswordFormat Gets a value indicating the format for storing passwords in the SQL Server membership database. (Overrides MembershipProvider.PasswordFormat.)
Public property PasswordStrengthRegularExpression Gets the regular expression used to evaluate a password. (Overrides MembershipProvider.PasswordStrengthRegularExpression.)
Public property RequiresQuestionAndAnswer Gets a value indicating whether the SQL Server membership provider is configured to require the user to answer a password question for password reset and retrieval. (Overrides MembershipProvider.RequiresQuestionAndAnswer.)
Public property RequiresUniqueEmail Gets a value indicating whether the SQL Server membership provider is configured to require a unique e-mail address for each user name. (Overrides MembershipProvider.RequiresUniqueEmail.)
Top
Methods

  Name Description
Public method ChangePassword Modifies a user's password. (Overrides MembershipProvider.ChangePassword(String, String, String).)
Public method ChangePasswordQuestionAndAnswer Updates the password question and answer for a user in the SQL Server membership database. (Overrides MembershipProvider.ChangePasswordQuestionAndAnswer(String, String, String, String).)
Public method CreateUser Adds a new user to the SQL Server membership database. (Overrides MembershipProvider.CreateUser(String, String, String, String, String, Boolean, Object, MembershipCreateStatus).)
Protected method DecryptPassword Decrypts an encrypted password. (Inherited from MembershipProvider.)
Public method DeleteUser Removes a user's membership information from the SQL Server membership database. (Overrides MembershipProvider.DeleteUser(String, Boolean).)
Protected method EncryptPassword(Byte[]) Encrypts a password. (Inherited from MembershipProvider.)
Protected method EncryptPassword(Byte[], MembershipPasswordCompatibilityMode) Encrypts the specified password using the specified password-compatibility mode. (Inherited from MembershipProvider.)
Public method Equals(Object) Determines whether the specified Object is equal to the current Object. (Inherited from Object.)
Protected method Finalize Allows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection. (Inherited from Object.)
Public method FindUsersByEmail Returns a collection of membership users for which the e-mail address field contains the specified e-mail address. (Overrides MembershipProvider.FindUsersByEmail(String, Int32, Int32, Int32).)
Public method FindUsersByName Gets a collection of membership users where the user name contains the specified user name to match. (Overrides MembershipProvider.FindUsersByName(String, Int32, Int32, Int32).)
Public method GeneratePassword Generates a random password that is at least 14 characters long.
Public method GetAllUsers Gets a collection of all the users in the SQL Server membership database. (Overrides MembershipProvider.GetAllUsers(Int32, Int32, Int32).)
Public method GetHashCode Serves as a hash function for a particular type. (Inherited from Object.)
Public method GetNumberOfUsersOnline Returns the number of users currently accessing the application. (Overrides MembershipProvider.GetNumberOfUsersOnline().)
Public method GetPassword Returns the password for the specified user name from the SQL Server membership database. (Overrides MembershipProvider.GetPassword(String, String).)
Public method GetType Gets the Type of the current instance. (Inherited from Object.)
Public method GetUser(Object, Boolean) Gets the information from the data source for the membership user associated with the specified unique identifier and updates the last activity date/time stamp for the user, if specified. (Overrides MembershipProvider.GetUser(Object, Boolean).)
Public method GetUser(String, Boolean) Returns information from the SQL Server membership database for a user and provides an option to update the last activity date/time stamp for the user. (Overrides MembershipProvider.GetUser(String, Boolean).)
Public method GetUserNameByEmail Gets the user name associated with the specified e-mail address. (Overrides MembershipProvider.GetUserNameByEmail(String).)
Public method Initialize Initializes the SQL Server membership provider with the property values specified in the ASP.NET application's configuration file. This method is not intended to be used directly from your code. (Overrides ProviderBase.Initialize(String, NameValueCollection).)
Protected method MemberwiseClone Creates a shallow copy of the current Object. (Inherited from Object.)
Protected method OnValidatingPassword Raises the ValidatingPassword event if an event handler has been defined. (Inherited from MembershipProvider.)
Public method ResetPassword Resets a user's password to a new, automatically generated password. (Overrides MembershipProvider.ResetPassword(String, String).)
Public method ToString Returns a string that represents the current object. (Inherited from Object.)
Public method UnlockUser Clears the user's locked-out status so that the membership user can be validated. (Overrides MembershipProvider.UnlockUser(String).)
Public method UpdateUser Updates information about a user in the SQL Server membership database. (Overrides MembershipProvider.UpdateUser(MembershipUser).)
Public method ValidateUser Verifies that the specified user name and password exist in the SQL Server membership database. (Overrides MembershipProvider.ValidateUser(String, String).)
Top
Events

  Name Description
Public event ValidatingPassword Occurs when a user is created, a password is changed, or a password is reset. (Inherited from MembershipProvider.)
Top
Remarks

This class is used by the Membership and MembershipUser classes to provide membership services for an ASP.NET application using a SQL Server database. You cannot use a SqlMembershipProvider without SQL Server. When your computer has SQL Server Express installed with the default instance name and user-instancing enabled, the SqlMembershipProvider object will create a database called aspnetdb in the application's App_Data directory the first time the application is run.

To manually create the database, run the Aspnet_regsql.exe executable found in the %systemroot%\Microsoft.NET\Framework\ versionNumber folder and specify the -A m option (for example aspnet_regsql.exe -A m). The database created is called Aspnetdb. Alternatively, run Aspnet_regsql.exe to pull up the GUI configuration mode and choose to configure all ASP.NET Features.

If the membership provider is configured with a connection string that uses integrated security, the process account of the ASP.NET application must have rights to connect to the SQL Server database.

The Machine.config file defines a default SqlMembershipProvider instance named AspNetSqlMembershipProvider that connects to the default SQL Server Express instance on the local machine. You can use this instance of the provider if you installed SQL Server Express with the default instance name, or you can define your own instance in the Web.config file for your ASP.NET application.

Examples

The following code example shows the Web.config file for an ASP.NET application configured to use a SqlMembershipProvider.

<configuration>
  <connectionStrings>
    <add name="SqlServices" connectionString="Data Source=localhost;Integrated Security=SSPI;Initial Catalog=aspnetdb;" />
  </connectionStrings>
  <system.web>
    <authentication mode="Forms" >
      <forms loginUrl="login.aspx"
        name=".ASPXFORMSAUTH" />
    </authentication>
    <authorization>
      <deny users="?" />
    </authorization>
    <membership defaultProvider="SqlProvider"
      userIsOnlineTimeWindow="15">
      <providers>
        <add 
          name="SqlProvider" 
          type="System.Web.Security.SqlMembershipProvider" 
          connectionStringName="SqlServices"
          applicationName="MyApplication"
          enablePasswordRetrieval="false"
          enablePasswordReset="true"
          requiresQuestionAndAnswer="true"
          requiresUniqueEmail="false"
          passwordFormat="Hashed"
          maxInvalidPasswordAttempts="5"
          passwordAttemptWindow="10" />
      </providers>
    </membership>
  </system.web>
</configuration>
Version Information

.NET Framework

Supported in: 4, 3.5, 3.0, 2.0
Platforms

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
Thread Safety

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.
See Also

Reference

Other Resources

Community Content

Haris Tafro
SqlMembershipProvider in custom database
Adrian,
there is a  tool to add all required tables, storedprocedures and views in your own database so you can use that database as a repository for SqlMembershipProvider. In Windows\Microsoft.Net\Framework\<Version>\ is file aspnet_regsql.exe, run it and follow the instructions.

Agreeable
sqlmembershipprovider: question?
Hi all,

I am used to using my own login templates, but would like to use these features. Is it possible to use this feature in asp.net to connect to my own db with extra field required for login? if so, how would I go about it.

Any advice would be appreciated.

Thank you
Adriaan.