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;");
}
}