Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Web;
- using System.Web.Security;
- using Altairis.Nemesis.Events.WebCore.Security;
- namespace Altairis.Nemesis.Events.WebCore.Pages {
- public partial class PasswordReset : System.Web.UI.Page {
- MembershipUser user;
- protected void Page_Load(object sender, EventArgs e) {
- // Try to find user by name or e-mail
- this.user = FindUserByNameOrEmail(this.RouteData.Values["user"] as string);
- var code = this.RouteData.Values["code"] as string;
- if (this.user == null) {
- // User not found, show error
- this.MultiViewPage.SetActiveView(this.ViewError);
- this.LiteralErrorUserNotFound.Visible = true;
- this.LiteralErrorUserNotFound.Text = string.Format(this.LiteralErrorUserNotFound.Text, HttpUtility.HtmlEncode(this.RouteData.Values["user"] as string));
- }
- else if (string.IsNullOrEmpty(code)) {
- // Code not present - Step 1
- this.MultiViewPage.SetActiveView(this.ViewStep1);
- // Build confirmation URI
- var uri = new UriBuilder(this.Request.Url);
- code = this.user.CreatePasswordResetCode();
- uri.Path = string.Format("/reset/{0}/{1}", this.user.UserName, code);
- // Send mail
- Altairis.MailToolkit.Mailer.SendTemplatedMessage(
- this.user.Email, // recipient
- "PasswordReset", // template
- this.user.UserName, // {0}
- uri.ToString()); // {1}
- return;
- }
- else if (this.user.VerifyPasswordResetCode(code)) {
- // Correct code - Step 2
- this.MultiViewPage.SetActiveView(this.ViewStep2);
- }
- else {
- // Incorrect code - Error
- this.MultiViewPage.SetActiveView(this.ViewError);
- this.LiteralErrorCodeInvalid.Visible = true;
- this.LiteralErrorCodeInvalid.Text = string.Format(this.LiteralErrorCodeInvalid.Text, this.user.UserName);
- }
- }
- protected void ButtonResetPassword_Click(object sender, EventArgs e) {
- if (!this.IsValid) return;
- // Change password
- var tempPassword = this.user.ResetPassword();
- this.user.ChangePassword(tempPassword, this.PasswordTextBox.Text);
- // Login user
- FormsAuthentication.RedirectFromLoginPage(this.user.UserName, false);
- }
- // Helper methods
- private static MembershipUser FindUserByNameOrEmail(string s) {
- if (string.IsNullOrWhiteSpace(s)) return null;
- // Try to find user by user name
- var user = Membership.GetUser(s, false);
- if (user != null) return user;
- // Try to find user by e-mail
- var userName = Membership.GetUserNameByEmail(s);
- if (string.IsNullOrEmpty(userName)) return null;
- return Membership.GetUser(userName, false);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement