asp.net.ph

Membership Class

System.Web.Security Namespace


.NET Framework version 2.0

Validates user credentials and manages user settings. This class cannot be inherited.

Membership Class Members

Collapse   Properties

Visibility Name Value Type Accessibility
public static ApplicationName String [ Get , Set ]
public static EnablePasswordReset Boolean [ Get ]
public static EnablePasswordRetrieval Boolean [ Get ]
public static HashAlgorithmType String [ Get ]
public static MaxInvalidPasswordAttempts Int32 [ Get ]
public static MinRequiredNonAlphanumericCharacters Int32 [ Get ]
public static MinRequiredPasswordLength Int32 [ Get ]
public static PasswordAttemptWindow Int32 [ Get ]
public static PasswordStrengthRegularExpression String [ Get ]
public static Provider MembershipProvider [ Get ]
public static Providers MembershipProviderCollection [ Get ]
public static RequiresQuestionAndAnswer Boolean [ Get ]
public static UserIsOnlineTimeWindow Int32 [ Get ]

Collapse   Methods

Visibility Name Parameters Return Type
public static CreateUser ( String username , String password , String email , String passwordQuestion , String passwordAnswer , Boolean isApproved , MembershipCreateStatus& status ) MembershipUser
public static CreateUser ( String username , String password , String email , String passwordQuestion , String passwordAnswer , Boolean isApproved , Object providerUserKey , MembershipCreateStatus& status ) MembershipUser
public static CreateUser ( String username , String password ) MembershipUser
public static CreateUser ( String username , String password , String email ) MembershipUser
public static DeleteUser ( String username , Boolean deleteAllRelatedData ) Boolean
public static DeleteUser ( String username ) Boolean
public static FindUsersByEmail ( String emailToMatch ) MembershipUserCollection
public static FindUsersByEmail ( String emailToMatch , Int32 pageIndex , Int32 pageSize , Int32& totalRecords ) MembershipUserCollection
public static FindUsersByName ( String usernameToMatch , Int32 pageIndex , Int32 pageSize , Int32& totalRecords ) MembershipUserCollection
public static FindUsersByName ( String usernameToMatch ) MembershipUserCollection
public static GeneratePassword ( Int32 length , Int32 numberOfNonAlphanumericCharacters ) String
public static GetAllUsers ( Int32 pageIndex , Int32 pageSize , Int32& totalRecords ) MembershipUserCollection
public static GetAllUsers ( ) MembershipUserCollection
public static GetNumberOfUsersOnline ( ) Int32
public static GetUser ( String username , Boolean userIsOnline ) MembershipUser
public static GetUser ( Object providerUserKey ) MembershipUser
public static GetUser ( Object providerUserKey , Boolean userIsOnline ) MembershipUser
public static GetUser ( Boolean userIsOnline ) MembershipUser
public static GetUser ( String username ) MembershipUser
public static GetUser ( ) MembershipUser
public static GetUserNameByEmail ( String emailToMatch ) String
public static UpdateUser ( MembershipUser user ) Void
public static ValidateUser ( String username , String password ) Boolean

Collapse   Events

Remarks

The Membership class is used in ASP.NET applications to validate user credentials and manage user settings such as passwords and e-mail addresses. The Membership class can be used on its own, or in conjunction with the FormsAuthentication to create a complete system for authenticating users of a Web application or site. The Login control encapsulates the Membership class to provide a convenient mechanism for validating users.

The Membership class provides facilities for:

  • Creating new users.
  • Storing membership information ( user names, passwords, e-mail addresses, and supporting data ) in Microsoft SQL Server or in an alternative data store.
  • Authenticating users who visit your site. You can authenticate users programmatically, or you can use the Login control to create a complete authentication system that requires little or no code.
  • Managing passwords, which includes creating, changing, retrieving, and resetting them, and so on. You can optionally configure ASP.NET membership to require a password question and answer to authenticate password reset or retrieval requests for users that have forgotten their password.

Although ASP.NET membership is a self-standing feature in ASP.NET For authentication, it can be integrated with ASP.NET role management to provide authorization services for your site. Membership can also be integrated with the ASP.NET user System.Web.Profile to provide application-specific customization that can be tailored to individual users. For details, see Understanding Role Management and ASP.NET Profile Properties Overview.

The Membership class relies on membership providers to communicate with a data source. The .NET Framework includes a SqlMembershipProvider, which stores user information in a Microsoft SQL Server database. You can also implement a custom membership provider to communicate with an alternative data source that can be used by the Membership class. Custom membership providers inherit the MembershipProvider abstract class. For more information, see Implementing a Membership Provider.

By default, ASP.NET membership is enabled for all ASP.NET applications. The default membership provider is the SqlMembershipProvider and is specified in the machine configuration with the name AspNetSqlProvider. The default instance of the SqlMembershipProvider is configured to connect to a local instance of Microsoft SQL Server.

You can modify the default settings to specify a SqlMembershipProvider other than the AspNetSqlProvider instance as the default provider, or specify an instance of a custom provider as the default provider for your ASP.NET application using the Web.config file. You can specify the ASP.NET membership configuration for your Web application using the membership configuration section in the Web.config file. You can use the providers subsection of the membership section to specify a membership provider other than one of the default providers. For example, the following membership section removes the default membership providers from the current application configuration and adds a new provider with a name of SqlProvider that connects to a SQL Server instance named MySqlServer.

<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>

Example

The following code example shows the login page for an ASP.NET application configured to use forms authentication and ASP.NET membership. If the supplied user credentials are invalid, a message is displayed to the user. Otherwise, the user is redirected to the originally requested URL using the RedirectFromLoginPage.

NOTE: The ASP.NET login controls ( Login, LoginView, LoginStatus, LoginName, and PasswordRecovery ) encapsulate virtually all of the logic required to prompt users for credentials and validate the credentials in the membership system and can be used in place of programmatic checking using the Membership class.

Below is the HTML for the page.

<html>
<head>
   <title>Login</title>
</head>
<body>

<form runat = "server">
   <h3>Login</h3>

   <asp:Label id = "Msg" ForeColor = "maroon" runat = "server" /><P>

   Username: <asp:Textbox id = "UsernameTextbox" runat = "server" /><br>
   Password: <asp:Textbox id = "PasswordTextbox" runat = "server" TextMode = "Password" /><br>
 
   <asp:Button id = "LoginButton" Text = "Login" OnClick = "Login_OnClick" runat = "server" />
   <asp:CheckBox id = "NotPublicCheckBox" runat = "server" /> 
      Check here if this is <u>not</u> a public computer.

</form>

</body>
</html>

Below is the code for the above page.

<%@ Page Language = "C#" %>
<%@ Import Namespace = "System.Web.Security" %>

<script runat = "server">

public void Login_OnClick ( object sender, EventArgs args ) {
   if ( Membership.ValidateUser ( UsernameTextbox.Text, PasswordTextbox.Text ) ) {
      FormsAuthentication.RedirectFromLoginPage ( UsernameTextbox.Text, NotPublicCheckBox.Checked );
   } else {
      Msg.Text = "Login failed. Please check your user name and password and try again.";
   }
}

</script>
  C# VB

See Also

MembershipProvider Class   MembershipUser Class   <membership> Section Skip Navigation Links




Home
Suggested Reading


Previous page Back to top Next page

© 2000-2010 Rey Nuñez All rights reserved.

If you have any question, comment or suggestion
about this site, please send us a note

You can help support asp.net.ph