Advertisement
altair

GOC335 PasswordReset.aspx.cs

Apr 3rd, 2011
230
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.77 KB | None | 0 0
  1. using System;
  2. using System.Web;
  3. using System.Web.Security;
  4. using Altairis.Nemesis.Events.WebCore.Security;
  5.  
  6. namespace Altairis.Nemesis.Events.WebCore.Pages {
  7.   public partial class PasswordReset : System.Web.UI.Page {
  8.     MembershipUser user;
  9.  
  10.     protected void Page_Load(object sender, EventArgs e) {
  11.       // Try to find user by name or e-mail
  12.       this.user = FindUserByNameOrEmail(this.RouteData.Values["user"] as string);
  13.       var code = this.RouteData.Values["code"] as string;
  14.  
  15.       if (this.user == null) {
  16.         // User not found, show error
  17.         this.MultiViewPage.SetActiveView(this.ViewError);
  18.         this.LiteralErrorUserNotFound.Visible = true;
  19.         this.LiteralErrorUserNotFound.Text = string.Format(this.LiteralErrorUserNotFound.Text, HttpUtility.HtmlEncode(this.RouteData.Values["user"] as string));
  20.       }
  21.       else if (string.IsNullOrEmpty(code)) {
  22.         // Code not present - Step 1
  23.         this.MultiViewPage.SetActiveView(this.ViewStep1);
  24.  
  25.         // Build confirmation URI
  26.         var uri = new UriBuilder(this.Request.Url);
  27.         code = this.user.CreatePasswordResetCode();
  28.         uri.Path = string.Format("/reset/{0}/{1}", this.user.UserName, code);
  29.  
  30.         // Send mail
  31.         Altairis.MailToolkit.Mailer.SendTemplatedMessage(
  32.             this.user.Email,         // recipient
  33.             "PasswordReset",    // template
  34.             this.user.UserName,      // {0}
  35.             uri.ToString());    // {1}
  36.         return;
  37.  
  38.       }
  39.       else if (this.user.VerifyPasswordResetCode(code)) {
  40.         // Correct code - Step 2
  41.         this.MultiViewPage.SetActiveView(this.ViewStep2);
  42.       }
  43.       else {
  44.         // Incorrect code - Error
  45.         this.MultiViewPage.SetActiveView(this.ViewError);
  46.         this.LiteralErrorCodeInvalid.Visible = true;
  47.         this.LiteralErrorCodeInvalid.Text = string.Format(this.LiteralErrorCodeInvalid.Text, this.user.UserName);
  48.       }
  49.     }
  50.  
  51.     protected void ButtonResetPassword_Click(object sender, EventArgs e) {
  52.       if (!this.IsValid) return;
  53.  
  54.       // Change password
  55.       var tempPassword = this.user.ResetPassword();
  56.       this.user.ChangePassword(tempPassword, this.PasswordTextBox.Text);
  57.  
  58.       // Login user
  59.       FormsAuthentication.RedirectFromLoginPage(this.user.UserName, false);
  60.     }
  61.  
  62.     // Helper methods
  63.  
  64.     private static MembershipUser FindUserByNameOrEmail(string s) {
  65.       if (string.IsNullOrWhiteSpace(s)) return null;
  66.  
  67.       // Try to find user by user name
  68.       var user = Membership.GetUser(s, false);
  69.       if (user != null) return user;
  70.  
  71.       // Try to find user by e-mail
  72.       var userName = Membership.GetUserNameByEmail(s);
  73.       if (string.IsNullOrEmpty(userName)) return null;
  74.       return Membership.GetUser(userName, false);
  75.     }
  76.  
  77.   }
  78. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement