| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351 |
- //
- // System.Web.UI.WebControls.PasswordRecovery.cs
- //
- // Authors:
- // Vladimir Krasnov ([email protected])
- //
- // (C) 2006 Mainsoft, Inc (http://www.mainsoft.com)
- //
- // Permission is hereby granted, free of charge, to any person obtaining
- // a copy of this software and associated documentation files (the
- // "Software"), to deal in the Software without restriction, including
- // without limitation the rights to use, copy, modify, merge, publish,
- // distribute, sublicense, and/or sell copies of the Software, and to
- // permit persons to whom the Software is furnished to do so, subject to
- // the following conditions:
- //
- // The above copyright notice and this permission notice shall be
- // included in all copies or substantial portions of the Software.
- //
- // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
- // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
- // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
- // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
- // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
- // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
- // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
- //
- #if NET_2_0
- using System;
- using System.Net.Mail;
- using System.Collections;
- using System.Collections.Specialized;
- using System.ComponentModel;
- using System.Drawing.Design;
- using System.Web;
- using System.Web.UI;
- using System.Web.Security;
- namespace System.Web.UI.WebControls
- {
- [Bindable (false)]
- public class PasswordRecovery : CompositeControl
- {
- static readonly object answerLookupErrorEvent = new object ();
- static readonly object sendingMailEvent = new object ();
- static readonly object sendMailErrorEvent = new object ();
- static readonly object userLookupErrorEvent = new object ();
- static readonly object verifyingAnswerEvent = new object ();
- static readonly object verifyingUserEvent = new object ();
-
- public static readonly string SubmitButtonCommandName = "Submit";
- TableItemStyle _failureTextStyle;
- TableItemStyle _hyperLinkStyle;
- TableItemStyle _instructionTextStyle;
- TableItemStyle _labelStyle;
- Style _submitButtonStyle;
- TableItemStyle _successTextStyle;
- Style _textBoxStyle;
- TableItemStyle _titleTextStyle;
- Style _validatorTextStyle;
- MailDefinition _mailDefinition;
- MembershipProvider _provider = null;
- ITemplate _questionTemplate = null;
- ITemplate _successTemplate = null;
- ITemplate _userNameTemplate = null;
- QuestionContainer _questionTemplateContainer = null;
- SuccessContainer _successTemplateContainer = null;
- UserNameContainer _userNameTemplateContainer = null;
- PasswordReciveryStep _currentStep = PasswordReciveryStep.StepUserName;
- string _username = null;
- string _answer = null;
- EventHandlerList events = new EventHandlerList ();
- #region Events
- public event EventHandler AnswerLookupError {
- add { events.AddHandler (answerLookupErrorEvent, value); }
- remove { events.RemoveHandler (answerLookupErrorEvent, value); }
- }
-
- public event MailMessageEventHandler SendingMail {
- add { events.AddHandler (sendingMailEvent, value); }
- remove { events.RemoveHandler (sendingMailEvent, value); }
- }
-
- public event SendMailErrorEventHandler SendMailError {
- add { events.AddHandler (sendMailErrorEvent, value); }
- remove { events.RemoveHandler (sendMailErrorEvent, value); }
- }
-
- public event EventHandler UserLookupError {
- add { events.AddHandler (userLookupErrorEvent, value); }
- remove { events.RemoveHandler (userLookupErrorEvent, value); }
- }
-
- public event LoginCancelEventHandler VerifyingAnswer {
- add { events.AddHandler (verifyingAnswerEvent, value); }
- remove { events.RemoveHandler (verifyingAnswerEvent, value); }
- }
-
- public event LoginCancelEventHandler VerifyingUser {
- add { events.AddHandler (verifyingUserEvent, value); }
- remove { events.RemoveHandler (verifyingUserEvent, value); }
- }
- #endregion
- public PasswordRecovery ()
- {
- }
- [Themeable (false)]
- public virtual string Answer
- {
- get { return _answer != null ? _answer : string.Empty; }
- }
- [Localizable (true)]
- public virtual string AnswerLabelText
- {
- get { return ViewState.GetString ("AnswerLabelText", "Answer:"); }
- set { ViewState ["AnswerLabelText"] = value; }
- }
- [Localizable (true)]
- public virtual string AnswerRequiredErrorMessage
- {
- get { return ViewState.GetString ("AnswerRequiredErrorMessage", "Answer is required."); }
- set { ViewState ["AnswerRequiredErrorMessage"] = value; }
- }
- public virtual int BorderPadding
- {
- get { return ViewState.GetInt ("BorderPadding", 1); }
- set
- {
- if (value < -1)
- throw new ArgumentOutOfRangeException ();
- ViewState ["BorderPadding"] = value;
- }
- }
- [Localizable (true)]
- public virtual string GeneralFailureText
- {
- get { return ViewState.GetString ("GeneralFailureText", "Your attempt to retrieve your password was not successful. Please try again."); }
- set { ViewState ["GeneralFailureText"] = value; }
- }
- public virtual string HelpPageIconUrl
- {
- get { return ViewState.GetString ("HelpPageIconUrl", String.Empty); }
- set { ViewState ["HelpPageIconUrl"] = value; }
- }
- [Localizable (true)]
- public virtual string HelpPageText
- {
- get { return ViewState.GetString ("HelpPageText", String.Empty); }
- set { ViewState ["HelpPageText"] = value; }
- }
- public virtual string HelpPageUrl
- {
- get { return ViewState.GetString ("HelpPageUrl", String.Empty); }
- set { ViewState ["HelpPageUrl"] = value; }
- }
- [Themeable (false)]
- public MailDefinition MailDefinition
- {
- get
- {
- if (_mailDefinition == null) {
- _mailDefinition = new MailDefinition ();
- if (IsTrackingViewState)
- ((IStateManager) _mailDefinition).TrackViewState ();
- }
- return _mailDefinition;
- }
- }
- [Themeable (false)]
- public virtual string MembershipProvider
- {
- get { return ViewState.GetString ("MembershipProvider", String.Empty); }
- set { ViewState ["MembershipProvider"] = value; }
- }
- [Themeable (false)]
- public virtual string Question
- {
- get { return ViewState.GetString ("Question", ""); }
- private set { ViewState ["Question"] = value; }
- }
- [Localizable (true)]
- public virtual string QuestionFailureText
- {
- get { return ViewState.GetString ("QuestionFailureText", "Your answer could not be verified. Please try again."); }
- set { ViewState ["QuestionFailureText"] = value; }
- }
- [Localizable (true)]
- public virtual string QuestionInstructionText
- {
- get { return ViewState.GetString ("QuestionInstructionText", "Answer the following question to receive your password."); }
- set { ViewState ["QuestionInstructionText"] = value; }
- }
- [Localizable (true)]
- public virtual string QuestionLabelText
- {
- get { return ViewState.GetString ("QuestionLabelText", "Question:"); }
- set { ViewState ["QuestionLabelText"] = value; }
- }
- [Localizable (true)]
- public virtual string QuestionTitleText
- {
- get { return ViewState.GetString ("QuestionTitleText", "Identity Confirmation"); }
- set { ViewState ["QuestionTitleText"] = value; }
- }
- public virtual string SubmitButtonImageUrl
- {
- get { return ViewState.GetString ("SubmitButtonImageUrl", String.Empty); }
- set { ViewState ["SubmitButtonImageUrl"] = value; }
- }
- [Localizable (true)]
- public virtual string SubmitButtonText
- {
- get { return ViewState.GetString ("SubmitButtonText", "Submit"); }
- set { ViewState ["SubmitButtonText"] = value; }
- }
- public virtual ButtonType SubmitButtonType
- {
- get
- {
- object o = ViewState ["SubmitButtonType"];
- return (o == null) ? ButtonType.Button : (ButtonType) o;
- }
- set
- {
- if ((value < ButtonType.Button) || (value > ButtonType.Link))
- throw new ArgumentOutOfRangeException ("SubmitButtonType");
- ViewState ["SubmitButtonType"] = (int) value;
- }
- }
- [Themeable (false)]
- public virtual string SuccessPageUrl
- {
- get { return ViewState.GetString ("SuccessPageUrl", String.Empty); }
- set { ViewState ["SuccessPageUrl"] = value; }
- }
- [Localizable (true)]
- public virtual string SuccessText
- {
- get { return ViewState.GetString ("SuccessText", "Your password has been sent to you."); }
- set { ViewState ["SuccessText"] = value; }
- }
- public virtual LoginTextLayout TextLayout
- {
- get
- {
- object o = ViewState ["TextLayout"];
- return (o == null) ? LoginTextLayout.TextOnLeft : (LoginTextLayout) o;
- }
- set
- {
- if ((value < LoginTextLayout.TextOnLeft) || (value > LoginTextLayout.TextOnTop))
- throw new ArgumentOutOfRangeException ("TextLayout");
- ViewState ["TextLayout"] = (int) value;
- }
- }
- [Localizable (true)]
- public virtual string UserName
- {
- get { return _username != null ? _username : ""; }
- set { _username = value; }
- }
- [Localizable (true)]
- public virtual string UserNameFailureText
- {
- get { return ViewState.GetString ("UserNameFailureText", "We were unable to access your information. Please try again."); }
- set { ViewState ["UserNameFailureText"] = value; }
- }
- [Localizable (true)]
- public virtual string UserNameInstructionText
- {
- get { return ViewState.GetString ("UserNameInstructionText", "Enter your User Name to receive your password."); }
- set { ViewState ["UserNameInstructionText"] = value; }
- }
- [Localizable (true)]
- public virtual string UserNameLabelText
- {
- get { return ViewState.GetString ("UserNameLabelText", "User Name:"); }
- set { ViewState ["UserNameLabelText"] = value; }
- }
- [Localizable (true)]
- public virtual string UserNameRequiredErrorMessage
- {
- get { return ViewState.GetString ("UserNameRequiredErrorMessage", "User Name is required."); }
- set { ViewState ["UserNameRequiredErrorMessage"] = value; }
- }
- [Localizable (true)]
- public virtual string UserNameTitleText
- {
- get { return ViewState.GetString ("UserNameTitleText", "Forgot Your Password?"); }
- set { ViewState ["UserNameTitleText"] = value; }
- }
- [TemplateContainer (typeof (PasswordRecovery))]
- public virtual ITemplate QuestionTemplate
- {
- get { return _questionTemplate; }
- set { _questionTemplate = value; }
- }
- public Control QuestionTemplateContainer
- {
- get
- {
- if (_questionTemplateContainer == null)
- _questionTemplateContainer = new QuestionContainer (this);
- return _questionTemplateContainer;
- }
- }
- [TemplateContainer (typeof (PasswordRecovery))]
- public virtual ITemplate SuccessTemplate
- {
- get { return _successTemplate; }
- set { _successTemplate = value; }
- }
- public Control SuccessTemplateContainer
- {
- get
- {
- if (_successTemplateContainer == null)
- _successTemplateContainer = new SuccessContainer (this);
- return _successTemplateContainer;
- }
- }
- [TemplateContainer (typeof (PasswordRecovery))]
- public virtual ITemplate UserNameTemplate
- {
- get { return _userNameTemplate; }
- set { _userNameTemplate = value; }
- }
- public Control UserNameTemplateContainer
- {
- get
- {
- if (_userNameTemplateContainer == null)
- _userNameTemplateContainer = new UserNameContainer (this);
- return _userNameTemplateContainer;
- }
- }
- public TableItemStyle FailureTextStyle
- {
- get
- {
- if (_failureTextStyle == null) {
- _failureTextStyle = new TableItemStyle ();
- if (IsTrackingViewState)
- _failureTextStyle.TrackViewState ();
- }
- return _failureTextStyle;
- }
- }
- public TableItemStyle HyperLinkStyle
- {
- get
- {
- if (_hyperLinkStyle == null) {
- _hyperLinkStyle = new TableItemStyle ();
- if (IsTrackingViewState)
- _hyperLinkStyle.TrackViewState ();
- }
- return _hyperLinkStyle;
- }
- }
- public TableItemStyle InstructionTextStyle
- {
- get
- {
- if (_instructionTextStyle == null) {
- _instructionTextStyle = new TableItemStyle ();
- if (IsTrackingViewState)
- _instructionTextStyle.TrackViewState ();
- }
- return _instructionTextStyle;
- }
- }
- public TableItemStyle LabelStyle
- {
- get
- {
- if (_labelStyle == null) {
- _labelStyle = new TableItemStyle ();
- if (IsTrackingViewState)
- _labelStyle.TrackViewState ();
- }
- return _labelStyle;
- }
- }
- public Style SubmitButtonStyle
- {
- get
- {
- if (_submitButtonStyle == null) {
- _submitButtonStyle = new TableItemStyle ();
- if (IsTrackingViewState)
- _submitButtonStyle.TrackViewState ();
- }
- return _submitButtonStyle;
- }
- }
- public TableItemStyle SuccessTextStyle
- {
- get
- {
- if (_successTextStyle == null) {
- _successTextStyle = new TableItemStyle ();
- if (IsTrackingViewState)
- _successTextStyle.TrackViewState ();
- }
- return _successTextStyle;
- }
- }
- public Style TextBoxStyle
- {
- get
- {
- if (_textBoxStyle == null) {
- _textBoxStyle = new TableItemStyle ();
- if (IsTrackingViewState)
- _textBoxStyle.TrackViewState ();
- }
- return _textBoxStyle;
- }
- }
- public TableItemStyle TitleTextStyle
- {
- get
- {
- if (_titleTextStyle == null) {
- _titleTextStyle = new TableItemStyle ();
- if (IsTrackingViewState)
- _titleTextStyle.TrackViewState ();
- }
- return _titleTextStyle;
- }
- }
- public Style ValidatorTextStyle
- {
- get
- {
- if (_validatorTextStyle == null) {
- _validatorTextStyle = new TableItemStyle ();
- if (IsTrackingViewState)
- _validatorTextStyle.TrackViewState ();
- }
- return _validatorTextStyle;
- }
- }
- #region Protected Properties
- protected override HtmlTextWriterTag TagKey
- {
- get { return HtmlTextWriterTag.Table; }
- }
- internal virtual MembershipProvider MembershipProviderInternal
- {
- get
- {
- if (_provider == null)
- InitMemberShipProvider ();
- return _provider;
- }
- }
- #endregion
- protected internal override void CreateChildControls ()
- {
- ITemplate userNameTemplate = UserNameTemplate;
- if (userNameTemplate == null)
- userNameTemplate = new UserNameDefaultTemplate (this);
- ((UserNameContainer) UserNameTemplateContainer).InstantiateTemplate (userNameTemplate);
- ITemplate questionTemplate = QuestionTemplate;
- if (questionTemplate == null)
- questionTemplate = new QuestionDefaultTemplate (this);
- ((QuestionContainer) QuestionTemplateContainer).InstantiateTemplate (questionTemplate);
- ITemplate successTemplate = SuccessTemplate;
- if (successTemplate == null)
- successTemplate = new SuccessDefaultTemplate (this);
- ((SuccessContainer) SuccessTemplateContainer).InstantiateTemplate (successTemplate);
- Controls.AddAt (0, UserNameTemplateContainer);
- Controls.AddAt (1, QuestionTemplateContainer);
- Controls.AddAt (2, SuccessTemplateContainer);
- IEditableTextControl editable;
- editable = ((UserNameContainer) UserNameTemplateContainer).UserNameTextBox;
- if (editable != null)
- editable.TextChanged += new EventHandler (UserName_TextChanged);
- editable = ((QuestionContainer) QuestionTemplateContainer).AnswerTextBox;
- if (editable != null)
- editable.TextChanged += new EventHandler (Answer_TextChanged);
- }
- #region Protected methods
- protected internal override void Render (HtmlTextWriter writer)
- {
- ((QuestionContainer) QuestionTemplateContainer).UpdateChildControls ();
- for (int i = 0; i < Controls.Count; i++)
- if (Controls [i].Visible)
- Controls [i].Render (writer);
- }
- protected internal override void LoadControlState (object savedState)
- {
- if (savedState == null) return;
- object [] state = (object []) savedState;
- base.LoadControlState (state [0]);
- _currentStep = (PasswordReciveryStep) state [1];
- _username = (string) state [2];
- }
- protected internal override object SaveControlState ()
- {
- object state = base.SaveControlState ();
- return new object [] { state, _currentStep, _username };
- }
- protected override void TrackViewState ()
- {
- base.TrackViewState ();
- if (_failureTextStyle != null)
- _failureTextStyle.TrackViewState ();
- if (_hyperLinkStyle != null)
- _hyperLinkStyle.TrackViewState ();
- if (_instructionTextStyle != null)
- _instructionTextStyle.TrackViewState ();
- if (_labelStyle != null)
- _labelStyle.TrackViewState ();
- if (_submitButtonStyle != null)
- _submitButtonStyle.TrackViewState ();
- if (_successTextStyle != null)
- _successTextStyle.TrackViewState ();
- if (_textBoxStyle != null)
- _textBoxStyle.TrackViewState ();
- if (_titleTextStyle != null)
- _titleTextStyle.TrackViewState ();
- if (_validatorTextStyle != null)
- _validatorTextStyle.TrackViewState ();
- if (_mailDefinition != null)
- ((IStateManager) _mailDefinition).TrackViewState ();
- }
- protected override void LoadViewState (object savedState)
- {
- if (savedState == null)
- return;
- object [] states = (object []) savedState;
- base.LoadViewState (states [0]);
- if (states [1] != null)
- FailureTextStyle.LoadViewState (states [1]);
- if (states [2] != null)
- HyperLinkStyle.LoadViewState (states [2]);
- if (states [3] != null)
- InstructionTextStyle.LoadViewState (states [3]);
- if (states [4] != null)
- LabelStyle.LoadViewState (states [4]);
- if (states [5] != null)
- SubmitButtonStyle.LoadViewState (states [5]);
- if (states [6] != null)
- SuccessTextStyle.LoadViewState (states [6]);
- if (states [7] != null)
- TextBoxStyle.LoadViewState (states [7]);
- if (states [8] != null)
- TitleTextStyle.LoadViewState (states [8]);
- if (states [9] != null)
- ValidatorTextStyle.LoadViewState (states [9]);
- if (states [10] != null)
- ((IStateManager) MailDefinition).LoadViewState (states [10]);
- }
- protected override object SaveViewState ()
- {
- object [] states = new object [11];
- states [0] = base.SaveViewState ();
- if (_failureTextStyle != null)
- states [1] = _failureTextStyle.SaveViewState ();
- if (_hyperLinkStyle != null)
- states [2] = _hyperLinkStyle.SaveViewState ();
- if (_instructionTextStyle != null)
- states [3] = _instructionTextStyle.SaveViewState ();
- if (_labelStyle != null)
- states [4] = _labelStyle.SaveViewState ();
- if (_submitButtonStyle != null)
- states [5] = _submitButtonStyle.SaveViewState ();
- if (_successTextStyle != null)
- states [6] = _successTextStyle.SaveViewState ();
- if (_textBoxStyle != null)
- states [7] = _textBoxStyle.SaveViewState ();
- if (_titleTextStyle != null)
- states [8] = _titleTextStyle.SaveViewState ();
- if (_validatorTextStyle != null)
- states [9] = _validatorTextStyle.SaveViewState ();
- if (_mailDefinition != null)
- states [10] = ((IStateManager) _mailDefinition).SaveViewState ();
- for (int i = 0; i < states.Length; i++) {
- if (states [i] != null)
- return states;
- }
- return null;
- }
- #endregion
- void ProcessCommand (CommandEventArgs args)
- {
- if (!Page.IsValid)
- return;
- switch (_currentStep) {
- case PasswordReciveryStep.StepUserName:
- ProcessUserName ();
- break;
- case PasswordReciveryStep.StepAnswer:
- ProcessUserAnswer ();
- break;
- }
- }
- void ProcessUserName ()
- {
- LoginCancelEventArgs args = new LoginCancelEventArgs ();
- OnVerifyingUser (args);
- if (args.Cancel)
- return;
- MembershipUser user = MembershipProviderInternal.GetUser (UserName, false);
- if (user == null) {
- OnUserLookupError (EventArgs.Empty);
- ((UserNameContainer) UserNameTemplateContainer).FailureTextLiteral.Text = UserNameFailureText;
- return;
- }
- if (!MembershipProviderInternal.RequiresQuestionAndAnswer) {
- GenerateAndSendEmail ();
- _currentStep = PasswordReciveryStep.StepSuccess;
- return;
- }
- Question = user.PasswordQuestion;
- _currentStep = PasswordReciveryStep.StepAnswer;
- return;
- }
- void ProcessUserAnswer ()
- {
- LoginCancelEventArgs args = new LoginCancelEventArgs ();
- OnVerifyingAnswer (args);
- if (args.Cancel)
- return;
- MembershipUser user = MembershipProviderInternal.GetUser (UserName, false);
- if (user == null || string.IsNullOrEmpty (user.Email)) {
- ((QuestionContainer) QuestionTemplateContainer).FailureTextLiteral.Text = GeneralFailureText;
- return;
- }
- GenerateAndSendEmail ();
- _currentStep = PasswordReciveryStep.StepSuccess;
- return;
- }
- void GenerateAndSendEmail ()
- {
- string newPassword = "";
- try {
- if (MembershipProviderInternal.EnablePasswordRetrieval) {
- newPassword = MembershipProviderInternal.GetPassword (UserName, Answer);
- }
- else if (MembershipProviderInternal.EnablePasswordReset) {
- newPassword = MembershipProviderInternal.ResetPassword (UserName, Answer);
- }
- else
- throw new HttpException ("Membership provider does not support password retrieval or reset.");
- }
- catch (MembershipPasswordException) {
- OnAnswerLookupError (EventArgs.Empty);
- ((QuestionContainer) QuestionTemplateContainer).FailureTextLiteral.Text = QuestionFailureText;
- return;
- }
- SendPasswordByMail (UserName, newPassword);
- }
- void InitMemberShipProvider ()
- {
- string mp = MembershipProvider;
- _provider = (mp.Length == 0) ? _provider = Membership.Provider : Membership.Providers [mp];
- if (_provider == null)
- throw new HttpException (Locale.GetText ("No provider named '{0}' could be found.", mp));
- }
- void SendPasswordByMail (string username, string password)
- {
- MembershipUser user = MembershipProviderInternal.GetUser (UserName, false);
- if (user == null)
- return;
- string messageText = "Please return to the site and log in using the following information.\n" +
- "User Name: <%USERNAME%>\nPassword: <%PASSWORD%>";
- ListDictionary dictionary = new ListDictionary (StringComparer.OrdinalIgnoreCase);
- dictionary.Add ("<%USERNAME%>", username);
- dictionary.Add ("<% UserName %>", username);
- dictionary.Add ("<%PASSWORD%>", password);
- dictionary.Add ("<% Password %>", password);
- MailMessage message = null;
-
- if (MailDefinition.BodyFileName.Length == 0)
- message = MailDefinition.CreateMailMessage (user.Email, dictionary, messageText, this);
- else
- message = MailDefinition.CreateMailMessage (user.Email, dictionary, this);
- if (string.IsNullOrEmpty (message.Subject))
- message.Subject = "Password";
- MailMessageEventArgs args = new MailMessageEventArgs (message);
- OnSendingMail (args);
- SmtpClient smtpClient = new SmtpClient ();
- try {
- smtpClient.Send (message);
- }
- catch (Exception e) {
- SendMailErrorEventArgs mailArgs = new SendMailErrorEventArgs (e);
- OnSendMailError (mailArgs);
- if (!mailArgs.Handled)
- throw e;
- }
- }
- #region Event handlers
- protected virtual void OnAnswerLookupError (EventArgs e)
- {
- }
- protected override bool OnBubbleEvent (object source, EventArgs e)
- {
- CommandEventArgs args = e as CommandEventArgs;
- if (e != null && args.CommandName == SubmitButtonCommandName) {
- ProcessCommand (args);
- return true;
- }
- return base.OnBubbleEvent (source, e);
- }
- protected internal override void OnInit (EventArgs e)
- {
- Page.RegisterRequiresControlState (this);
- base.OnInit (e);
- }
- protected internal override void OnPreRender (EventArgs e)
- {
- UserNameTemplateContainer.Visible = false;
- QuestionTemplateContainer.Visible = false;
- SuccessTemplateContainer.Visible = false;
- switch (_currentStep) {
- case PasswordReciveryStep.StepUserName:
- UserNameTemplateContainer.Visible = true;
- break;
- case PasswordReciveryStep.StepAnswer:
- QuestionTemplateContainer.Visible = true;
- break;
- case PasswordReciveryStep.StepSuccess:
- SuccessTemplateContainer.Visible = true;
- break;
- }
- base.OnPreRender (e);
- }
- protected virtual void OnSendingMail (MailMessageEventArgs e)
- {
- MailMessageEventHandler eh = events [sendingMailEvent] as MailMessageEventHandler;
- if (eh != null)
- eh (this, e);
- }
- protected virtual void OnSendMailError (SendMailErrorEventArgs e)
- {
- SendMailErrorEventHandler eh = events [sendingMailEvent] as SendMailErrorEventHandler;
- if (eh != null)
- eh (this, e);
- }
- protected virtual void OnUserLookupError (EventArgs e)
- {
- EventHandler eh = events [userLookupErrorEvent] as EventHandler;
- if (eh != null)
- eh (this, e);
- }
- protected virtual void OnVerifyingAnswer (LoginCancelEventArgs e)
- {
- LoginCancelEventHandler eh = events [verifyingAnswerEvent] as LoginCancelEventHandler;
- if (eh != null)
- eh (this, e);
- }
- protected virtual void OnVerifyingUser (LoginCancelEventArgs e)
- {
- LoginCancelEventHandler eh = events [verifyingUserEvent] as LoginCancelEventHandler;
- if (eh != null)
- eh (this, e);
- }
- #endregion
- #region Private Event Handlers
- void UserName_TextChanged (object sender, EventArgs e)
- {
- UserName = ((ITextControl) sender).Text;
- }
- void Answer_TextChanged (object sender, EventArgs e)
- {
- _answer = ((ITextControl) sender).Text;
- }
- #endregion
- [MonoTODO ("Not implemented")]
- protected override void SetDesignModeState (IDictionary data)
- {
- throw new NotImplementedException ();
- }
- abstract class BasePasswordRecoveryContainer : Table, INamingContainer
- {
- protected readonly PasswordRecovery _owner = null;
- TableCell _containerCell = null;
- public BasePasswordRecoveryContainer (PasswordRecovery owner)
- {
- _owner = owner;
- InitTable ();
- }
- public void InstantiateTemplate (ITemplate template)
- {
- template.InstantiateIn (_containerCell);
- }
- void InitTable ()
- {
- Attributes.Add ("ID", _owner.ID);
- CellSpacing = 0;
- CellPadding = _owner.BorderPadding;
- _containerCell = new TableCell ();
- TableRow row = new TableRow ();
- row.Cells.Add (_containerCell);
- Rows.Add (row);
- }
- protected internal override void OnPreRender (EventArgs e)
- {
- ApplyStyle (_owner.ControlStyle);
- base.OnPreRender (e);
- }
- public abstract void UpdateChildControls();
- }
- sealed class QuestionContainer : BasePasswordRecoveryContainer
- {
- public QuestionContainer (PasswordRecovery owner)
- : base (owner)
- {
- }
- // Requried controls
- public IEditableTextControl AnswerTextBox
- {
- get
- {
- Control c = FindControl ("Answer");
- if (c == null)
- throw new HttpException ("QuestionTemplate does not contain an IEditableTextControl with ID Answer for the username.");
- return c as IEditableTextControl;
- }
- }
- // Optional controls
- public Literal UserNameLiteral
- {
- get { return FindControl ("UserName") as Literal; }
- }
- public Literal QuestionLiteral
- {
- get { return FindControl ("Question") as Literal; }
- }
- public Literal FailureTextLiteral
- {
- get { return FindControl ("FailureText") as Literal; }
- }
- public override void UpdateChildControls ()
- {
- if (UserNameLiteral != null)
- UserNameLiteral.Text = _owner.UserName;
- if (QuestionLiteral != null)
- QuestionLiteral.Text = _owner.Question;
- }
- }
- sealed class SuccessContainer : BasePasswordRecoveryContainer
- {
- public SuccessContainer (PasswordRecovery owner)
- : base (owner)
- {
- }
- public override void UpdateChildControls ()
- {
- }
- }
- sealed class UserNameContainer : BasePasswordRecoveryContainer
- {
- public UserNameContainer (PasswordRecovery owner)
- : base (owner)
- {
- }
- // Requried controls
- public IEditableTextControl UserNameTextBox
- {
- get
- {
- Control c = FindControl ("UserName");
- if (c == null)
- throw new HttpException ("UserNameTemplate does not contain an IEditableTextControl with ID UserName for the username.");
- return c as IEditableTextControl;
- }
- }
- // Optional controls
- public ITextControl FailureTextLiteral
- {
- get { return FindControl ("FailureText") as ITextControl; }
- }
- public override void UpdateChildControls ()
- {
- }
- }
- class TemplateUtils
- {
- public static TableRow CreateRow(Control c1, Control c2, Style s1, Style s2, bool twoCells)
- {
- TableRow row = new TableRow ();
- TableCell cell1 = new TableCell ();
- cell1.Controls.Add (c1);
- if (s1 != null)
- cell1.ApplyStyle (s1);
- row.Cells.Add (cell1);
- if (c2 != null) {
- TableCell cell2 = new TableCell ();
- cell2.Controls.Add (c2);
- if (s2 != null)
- cell2.ApplyStyle (s2);
- row.Cells.Add (cell2);
- cell1.HorizontalAlign = HorizontalAlign.Right;
- cell2.HorizontalAlign = HorizontalAlign.Left;
- }
- else {
- cell1.HorizontalAlign = HorizontalAlign.Center;
- if (twoCells)
- cell1.ColumnSpan = 2;
- }
-
- return row;
- }
- public static TableRow CreateHelpRow (string pageUrl, string linkText, string linkIcon, Style linkStyle, bool twoCells)
- {
- TableRow row = new TableRow ();
- TableCell cell1 = new TableCell ();
- if (linkIcon.Length > 0) {
- Image img = new Image ();
- img.ImageUrl = linkIcon;
- cell1.Controls.Add (img);
- }
- if (linkText.Length > 0) {
- HyperLink link = new HyperLink ();
- link.NavigateUrl = pageUrl;
- link.Text = linkText;
- link.ControlStyle.CopyTextStylesFrom (linkStyle);
- cell1.Controls.Add (link);
- }
- if (twoCells)
- cell1.ColumnSpan = 2;
- row.ControlStyle.CopyFrom (linkStyle);
- row.Cells.Add (cell1);
- return row;
- }
- }
- sealed class UserNameDefaultTemplate : ITemplate
- {
- readonly PasswordRecovery _owner = null;
- public UserNameDefaultTemplate (PasswordRecovery _owner)
- {
- this._owner = _owner;
- }
- public void InstantiateIn (Control container)
- {
- Table table = new Table ();
- table.CellPadding = 0;
- bool twoCells = _owner.TextLayout == LoginTextLayout.TextOnLeft;
- // row 0
- table.Rows.Add (
- TemplateUtils.CreateRow (new LiteralControl (_owner.UserNameTitleText), null, _owner.TitleTextStyle, null, twoCells));
- // row 1
- table.Rows.Add (
- TemplateUtils.CreateRow (new LiteralControl (_owner.UserNameInstructionText), null, _owner.InstructionTextStyle, null, twoCells));
- // row 2
- TextBox UserNameTextBox = new TextBox ();
- UserNameTextBox.ID = "UserName";
- UserNameTextBox.Text = _owner.UserName;
- UserNameTextBox.ApplyStyle (_owner.TextBoxStyle);
- Label UserNameLabel = new Label ();
- UserNameLabel.ID = "UserNameLabel";
- UserNameLabel.AssociatedControlID = "UserName";
- UserNameLabel.Text = _owner.UserNameLabelText;
- UserNameLabel.ApplyStyle (_owner.LabelStyle);
- RequiredFieldValidator UserNameRequired = new RequiredFieldValidator ();
- UserNameRequired.ID = "UserNameRequired";
- UserNameRequired.ControlToValidate = "UserName";
- UserNameRequired.ErrorMessage = _owner.UserNameRequiredErrorMessage;
- UserNameRequired.ToolTip = _owner.UserNameRequiredErrorMessage;
- UserNameRequired.Text = "*";
- UserNameRequired.ValidationGroup = _owner.ID;
- UserNameRequired.ApplyStyle (_owner.ValidatorTextStyle);
- if (twoCells) {
- TableRow row = TemplateUtils.CreateRow (UserNameLabel, UserNameTextBox, null, null, twoCells);
- row.Cells [1].Controls.Add (UserNameRequired);
- table.Rows.Add (row);
- }
- else {
- table.Rows.Add (TemplateUtils.CreateRow (UserNameLabel, null, null, null, twoCells));
- TableRow row = TemplateUtils.CreateRow (UserNameTextBox, null, null, null, twoCells);
- row.Cells [0].Controls.Add (UserNameRequired);
- table.Rows.Add (row);
- }
- // row 3
- Literal FailureText = new Literal ();
- FailureText.ID = "FailureText";
- if (_owner.FailureTextStyle.ForeColor.IsEmpty)
- _owner.FailureTextStyle.ForeColor = System.Drawing.Color.Red;
- table.Rows.Add (TemplateUtils.CreateRow (FailureText, null, _owner.FailureTextStyle, null, twoCells));
- // row 4
- WebControl SubmitButton = null;
- switch (_owner.SubmitButtonType) {
- case ButtonType.Button:
- SubmitButton = new Button ();
- break;
- case ButtonType.Image:
- SubmitButton = new ImageButton ();
- break;
- case ButtonType.Link:
- SubmitButton = new LinkButton ();
- break;
- }
- SubmitButton.ID = "SubmitButton";
- SubmitButton.ApplyStyle (_owner.SubmitButtonStyle);
- ((IButtonControl) SubmitButton).CommandName = PasswordRecovery.SubmitButtonCommandName;
- ((IButtonControl) SubmitButton).Text = _owner.SubmitButtonText;
- ((IButtonControl) SubmitButton).ValidationGroup = _owner.ID;
- TableRow buttonRow = TemplateUtils.CreateRow (SubmitButton, null, null, null, twoCells);
- buttonRow.Cells [0].HorizontalAlign = HorizontalAlign.Right;
- table.Rows.Add (buttonRow);
- // row 5
- table.Rows.Add (
- TemplateUtils.CreateHelpRow (
- _owner.HelpPageUrl, _owner.HelpPageText, _owner.HelpPageIconUrl, _owner.HyperLinkStyle, twoCells));
- container.Controls.Add (table);
- }
- }
- sealed class QuestionDefaultTemplate : ITemplate
- {
- readonly PasswordRecovery _owner = null;
- public QuestionDefaultTemplate (PasswordRecovery _owner)
- {
- this._owner = _owner;
- }
- public void InstantiateIn (Control container)
- {
- Table table = new Table ();
- table.CellPadding = 0;
- bool twoCells = _owner.TextLayout == LoginTextLayout.TextOnLeft;
- // row 0
- table.Rows.Add (
- TemplateUtils.CreateRow (new LiteralControl (_owner.QuestionTitleText), null, _owner.TitleTextStyle, null, twoCells));
- // row 1
- table.Rows.Add (
- TemplateUtils.CreateRow (new LiteralControl (_owner.QuestionInstructionText), null, _owner.InstructionTextStyle, null, twoCells));
- // row 2
- Literal UserNameLiteral = new Literal ();
- UserNameLiteral.ID = "UserName";
- table.Rows.Add (
- TemplateUtils.CreateRow (new LiteralControl (_owner.UserNameLabelText), UserNameLiteral, _owner.LabelStyle, _owner.LabelStyle, twoCells));
- // row 3
- Literal QuestionLiteral = new Literal ();
- QuestionLiteral.ID = "Question";
- table.Rows.Add (
- TemplateUtils.CreateRow (new LiteralControl (_owner.QuestionLabelText), QuestionLiteral, _owner.LabelStyle, _owner.LabelStyle, twoCells));
- // row 5
- TextBox AnswerTextBox = new TextBox ();
- AnswerTextBox.ID = "Answer";
- AnswerTextBox.ApplyStyle (_owner.TextBoxStyle);
- Label AnswerLabel = new Label ();
- AnswerLabel.ID = "AnswerLabel";
- AnswerLabel.AssociatedControlID = "Answer";
- AnswerLabel.Text = _owner.AnswerLabelText;
- AnswerLabel.ApplyStyle (_owner.LabelStyle);
- RequiredFieldValidator AnswerRequired = new RequiredFieldValidator ();
- AnswerRequired.ID = "AnswerRequired";
- AnswerRequired.ControlToValidate = "Answer";
- AnswerRequired.ErrorMessage = _owner.AnswerRequiredErrorMessage;
- AnswerRequired.ToolTip = _owner.AnswerRequiredErrorMessage;
- AnswerRequired.Text = "*";
- AnswerRequired.ValidationGroup = _owner.ID;
- AnswerRequired.ApplyStyle (_owner.ValidatorTextStyle);
- if (twoCells) {
- TableRow row = TemplateUtils.CreateRow (AnswerLabel, AnswerTextBox, null, null, twoCells);
- row.Cells [1].Controls.Add (AnswerRequired);
- table.Rows.Add (row);
- }
- else {
- table.Rows.Add (TemplateUtils.CreateRow (AnswerLabel, null, null, null, twoCells));
- TableRow row = TemplateUtils.CreateRow (AnswerTextBox, null, null, null, twoCells);
- row.Cells [0].Controls.Add (AnswerRequired);
- table.Rows.Add (row);
- }
- // row 6
- Literal FailureText = new Literal ();
- FailureText.ID = "FailureText";
- if (_owner.FailureTextStyle.ForeColor.IsEmpty)
- _owner.FailureTextStyle.ForeColor = System.Drawing.Color.Red;
- table.Rows.Add (TemplateUtils.CreateRow (FailureText, null, _owner.FailureTextStyle, null, twoCells));
- // row 7
- WebControl SubmitButton = null;
- switch (_owner.SubmitButtonType) {
- case ButtonType.Button:
- SubmitButton = new Button ();
- break;
- case ButtonType.Image:
- SubmitButton = new ImageButton ();
- break;
- case ButtonType.Link:
- SubmitButton = new LinkButton ();
- break;
- }
- SubmitButton.ID = "SubmitButton";
- SubmitButton.ApplyStyle (_owner.SubmitButtonStyle);
- ((IButtonControl) SubmitButton).CommandName = PasswordRecovery.SubmitButtonCommandName;
- ((IButtonControl) SubmitButton).Text = _owner.SubmitButtonText;
- ((IButtonControl) SubmitButton).ValidationGroup = _owner.ID;
- TableRow buttonRow = TemplateUtils.CreateRow (SubmitButton, null, null, null, twoCells);
- buttonRow.Cells [0].HorizontalAlign = HorizontalAlign.Right;
- table.Rows.Add (buttonRow);
- // row 8
- table.Rows.Add (
- TemplateUtils.CreateHelpRow (
- _owner.HelpPageUrl, _owner.HelpPageText, _owner.HelpPageIconUrl, _owner.HyperLinkStyle, twoCells));
- container.Controls.Add (table);
- }
- }
- sealed class SuccessDefaultTemplate : ITemplate
- {
- readonly PasswordRecovery _owner = null;
- public SuccessDefaultTemplate (PasswordRecovery _owner)
- {
- this._owner = _owner;
- }
- public void InstantiateIn (Control container)
- {
- Table table = new Table ();
- table.CellPadding = 0;
- bool twoCells = _owner.TextLayout == LoginTextLayout.TextOnLeft;
- // row 0
- table.Rows.Add (
- TemplateUtils.CreateRow (new LiteralControl (_owner.SuccessText), null, _owner.SuccessTextStyle, null, twoCells));
- container.Controls.Add (table);
- }
- }
- enum PasswordReciveryStep
- {
- StepUserName,
- StepAnswer,
- StepSuccess,
- }
- }
- }
- #endif
|