Advertisement
altair

GOC335 Registration.ascs.cs

Apr 3rd, 2011
279
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.36 KB | None | 0 0
  1. using System;
  2. using System.Configuration;
  3. using System.Linq;
  4. using System.Security.Cryptography;
  5. using System.Text;
  6. using System.Web.Security;
  7. using Altairis.Nemesis.Events.Data;
  8. using Altairis.Nemesis.Events.WebCore.Security;
  9.  
  10. namespace Altairis.Nemesis.Events.WebCore.Pages {
  11.     public partial class Registration : System.Web.UI.Page {
  12.  
  13.         protected void Page_Load(object sender, EventArgs e) {
  14.             // Check if there is already user with this address
  15.             var email = this.RouteData.Values["email"] as string;
  16.             var existingUserName = Membership.GetUserNameByEmail(email);
  17.             if (!string.IsNullOrEmpty(existingUserName)) {
  18.                 // E-mail already used
  19.                 this.MultiViewPage.SetActiveView(this.ViewError);
  20.                 this.LiteralErrorDuplicateEmail.Text = string.Format(this.LiteralErrorDuplicateEmail.Text, email, existingUserName);
  21.                 this.LiteralErrorDuplicateEmail.Visible = true;
  22.                 return;
  23.             }
  24.  
  25.             // Get verification code
  26.             var code = this.RouteData.Values["code"] as string;
  27.             if (string.IsNullOrEmpty(code)) {
  28.                 // Step 1
  29.                 this.MultiViewPage.SetActiveView(this.ViewStep1);
  30.                 this.LiteralVerify.Text = string.Format(this.LiteralVerify.Text, email);
  31.  
  32.                 // Build confirmation URI
  33.                 var uri = new UriBuilder(this.Request.Url);
  34.                 code = GetEmailVerificationCode(email);
  35.                 uri.Path = string.Format("/reg/{0}/{1}", email, code);
  36.  
  37.                 // Send mail
  38.                 Altairis.MailToolkit.Mailer.SendTemplatedMessage(
  39.                     email,                  // recipient
  40.                     "EmailVerification",    // template
  41.                     uri.ToString());        // {0}
  42.                 return;
  43.             }
  44.  
  45.             // Verification code was supplied - check it
  46.             if (!code.Equals(GetEmailVerificationCode(email))) {
  47.                 // Invalid code
  48.                 this.MultiViewPage.SetActiveView(this.ViewError);
  49.                 this.LiteralErrorCodeInvalid.Text = string.Format(this.LiteralErrorCodeInvalid.Text, email);
  50.                 this.LiteralErrorCodeInvalid.Visible = true;
  51.                 return;
  52.             }
  53.  
  54.             // Verification code is valid - show registration form for Step 2
  55.             this.MultiViewPage.SetActiveView(this.ViewStep2);
  56.             this.LiteralVerified.Text = string.Format(this.LiteralVerified.Text, email);
  57.         }
  58.  
  59.         protected void ButtonSubmit_Click(object sender, EventArgs e) {
  60.             if (!this.IsValid) return;
  61.  
  62.             // Try to create user
  63.             MembershipCreateStatus status;
  64.             var u = Membership.CreateUser(
  65.                 this.UserNameTextBox.Text,
  66.                 this.PasswordTextBox.Text,
  67.                 this.RouteData.Values["email"] as string,
  68.                 null, // pw question
  69.                 null, // pw answer
  70.                 true, // is approved
  71.                 out status);
  72.  
  73.             if (status == MembershipCreateStatus.Success) {
  74.                 // Created successfully
  75.                 using (var dc = new NemesisEventsEntities()) {
  76.                     var user = dc.Users.Single(x => x.UserName.Equals(u.UserName));
  77.                     // Set display name
  78.                     user.DisplayName = this.DisplayNameTextBox.Text;
  79.  
  80.                     // Watch all areas
  81.                     foreach (var area in dc.Areas) {
  82.                         user.WatchedAreas.Add(area);
  83.                     }
  84.  
  85.                     // Save changes
  86.                     dc.SaveChanges();
  87.                 }
  88.  
  89.                 // Login and redirect to home
  90.                 FormsAuthentication.RedirectFromLoginPage(u.UserName, false);
  91.             }
  92.             else if (status == MembershipCreateStatus.DuplicateUserName) {
  93.                 // Duplicate user name
  94.                 this.MultiViewPage.SetActiveView(this.ViewError);
  95.                 this.LiteralErrorDuplicateUserName.Text = string.Format(this.LiteralErrorDuplicateUserName.Text, this.UserNameTextBox.Text);
  96.                 this.LiteralErrorDuplicateUserName.Visible = true;
  97.             }
  98.             else {
  99.                 // Other error
  100.                 this.MultiViewPage.SetActiveView(this.ViewError);
  101.                 this.LiteralErrorOther.Text = string.Format(this.LiteralErrorOther.Text, status);
  102.                 this.LiteralErrorOther.Visible = true;
  103.             }
  104.         }
  105.  
  106.         private static string GetEmailVerificationCode(string emailAddress) {
  107.             if (emailAddress == null) throw new ArgumentNullException("emailAddress");
  108.             if (string.IsNullOrWhiteSpace(emailAddress)) throw new ArgumentException("Value cannot be empty or whitespace only string.", "emailAddress");
  109.  
  110.             // Convert e-mail to byte array
  111.             var data = Encoding.UTF8.GetBytes(emailAddress);
  112.  
  113.             // Prepare HMAC
  114.             using (var hmac = new HMACSHA1()) {
  115.                 // Add salt
  116.                 hmac.Key = Convert.FromBase64String(ConfigurationManager.AppSettings["EmailVerificationKey"]);
  117.  
  118.                 // Compute HMAC
  119.                 var code = hmac.ComputeHash(data);
  120.  
  121.                 // Return as Base64-encoded string
  122.                 return code.ToUrlSafeBase64String();
  123.             }
  124.         }
  125.  
  126.     }
  127. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement