Advertisement
Guest User

Untitled

a guest
Apr 17th, 2012
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
HTML 5 6.90 KB | None | 0 0
  1. @{
  2.     // Set the layout page and page title
  3.     Layout = "~/_SiteLayout.cshtml";
  4.     Page.Title = "Register an Account";
  5.  
  6.     // Initialize general page variables
  7.     var email = "";
  8.     var password = "";
  9.     var confirmPassword = "";
  10.  
  11.     // Validation
  12.     var isValid = true;
  13.     var emailErrorMessage = "";
  14.     var passwordErrorMessage = "";
  15.     var confirmPasswordMessage = "";
  16.     var accountCreationErrorMessage = "";
  17.     // var captchaMessage = "";
  18.  
  19.     // If this is a POST request, validate and process data
  20.     if (IsPost) {
  21.         email = Request.Form["email"];
  22.         password = Request.Form["password"];
  23.         confirmPassword = Request.Form["confirmPassword"];
  24.  
  25.         // Validate the user's captcha answer
  26.         // if (!ReCaptcha.Validate("PRIVATE_KEY")) {
  27.         //     captchaMessage = "Captcha response was not correct";
  28.         //     isValid = false;
  29.         // }
  30.  
  31.         // Validate the user's email address
  32.         if (email.IsEmpty()) {
  33.             emailErrorMessage = "You must specify an email address.";
  34.             isValid = false;
  35.         }
  36.  
  37.         // Validate the user's password and password confirmation
  38.         if (password.IsEmpty()) {
  39.             passwordErrorMessage = "The password cannot be blank.";
  40.             isValid = false;
  41.         }
  42.  
  43.         if (password != confirmPassword) {
  44.             confirmPasswordMessage = "The new password and confirmation password do not match.";
  45.             isValid = false;
  46.         }
  47.  
  48.         // If all information is valid, create a new account
  49.         if (isValid) {
  50.             // Insert a new user into the database
  51.             var db = Database.Open("StarterSite");
  52.  
  53.             // Check if user already exists
  54.             var user = db.QuerySingle("SELECT Email FROM UserProfile WHERE LOWER(Email) = LOWER(@0)", email);
  55.             if (user == null) {
  56.                 // Insert email into the profile table
  57.                 db.Execute("INSERT INTO UserProfile (Email) VALUES (@0)", email);
  58.  
  59.                 // Create and associate a new entry in the membership database.
  60.                 // If successful, continue processing the request
  61.                 try {
  62.                     bool requireEmailConfirmation = !WebMail.SmtpServer.IsEmpty();
  63.                     var token = WebSecurity.CreateAccount(email, password, requireEmailConfirmation);
  64.                     if (requireEmailConfirmation) {
  65.                         var hostUrl = Request.Url.GetComponents(UriComponents.SchemeAndServer, UriFormat.Unescaped);
  66.                         var confirmationUrl = hostUrl + VirtualPathUtility.ToAbsolute("~/Account/Confirm?confirmationCode=" + HttpUtility.UrlEncode(token));
  67.  
  68.                         WebMail.Send(
  69.                             to: email, 
  70.                             subject: "Please confirm your account",
  71.                             body: "Your confirmation code is: " + token + ". Visit <a href=\"" + confirmationUrl + "\">" + confirmationUrl + "</a> to activate your account."
  72.                         );
  73.                     }
  74.  
  75.                     if (requireEmailConfirmation) {
  76.                         // Thank the user for registering and let them know an email is on its way
  77.                         Response.Redirect("~/Account/Thanks");
  78.                     } else {
  79.                         // Navigate back to the homepage and exit
  80.                         WebSecurity.Login(email, password);
  81.                         Response.Redirect("~/");
  82.                     }
  83.                 } catch (System.Web.Security.MembershipCreateUserException e) {
  84.                     isValid = false;
  85.                     accountCreationErrorMessage = e.ToString();
  86.                 }
  87.             } else {
  88.                 // User already exists
  89.                 isValid = false;
  90.                 accountCreationErrorMessage = "Email address is already in use.";
  91.             }
  92.         }    
  93.     }
  94. }
  95.  
  96. <p>
  97.    Use the form below to create a new account.
  98. </p>
  99.  
  100. @* If at least one validation error exists, notify the user *@
  101. @if (!isValid) {
  102.    <p class="message error">
  103.     @if (accountCreationErrorMessage.IsEmpty()) {
  104.         @:Please correct the errors and try again.
  105.     } else {
  106.         @accountCreationErrorMessage
  107.     }
  108.    </p>
  109. }
  110.  
  111. <form method="post" action="">
  112.     <fieldset>
  113.         <legend>Sign-up Form</legend>
  114.         <ol>
  115.             <li class="email">
  116.                 <label for="email">Email:</label>
  117.                 <input type="text" id="email" name="email" title="Email address" value="@email" @if(!emailErrorMessage.IsEmpty()){<text>class="error-field"</text>} />
  118.                 @* Write any email validation errors to the page *@
  119.                 @if (!emailErrorMessage.IsEmpty()) {
  120.                     <label for="email" class="validation-error">@emailErrorMessage</label>
  121.                 }
  122.             </li>
  123.             <li class="password">
  124.                 <label for="password">Password:</label>
  125.                 <input type="password" id="password" name="password" title="Password" @if(!passwordErrorMessage.IsEmpty()){<text>class="error-field"</text>} />
  126.                 @* Write any password validation errors to the page *@
  127.                 @if (!passwordErrorMessage.IsEmpty()) {
  128.                     <label for="password" class="validation-error">@passwordErrorMessage</label>
  129.                 }
  130.             </li>
  131.             <li class="confirm-password">
  132.                 <label for="confirmPassword">Confirm Password:</label>
  133.                 <input type="password" id="confirmPassword" name="confirmPassword" title="Confirm password" @if(!confirmPasswordMessage.IsEmpty()){<text>class="error-field"</text>} />
  134.                 @* Write any password validation errors to the page *@
  135.                 @if (!confirmPasswordMessage.IsEmpty()) {
  136.                     <label for="confirmPassword" class="validation-error">@confirmPasswordMessage</label>
  137.                 }
  138.             </li>
  139.             <li class="recaptcha">
  140.                 <div class="message info">
  141.                     <p>To enable CAPTCHA verification, <a href="http://go.microsoft.com/fwlink/?LinkId=204140">install the ASP.NET Web Helpers Library</a> and uncomment ReCaptcha.GetHtml and replace 'PUBLIC_KEY'
  142.                     with your public key.  At the top of this page, uncomment ReCaptcha.Validate and
  143.                     replace 'PRIVATE_KEY' with your private key, and also uncomment the captchaMessage variable.</p>
  144.                     <p>Register for reCAPTCHA keys at <a href="http://recaptcha.net">reCAPTCHA.net</a>.</p>
  145.                 </div>
  146.                 @*@ReCaptcha.GetHtml("PUBLIC_KEY", theme: "white")
  147.                 @if (!captchaMessage.IsEmpty()) {
  148.                     <label class="validation-error">@captchaMessage</label>
  149.                 }*@
  150.             </li>
  151.         </ol>
  152.         <p class="form-actions">
  153.             <input type="submit" value="Register" title="Register" />
  154.         </p>
  155.     </fieldset>
  156. </form>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement