WebSecurity - Login()


WebSecurity 对象WebSecurity Object

Definition

Login()The method uses the user name and password to log in the specified user.


C# and VB Syntax

WebSecurity.Login(userName, password, persistCookie)


Example

Example C#

if (WebSecurity.Login(username,password))
{
<p>Welcome</p>
}
else
{
<p>Incorrect username or password.</p>
}

Example VB

if WebSecurity.Login(username,password)
<p>Welcome</p>
else
<p>Incorrect username or password</p>
end if


Parameters

Parameters Type Description
userName String User name
password String User password
persistCookie String trueSpecifies whether the authentication token in the cookie should continue to be retained after the current session ends; otherwise,false. The default is false.


Return Value

Type Description
Boolean If the user is already logged in, returnstrue, otherwise returnsfalse。


Remarks

When the user is logged in, ASP.NET sets an authentication token in the cookie, so that ASP.NET knows the user is logged in for subsequent requests. IfpersistCookieis false, the token is only valid until the user closes the browser.


Errors and Exceptions

In the following cases, any access to theWebSecurityobject will throw anInvalidOperationException:

  • InitializeDatabaseConnection()The method has not been called
  • SimpleMembershipNot initialized (or disabled in the website configuration)

Technical Data

Name Value
Namespace WebMatrix.WebData
Assembly WebMatrix.WebData.dll


WebSecurity 对象WebSecurity Object Other Extensions