locked
Exception Handling Issue with not being able to access the exception RRS feed

  • Question

  • User-2066630380 posted

    I've built a custom membershipprovider handling all possible scenarios. One of which is to further deal with manually triggered exceptions. What I can't figure out for some reason is how to access the exception thrown in the OnPasswordChangeError Method. The server.GetLastError() returns a null. and what ever I do I can't get a hold of it.  Or do I need to pass the exception somehow to the OnPasswordError method? The changepassword only returns a bool to trigger the OnPasswordChageError. So I need to be able to access the exceptions here to be able to handle them.

    any help as always much appreciated.

    // On PasswordChangeEvent
    
         if (!r.IsMatch(args.Password))
                {
                    args.FailureInformation =
                      new Exception("Password must be at least 6 characters long and " +
                                        "contain at least one number and one special character.");
                    args.Cancel = true;
    
       		throw args.FailureInformation;
                }
    
    
    
    // OnPasswordErrorEvent
    
         /// <summary>
            /// Raises the <see cref="E:System.Web.UI.WebControls.ChangePassword.ChangePasswordError"/> event when the user's password is not changed.
            /// </summary>
            /// <param name="e">An <see cref="T:System.EventArgs"/> object that contains the event data.</param>
            protected override void OnChangePasswordError(EventArgs e)
            {
                var evt = new MembershipEvent("Password error", this, 6, this.UserName, this.NewPassword);
                evt.Raise();
    
               
                var server = HttpContext.Current.Server;
                Exception error = server.GetLastError();
                this.ChangePasswordFailureText = error.ToString();
                
                base.OnChangePasswordError(e);
            }

    Tuesday, November 18, 2014 9:01 AM

Answers

  • User-97501952 posted

    Hi,

    >What I can't figure out for some reason is how to access the exception thrown in the OnPasswordChangeError Method. 

    Do you mean that you want to access the exception content which is thrown in ChangePassword event from OnPasswordChangeError method? You may have a try to use a label to transfer the exception content. I mean you need to new the exception and catch the exception in your ChangePassword method and assign the exception.ToString to the label. So you will get the label content from OnPasswordChangeError Method. 

    You can refer to the sample code in this link:

    http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.changepassword.changingpassword(v=vs.110).aspx

    > The server.GetLastError() returns a null. and what ever I do I can't get a hold of it.  Or do I need to pass the exception somehow to the OnPasswordError method?

    I think you may not catch the exception in your change password method, you can set breakpoint to debug your code.

    By the way, you may also have a try to customize the changepassword control with adding the validation.

    asp changepassword control customization

    May

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, November 21, 2014 1:13 AM