RegisterWithConfirmation.cshtml in Root Folder

 

@{

  var username = "";

  var password = "";

  var confirmPassword = "";

  var message = "";

  var confirmationToken = "";

  var emailBodyText = "";

   

  if(!IsPost){

    if(WebSecurity.IsAuthenticated){

      message = String.Format("You are already logged in. (User name: {0})", WebSecurity.CurrentUserName);

    }

  }

   

  if(IsPost){

    username = Request["username"];

    password = Request["password"];

    confirmPassword = Request["confirmPassword"];

 

    if(password != confirmPassword){

      message = "Passwords don't match.";

    }

    else {

      if(WebSecurity.UserExists(username)){

        message = String.Format("User '{0}' already exists.", username);

      }

      else{

        // Start process of creating new user and getting confirmation token

        confirmationToken = WebSecurity.CreateUserAndAccount(username,password,null,true);

        // The URL is currently hard coded

        var confirmationUrl = "http://mikepope.com/testmembership/ConfirmAccount?confirmation=" +

            HttpUtility.UrlEncode(confirmationToken);

        var confirmationLink = String.Format("<a href=\"{0}\">Click to confirm your registration</a>",

            confirmationUrl);

       

        WebMail.SmtpServer = "smtp.<mySMTPServer>.com";

        WebMail.SmtpPort = 25;

        WebMail.EnableSsl = false;

        WebMail.UserName = "<myUserName>";

        WebMail.From = "mike@mikepope.com";

        WebMail.Password = "<myPassword>";

 

        emailBodyText =

              "<p>Thank you for signing up with us! Please confirm your registration by clicking the following link:</p>"

           +  "<p>" + confirmationLink + "</p>"

           +  "<p>In case you need it, here's the confirmation code:<strong> " + confirmationToken + "</strong></p>";

 

        WebMail.Send(

           to: username,

           subject: "Your example account confirmation",

           body: emailBodyText,

           isBodyHtml: true

        );

        message = String.Format(

            "Thank you for registering. An email has been sent to {0}. " +

            "Please check your email and use the enclosed link to finish registration.",

            username);

      }

    }

  }

}

 

<!DOCTYPE html>

<html lang="en">

  <head>

    <link rel="stylesheet" href="@Href("~/")TestMembership.css" type="text/css" />

    <meta charset="utf-8" />

    <title></title>

  </head>

  <body>

    <h1>Register</h1>

    <form method="post">

      <p>

        @if(message != ""){

            <span class="errorMessage">@message</span>

        }

      </p>

 

      <p>

        <label for="username">Username (email address):</label><br/>

        <input type="text" name="username" id="username" value='@Request["username"]' />

      </p>

      <p>

        <label for="password">Password:</label><br/>

        <input type="password" name="password" id="password" value="" />

      </p>   

      <p>

        <label for="confirmPassword">Confirm password:</label><br/>

        <input type="password" name="confirmPassword" id="confirmPassword" value="" />

      </p>   

 

      <p>

        <input type="submit" value="Submit" />

      </p>   

      <p>

        <a href="@Href("~/Home")">Home</a>

        </p>

      </form>

  </body>

</html>

 

Return to sample app.

Return to blog entry