📄 forgottenpassword.cs
字号:
//------------------------------------------------------------------------------
// <copyright company="Telligent Systems">
// Copyright (c) Telligent Systems Corporation. All rights reserved.
// </copyright>
//------------------------------------------------------------------------------
using System;
using System.Collections;
using System.Collections.Specialized;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.ComponentModel;
using System.IO;
using CommunityServer;
using CommunityServer.Components;
namespace CommunityServer.Controls {
/// <summary>
/// This Web control displays the textboxes needed to create a new user account.
/// The user can enter their username and email, and a warning message is displayed if
/// either the username or email message is already taken. Once the user enters an available
/// username and password, they will be sent a confirmation email with their password.
/// </summary>
[
ParseChildren(true)
]
public class ForgottenPassword : CommunityServer.Controls.TemplatedWebControl {
#region Member variables and contructor
TextBox emailAddress;
Button sendPasswordButton;
RequiredFieldValidator emailValidator;
RegularExpressionValidator emailRegexValidator;
ValidationSummary validationMsg;
Button nextButton;
Literal sQuestion;
TextBox sAnswer;
RequiredFieldValidator sAnswerValidator;
RegularExpressionValidator sAnswerRegexValidator;
CommunityServer.Components.PasswordRecovery recoveryMethod;
// *********************************************************************
// CreateUser
//
/// <summary>
/// Constructor
/// </summary>
// ***********************************************************************/
public ForgottenPassword() : base() {
recoveryMethod = CSContext.Current.SiteSettings.PasswordRecovery;
}
#endregion
protected void ToggleEmail(bool show)
{
FindControl("Email_Row").Visible = show;
}
protected void ToggleQA(bool show)
{
FindControl("Question_Row").Visible = show;
FindControl("Answer_Row").Visible = show;
}
#region Initialize skin
// *********************************************************************
// Initializeskin
//
/// <summary>
/// Initialize the control template and populate the control with values
/// </summary>
// ***********************************************************************/
protected override void AttachChildControls() {
// Text box for email address
emailAddress = (TextBox) FindControl("EmailAddress");
// Button to submit
sendPasswordButton = (Button) FindControl("SendPassword");
sendPasswordButton.Text = ResourceManager.GetString("ForgottenPassword_SendButton");
sendPasswordButton.Click += new EventHandler (SendPassword_Click);
// Validators
emailValidator = (RequiredFieldValidator) FindControl("EmailValidator");
emailValidator.ErrorMessage = ResourceManager.GetString("ForgottenPassword_RequiredField");
emailRegexValidator = (RegularExpressionValidator) FindControl("EmailRegexValidator");
emailRegexValidator.ErrorMessage = ResourceManager.GetString("ForgottenPassword_InvalidEmailAddress");
validationMsg = (ValidationSummary) FindControl("ValidationMsg");
// Next Button
nextButton = (Button) FindControl("Next");
nextButton.Text = ResourceManager.GetString("ForgottenPassword_NextButton");
nextButton.Click += new EventHandler (Next_Click);
sQuestion = FindControl("SQuestion") as Literal;
sAnswer = (TextBox) FindControl("SAnswer");
sAnswerValidator = (RequiredFieldValidator) FindControl("SAnswerValidator");
sAnswerValidator.ErrorMessage = ResourceManager.GetString("ForgottenPassword_SecretAnswerRequired");
sAnswerRegexValidator = (RegularExpressionValidator) FindControl("SAnswerRegexValidator");
sAnswerRegexValidator.ErrorMessage = ResourceManager.GetString("ForgottenPassword_InvalidSecretAnswer");
// Reset common validators
emailValidator.IsValid = true;
emailRegexValidator.IsValid = true;
ToggleEmail(true);
ToggleQA(false);
if (recoveryMethod == CommunityServer.Components.PasswordRecovery.Reset) {
// Reset
nextButton.Visible = false;
sendPasswordButton.Visible = true;
}
else {
nextButton.Visible = true;
sendPasswordButton.Visible = false;
// Reset few more validators
sAnswerValidator.IsValid = true;
sAnswerRegexValidator.IsValid = true;
}
}
#endregion
#region Events
void Next_Click(Object sender, EventArgs e) {
User user = ValidateUserOnStep(this.Step);
// If null there was a validation error
//
if (user == null)
return;
// If PasswordQuestion is not set, then stop here
// and Reset user's password.
//
if (Globals.IsNullorEmpty(user.PasswordQuestion))
{
// Update user's password in datastore & display operation's status
ResultMessage(user.ResetPassword());
return;
}
// Go on to next step
//
this.Step = 2;
sQuestion.Text = user.PasswordQuestion;
ToggleEmail(false);
ToggleQA(true);
nextButton.Visible = false;
sendPasswordButton.Visible = true;
}
void SendPassword_Click(Object sender, EventArgs e) {
User user = null;
// Validate on current step
user = ValidateUserOnStep(this.Step);
// If null there was a validation error
if(user == null)
return;
if(recoveryMethod == CommunityServer.Components.PasswordRecovery.Reset)
ResultMessage(user.ResetPassword());
else
ResultMessage(user.ResetPassword(sAnswer.Text));
}
#endregion
#region Helper Methods & Properties
public int Step {
get {
Object state = ViewState["RecoveryStep"];
if (state != null)
return (int)state;
else
return 1;
}
set {
ViewState["RecoveryStep"] = value;
}
}
private void ResultMessage(bool result)
{
Literal title = FindControl("MessageTitle") as Literal;
Literal body = FindControl("MessageBody") as Literal;
//No problem looking up messages by type, but should not be called exception type!
CSExceptionType ex = result ? CSExceptionType.UserPasswordResetSuccess : CSExceptionType.UserPasswordChangeFailed;
CommunityServer.Components.Message message = ResourceManager.GetMessage(ex);
title.Text = message.Title;
body.Text = message.Body;
}
private User ValidateUserOnStep(int step) {
// No mather what recovery method we use.
// We check step no. instead.
//
User user = null;
if (step == 1 || step == 2) {
// Validate step 1 and 2
//
// Fire Validators
emailValidator.Validate();
emailRegexValidator.Validate();
if (!emailValidator.IsValid || !emailRegexValidator.IsValid)
return null;
// Look up the user
user = Users.FindUserByEmail(emailAddress.Text);
// Is valid?
if (user == null || user.UserID == 0) {
emailValidator.ErrorMessage = ResourceManager.GetString("ForgottenPassword_EmailNotFound");
emailValidator.IsValid = false;
return null;
}
}
if (step == 2) {
// Validate step 2 only
//
// Fire Validators
sAnswerValidator.Validate();
sAnswerRegexValidator.Validate();
if (!sAnswerValidator.IsValid || !sAnswerRegexValidator.IsValid)
return null;
}
return user;
}
#endregion
/// <summary>
/// Override this method to attach templated or external skin controls to local references.
/// </summary>
/// <remarks>
/// This will only be called if the non-default skin is used.
/// </remarks>
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -