Request for change password template with Username

Rob Jeremy asked on March 7, 2014 15:17

Hi everyone

I have been tasked with customizing the change password webpart along with the email that gets sent when a request happens. I have been able to complete this successfully except for one part. I have to add a line in the email template that includes the username of the account.

I have tried doing this but it returns "public" most likely because the user is not logged in when they make this request. Username: {%UserName%}

If anyone has any suggestions for a macro to make it show the username of the user requesting the password, that would be great.

Thanks!

Correct Answer

Luca Dell'Angelo answered on March 21, 2014 04:34

Hi Rob,

yes I'm using {%UserName%} Macro in the Membership - Change password request email template

try this code: 1) var userData = UserInfoProvider.GetUsers("userEmail", "");

2)CMSContext.CurrentResolver.CurrentUser = new CurrentUserInfo(UserInfoProvider.GetUserInfo((string)userData.Tables[0].Rows[0]["UserName"]), true);

3)AuthenticationHelper.ForgottenEmailRequest(recoveryEmail.Value, siteName, "Password Recovery", from, CMSContext.CurrentResolver, resetUrl, out success, returnUrl);

1 votesVote for this answer Unmark Correct answer

Recent Answers


Brenden Kehren answered on March 7, 2014 15:48

If you're talking "reset" password, take a look at the /CMSModules/Membership/Controls/ResetPassword.ascs.cs control. The URL sent to the user to reset their password has a few parameters in it. Looks like on line 133 or so, it shows you how to get the user info from the HASH in the URL. That might get you started anyway.

I think to modify that email templates macros you might need the full source code because the email is sent in the AuthenticationHelper.ValidateResetPassword(ui, hash, time, interval, "Reset password control") method.

0 votesVote for this answer Mark as a Correct answer

Rob Jeremy answered on March 7, 2014 15:58

Hi, thanks for your quick response.

I am not talking about the reset password control. I am using the Password Retriveal within the LogonForm web part. I see that on btnPasswdRetrieval_Click it does this: AuthenticationHelper.ForgottenEmailRequest(value, CMSContext.CurrentSiteName, "LOGONFORM", SendEmailFrom, CMSContext.CurrentResolver, ResetPasswordURL, out success, returnUrl);

I'd imagine that this is where it sends the email (that I want to modify) to the user with a link to then reset their password.

So when you say this:

I think to modify that email templates macros you might need the full source code because the email is sent in the AuthenticationHelper.ValidateResetPassword(ui, hash, time, interval, "Reset password control") method. Does this mean that I will not be able to do it because I don't have access to the full source code?

0 votesVote for this answer Mark as a Correct answer

Brenden Kehren answered on March 7, 2014 16:21

I wouldn't say its not totally possible without the full source code version but it would surely be easier. You could clone the webpart and where that AuthenticationHelper.ValidateResetPassword() method is, you could do your own work to reset the password, process the macros and send the email out. There are ways around everything but it usually involves additional time and effort which = $$$.

0 votesVote for this answer Mark as a Correct answer

Juraj Ondrus answered on March 10, 2014 06:56

Hi,

The macro you are using uses the current context - which is public user. In this case, you will have to create a custom macro or add custom code, that will get the user information and set the context to this user or, resolve the macro in the code with this user.

Best regards,
Juraj Ondrus

0 votesVote for this answer Mark as a Correct answer

Jordan Spence answered on March 10, 2014 14:34

Hi,

I've run into the same issue trying to get the UserName in an email template. I'm making changes to the "Membership - Change password request" email template. Can you get to it from a custom macro? Or do you have to replicate the /CMSModules/Membership/Controls/ResetPassword.ascs.cs code?

0 votesVote for this answer Mark as a Correct answer

Rob Jeremy answered on March 11, 2014 12:33

Thank you everyone for your responses.

This kind of functionality seems pretty standard in other CMS packages that I have worked with.. and I am somewhat surprised it's not a part of Kentico. What led my project manager to wanting this functionality is simply this.

If a user has forgot their username and password, but knows their email that they signed up with, they can retrieve their password. By default (atleast in my Kentico 7), Kentico is only setup to allow user authentication via username. (I am using the Logon form web part). With that being said, a user would never be able to log in to the website without knowing their username.

I am curious to know how other Kentico CMS web sites have solved this problem.

If I were to go the route of creating a custom macro in the email template, wouldn't it require a parameter of the email address of the person who forgot their password? I could create a custom macro, but I don't know how I would be able to hook into the users information without some type of parameter. I do not have access to the full source code so that option is out the door for me.

0 votesVote for this answer Mark as a Correct answer

Luca Dell'Angelo answered on March 13, 2014 09:44

Hi,

I have the same problem, I'm trying to solve it creating a custom macros but how can get userName by email? UserInfoProvider has no useful methods to get it. I don't think Getting all site users "UserInfoProvider.GetUsers" and then filter by email is a good approach. Any suggestions?

thanks

0 votesVote for this answer Mark as a Correct answer

Luca Dell'Angelo answered on March 20, 2014 05:10

Hi, my solution:
1) var userData = UserInfoProvider.GetUsers("userEmail", ""); 2) CMSContext.CurrentUser = new CurrentUserInfo(UserInfoProvider.GetUserInfo((string)userData.Tables[0].Rows[0]["UserName"]), false); 3) AuthenticationHelper.ForgottenEmailRequest(recoveryEmail.Value, siteName, "Password Recovery", from, CMSContext.CurrentResolver, resetUrl, out success, returnUrl);

Setting the user context it works!

Any better solutions?

1 votesVote for this answer Mark as a Correct answer

Rob Jeremy answered on March 20, 2014 09:45

Hi LUCA DELL'ANGELO, Thanks for your response.

Are you using this to populate the {%UserName%} Macro in the Membership - Change password request email template?

It is still returning as "public" for me.

0 votesVote for this answer Mark as a Correct answer

Rob Jeremy answered on March 21, 2014 09:27

Hi Luca,

Thank you so much for your help. The second solution you provided worked so I marked it as the answer. Adding in the CMSContext.CurrentResolver.CurrentUser made it work for me.

I was using the forgot password on the LogonForm.ascx.cs Web Part, so here is my working function with the {%UserName%} macro in the Request for forgot password template. The forgot password prompts for either email or username, so I modified Luca's code slightly to accept username or email. I am using Kentico 7.

Thanks again.

/// <summary>
/// Retrieve the user password.
/// </summary>
private void btnPasswdRetrieval_Click(object sender, EventArgs e)
{
    string value = txtPasswordRetrieval.Text.Trim();

    if (value != String.Empty)
    {
        // Prepare return URL
        string returnUrl = URLHelper.CurrentURL;
        if (!string.IsNullOrEmpty(Login1.UserName))
        {
            returnUrl = URLHelper.AddParameterToUrl(returnUrl, "username", value);
        }

        bool success;
        //lblResult.Text = AuthenticationHelper.ForgottenEmailRequest(value, CMSContext.CurrentSiteName, "LOGONFORM", SendEmailFrom, CMSContext.CurrentResolver, ResetPasswordURL, out success, returnUrl);

        ///help from Kentico...
        //http://devnet.kentico.com/questions/request-for-change-password-template-with-username

        var userEmailData = UserInfoProvider.GetUsers("Email = '" + value + "'", "");
        var userUsernameData = UserInfoProvider.GetUsers("Username = '" + value + "'", "");

        if (userEmailData != null && userEmailData.Tables[0].Rows.Count > 0)
        {
            CMSContext.CurrentResolver.CurrentUser = new CurrentUserInfo(UserInfoProvider.GetUserInfo((string)userEmailData.Tables[0].Rows[0]["UserName"]), false);
            lblResult.Text += AuthenticationHelper.ForgottenEmailRequest(value, CMSContext.CurrentSiteName, "Password Recovery", SendEmailFrom, CMSContext.CurrentResolver, ResetPasswordURL, out success, returnUrl);
        }
        else if (userUsernameData != null && userUsernameData.Tables[0].Rows.Count > 0)
        {
            CMSContext.CurrentResolver.CurrentUser = new CurrentUserInfo(UserInfoProvider.GetUserInfo((string)userUsernameData.Tables[0].Rows[0]["UserName"]), false);
            lblResult.Text += AuthenticationHelper.ForgottenEmailRequest(value, CMSContext.CurrentSiteName, "Password Recovery", SendEmailFrom, CMSContext.CurrentResolver, ResetPasswordURL, out success, returnUrl);
        }
        else
        {
            lblResult.Text = "Could not find username or email.";
        }

        lblResult.Visible = true;

        pnlPasswdRetrieval.Attributes.Add("style", "display:block;");
    }
}
0 votesVote for this answer Mark as a Correct answer

   Please, sign in to be able to submit a new answer.