Export (0) Print
Expand All

WebSecurity.Login Method

Logs the user in.

Namespace:  WebMatrix.WebData
Assembly:  WebMatrix.WebData (in WebMatrix.WebData.dll)

public static bool Login(
	string userName,
	string password,
	bool persistCookie
)

Parameters

userName
Type: System.String
The user name.
password
Type: System.String
The password.
persistCookie
Type: System.Boolean
(Optional) true to specify that the authentication token in the cookie should be persisted beyond the current session; otherwise false. The default is false.

Return Value

Type: System.Boolean
true if the user was logged in; otherwise, false.

ExceptionCondition
InvalidOperationException

The SimpleMembershipProvider.Initialize(String, NameValueCollection) method was not called.

-or-

The InitializeDatabaseConnection() method was not called.

-or-

The SimpleMembershipProvider membership provider is not registered in the configuration of your site. For more information, contact your site's system administrator.

When a user is logged in, ASP.NET sets an authentication token in a cookie that lets ASP.NET know on subsequent requests that the user has been logged in. If persistCookie is false, the token is valid only until the user closes the browser.

The following example shows how to use the Login(String, String, Boolean) method to log in a user.


// Attempt to login the user.
if (WebSecurity.Login(username, password)) {
    // Greet the user.
    <p>Welcome @username.</p>
    <p>Click <a href="MemberContent.cshtml">here</a> to continue.</p>
} else {
    // Login failed.
    <p>Incorrect username or password.</p>
}


  • Medium trust for the immediate caller. This member can be used by partially trusted code.
Show:
© 2014 Microsoft