ConfirmAccount (WebSecurity Ref)

The confirmAccount method confirms a user account. It is an overloaded method, so it can either take one argument, the token, or two arguments, the user and the token.

Syntax

WebSecurity.ConfirmAccount(string ConfirmationToken)

//Overloaded
WebSecurity.ConfirmAccount(string username, string ConfirmationToken)
Sponsored Links

Example 1

C# Code

@{
    
string msg = "";
var confirmtoken = "";
string username = "";
 
 
if (IsPost)
{
    username = Request["username"];
    confirmtoken = Request["token"];
 
 
 
    if (username.IsEmpty())
    {
        ModelState.AddError("username", "Please enter a username");
    }
 
    if (ModelState.IsValid)
    {
 
    if (WebSecurity.UserExists(username))
    {
        if (!WebSecurity.IsConfirmed(username))
        {
            WebSecurity.ConfirmAccount(username, confirmtoken);
            msg = "Account confirmed";
        }else {
            ModelState.AddFormError("Account is already confirmed");
        }
    } else {
       ModelState.AddFormError("Could not find user specified");
    }
 }
 
}

}

HTML Code

<!DOCTYPE html>
 
<html lang="en">
    <head>
        <meta charset="utf-8" />
        <title></title>
    </head>
    <body>
       <form method="post">
       @msg
           @Html.ValidationSummary(true)
       <input type="text" name="username"/>
           @Html.ValidationMessage("username")
 
           <input type="submit"/>
       
       </form>
    </body>
</html>

Example 2

The above method does a lotĀ of verifying before confirming the account. It also uses the username and token method. You can simply confirm the account when the user visits the URL like this:

C# Code

@{
    
string msg = "";
var confirmtoken = Request["token"];
 
try
{
    WebSecurity.ConfirmAccount(confirmtoken);
    msg = "Account Confirmed";
}catch (Exception ex)
{
    msg = ex.Message.ToString();
}
 
 
}

HTML Code

<!DOCTYPE html>
 
<html lang="en">
    <head>
        <meta charset="utf-8" />
        <title></title>
    </head>
    <body>
       @msg
    </body>
</html

See Also

CreateUserAndAccount