ASP.NET Web Pages - Account Confirmation

[ This is an 8 part tutorial, previous tutorial creating registration page ]

In the ConfirmAccount.cshtml add the following:

@{
    
var token = Request["token"];
bool showForm = false;
 
var message = "";
 
if (!string.IsNullOrEmpty(token))
{
    if (WebSecurity.ConfirmAccount(token))
    {
        message = "Your account has been confirmed";
    }else
    {
        message = "Oops something has gone wrong there please contact the Administrator";
    }
}else
{
   showForm = true;
}

There are two methods we will use to confirm the user account. The first way is done directly. If the user enters the full URL, http://localhost:12345/account/confirmaccount?token=6363hfhfhf-fff, the account will be confirmed right away. If they don’t, a form will show where the user can type their confirmation token.

In the above example we have two variables: the first is the token which requests the token and the second is showForm. This will show or hide the form depending on the type of confirmation the user chooses.

The If Statement condition checks to make sure the token is not null; if it is not null we begin to confirm the account. The ConfirmAccount method takes one argument, which is the token. When the account is confirmed a message will appear telling the user the account has been confirmed, and you could perform a redirect to login page if you wish. If the token is empty the showForm variable is set to true.

if (IsPost)
{
       
    
    if (WebSecurity.ConfirmAccount(token))
    {
        message = "Your account has been confirmed";
    }else
    {
        message = "Oops something has gone wrong there please contact the Administrator";
    }
 
}

If the showForm variable is set to true then on post this will execute; the code is similar to the previous example.

HTML

@message
 
@if (showForm == true)
{
<form method="post">
 
<div>
<label>Token</label>
<input type="text" name="token"/>     
</div>
 
<input type="submit"/>
 
</form>   
}

We only show the form if showForm is set to true. Now check your email and confirm your account.

[ continue, Login Page]