Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Configuration;
- using System.Linq;
- using System.Security.Cryptography;
- using System.Text;
- using System.Web.Security;
- using Altairis.Nemesis.Events.Data;
- using Altairis.Nemesis.Events.WebCore.Security;
- namespace Altairis.Nemesis.Events.WebCore.Pages {
- public partial class Registration : System.Web.UI.Page {
- protected void Page_Load(object sender, EventArgs e) {
- // Check if there is already user with this address
- var email = this.RouteData.Values["email"] as string;
- var existingUserName = Membership.GetUserNameByEmail(email);
- if (!string.IsNullOrEmpty(existingUserName)) {
- // E-mail already used
- this.MultiViewPage.SetActiveView(this.ViewError);
- this.LiteralErrorDuplicateEmail.Text = string.Format(this.LiteralErrorDuplicateEmail.Text, email, existingUserName);
- this.LiteralErrorDuplicateEmail.Visible = true;
- return;
- }
- // Get verification code
- var code = this.RouteData.Values["code"] as string;
- if (string.IsNullOrEmpty(code)) {
- // Step 1
- this.MultiViewPage.SetActiveView(this.ViewStep1);
- this.LiteralVerify.Text = string.Format(this.LiteralVerify.Text, email);
- // Build confirmation URI
- var uri = new UriBuilder(this.Request.Url);
- code = GetEmailVerificationCode(email);
- uri.Path = string.Format("/reg/{0}/{1}", email, code);
- // Send mail
- Altairis.MailToolkit.Mailer.SendTemplatedMessage(
- email, // recipient
- "EmailVerification", // template
- uri.ToString()); // {0}
- return;
- }
- // Verification code was supplied - check it
- if (!code.Equals(GetEmailVerificationCode(email))) {
- // Invalid code
- this.MultiViewPage.SetActiveView(this.ViewError);
- this.LiteralErrorCodeInvalid.Text = string.Format(this.LiteralErrorCodeInvalid.Text, email);
- this.LiteralErrorCodeInvalid.Visible = true;
- return;
- }
- // Verification code is valid - show registration form for Step 2
- this.MultiViewPage.SetActiveView(this.ViewStep2);
- this.LiteralVerified.Text = string.Format(this.LiteralVerified.Text, email);
- }
- protected void ButtonSubmit_Click(object sender, EventArgs e) {
- if (!this.IsValid) return;
- // Try to create user
- MembershipCreateStatus status;
- var u = Membership.CreateUser(
- this.UserNameTextBox.Text,
- this.PasswordTextBox.Text,
- this.RouteData.Values["email"] as string,
- null, // pw question
- null, // pw answer
- true, // is approved
- out status);
- if (status == MembershipCreateStatus.Success) {
- // Created successfully
- using (var dc = new NemesisEventsEntities()) {
- var user = dc.Users.Single(x => x.UserName.Equals(u.UserName));
- // Set display name
- user.DisplayName = this.DisplayNameTextBox.Text;
- // Watch all areas
- foreach (var area in dc.Areas) {
- user.WatchedAreas.Add(area);
- }
- // Save changes
- dc.SaveChanges();
- }
- // Login and redirect to home
- FormsAuthentication.RedirectFromLoginPage(u.UserName, false);
- }
- else if (status == MembershipCreateStatus.DuplicateUserName) {
- // Duplicate user name
- this.MultiViewPage.SetActiveView(this.ViewError);
- this.LiteralErrorDuplicateUserName.Text = string.Format(this.LiteralErrorDuplicateUserName.Text, this.UserNameTextBox.Text);
- this.LiteralErrorDuplicateUserName.Visible = true;
- }
- else {
- // Other error
- this.MultiViewPage.SetActiveView(this.ViewError);
- this.LiteralErrorOther.Text = string.Format(this.LiteralErrorOther.Text, status);
- this.LiteralErrorOther.Visible = true;
- }
- }
- private static string GetEmailVerificationCode(string emailAddress) {
- if (emailAddress == null) throw new ArgumentNullException("emailAddress");
- if (string.IsNullOrWhiteSpace(emailAddress)) throw new ArgumentException("Value cannot be empty or whitespace only string.", "emailAddress");
- // Convert e-mail to byte array
- var data = Encoding.UTF8.GetBytes(emailAddress);
- // Prepare HMAC
- using (var hmac = new HMACSHA1()) {
- // Add salt
- hmac.Key = Convert.FromBase64String(ConfigurationManager.AppSettings["EmailVerificationKey"]);
- // Compute HMAC
- var code = hmac.ComputeHash(data);
- // Return as Base64-encoded string
- return code.ToUrlSafeBase64String();
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement