File: D:/HostingSpaces/MBoogaard/oosting-horseriding.com/wwwroot/CMSPages/logon.aspx.cs
using System;
using System.Data;
using System.Web;
using System.Text;
using System.Web.UI;
using System.Web.Security;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using CMS.ExtendedControls;
using CMS.Helpers;
using CMS.Localization;
using CMS.Base;
using CMS.SiteProvider;
using CMS.Membership;
using CMS.UIControls;
using CMS.DataEngine;
using CMS.PortalEngine;
using CMS.MembershipProvider;
public partial class CMSPages_logon : CMSPage, ICallbackEventHandler
{
#region "Variables"
private LocalizedLabel mFailureLabel = null;
private bool? mShowForgottenPassword = null;
#endregion
#region "Properties"
/// <summary>
/// Gets or sets the main text resource string
/// </summary>
protected string MainTextResString
{
get;
set;
}
/// <summary>
/// Failure text label.
/// </summary>
public LocalizedLabel FailureLabel
{
get
{
return mFailureLabel ?? (mFailureLabel = (LocalizedLabel)Login1.FindControl("FailureText"));
}
}
/// <summary>
/// Returns whether is page in forgotten password "mode"
/// </summary>
private bool IsForgottenPassword
{
get
{
return ValidationHelper.GetBoolean(ViewState["ForgottenPassword"], false);
}
set
{
ViewState["ForgottenPassword"] = value;
}
}
/// <summary>
/// Gets or sets whether link to forgotten password is shown on logon page.
/// </summary>
public bool ShowForgottenPassword
{
get
{
if (mShowForgottenPassword == null)
{
mShowForgottenPassword = ValidationHelper.GetBoolean(SettingsHelper.AppSettings["CMSShowForgottenPassLink"], true);
}
return mShowForgottenPassword.Value;
}
set
{
mShowForgottenPassword = value;
}
}
/// <summary>
/// Gets return URL for logon page
/// </summary>
public string ReturnUrl
{
get
{
return QueryHelper.GetString("returnurl", string.Empty);
}
}
#endregion
protected void Page_Load(object sender, EventArgs e)
{
SetBrowserClass();
AddNoCacheTag();
HideErrorWarnings();
MainTextResString = "LogonForm.LogOn";
// Ensure the refresh script
const string defaultCondition = "((top.frames['cmsdesktop'] != null) || (top.frames['propheader'] != null))";
ScriptHelper.RegisterClientScriptBlock(this, typeof(string), "TopWindow", ScriptHelper.GetScript(" if " + defaultCondition + " { try {top.window.location.reload();} catch(err){} }"));
// Enable caps lock check
if (ValidationHelper.GetBoolean(SettingsHelper.AppSettings["CMSUseCapsLockNotification"], true))
{
RegisterCAPSLOCKScript();
TextBox txtPassword = (TextBox)Login1.FindControl("Password");
if (txtPassword != null)
{
txtPassword.Attributes.Add("onkeypress", "CheckCapsLock(event)");
}
}
LocalizedLabel lblItem = (LocalizedLabel)Login1.FindControl("lblUserName");
if (lblItem != null)
{
lblItem.Text = "{$LogonForm.UserName$}";
}
lblItem = (LocalizedLabel)Login1.FindControl("lblPassword");
if (lblItem != null)
{
lblItem.Text = "{$LogonForm.Password$}";
}
// Display culture link due to value of the key stored in the web.config file
bool showCultureSelector = ValidationHelper.GetBoolean(SettingsHelper.AppSettings["CMSShowLogonCultureSelector"], true);
if (showCultureSelector)
{
LocalizedLinkButton lnkLanguage = (LocalizedLinkButton)Login1.FindControl("lnkLanguage");
if (lnkLanguage != null)
{
lnkLanguage.Visible = true;
// Ensure language selection panel functionality
HtmlGenericControl pnlLanguage = (HtmlGenericControl)Login1.FindControl("pnlLanguage");
if (pnlLanguage != null)
{
ltlScript.Text = ScriptHelper.GetScript("function ShowLanguage(id){var panel=document.getElementById(id);if(panel!=null){panel.style.display=(panel.style.display == 'block')?'none':'block';}}");
lnkLanguage.Attributes.Add("onclick", "ShowLanguage('" + pnlLanguage.ClientID + "'); return false;");
}
}
}
// Set up forgotten password link
if (ShowForgottenPassword)
{
LocalizedLinkButton lnkPassword = (LocalizedLinkButton)Login1.FindControl("lnkPassword");
if (lnkPassword != null)
{
lnkPassword.Visible = true;
lnkPassword.Click += lnkPassword_Click;
}
}
PlaceHolder plcRemeberMe = (PlaceHolder)Login1.FindControl("plcRemeberMe");
if ((MFAuthenticationHelper.IsMultiFactorAutEnabled) && (plcRemeberMe != null))
{
plcRemeberMe.Visible = false;
}
LocalizedButton btnItem = (LocalizedButton)Login1.FindControl("LoginButton");
if (btnItem != null)
{
btnItem.Text = "{$LogonForm.LogOnButton$}";
btnItem.Click += btnItem_Click;
}
// Load UI cultures for the dropdown list
if (!RequestHelper.IsPostBack())
{
LoadCultures();
}
Login1.LoggingIn += Login1_LoggingIn;
Login1.LoggedIn += Login1_LoggedIn;
Login1.LoginError += Login1_LoginError;
Login1.Authenticate += Login1_Authenticate;
if (!RequestHelper.IsPostBack())
{
Login1.UserName = QueryHelper.GetString("username", String.Empty);
}
// Ensure username textbox focus
CMSTextBox txtUserName = (CMSTextBox)Login1.FindControl("UserName");
if (txtUserName != null)
{
ScriptHelper.RegisterStartupScript(this, GetType(), "SetFocus", ScriptHelper.GetScript("var txt=document.getElementById('" + txtUserName.ClientID + "');if(txt!=null){txt.focus();}"));
txtUserName.EnableAutoComplete = SecurityHelper.IsAutoCompleteEnabledForLogin(SiteContext.CurrentSiteName);
}
if (QueryHelper.GetBoolean("forgottenpassword", false))
{
SetForgottenPasswordMode();
}
// Register script to update logon error message
StringBuilder sbScript = new StringBuilder();
sbScript.Append(@"
var failedText_", ClientID, "= document.getElementById('", FailureLabel.ClientID, @"');
function UpdateLabel_", ClientID, @"(content, context) {
var lbl = document.getElementById(context);
if(lbl)
{
lbl.innerHTML = content;
lbl.className = """";
}
}");
ScriptHelper.RegisterClientScriptBlock(this, GetType(), "InvalidLogonAttempts_" + ClientID, sbScript.ToString(), true);
}
/// <summary>
/// PreRender event handler
/// </summary>
protected override void OnPreRender(EventArgs e)
{
// Keep latest application after logon
UIContextHelper.RegisterApplicationStorageScript(Page);
base.OnPreRender(e);
}
private void btnItem_Click(object sender, EventArgs e)
{
// Check if should send password
if (IsForgottenPassword)
{
SetForgottenPasswordMode();
TextBox txtUserName = (TextBox)Login1.FindControl("UserName");
if ((txtUserName != null) && !string.IsNullOrEmpty(txtUserName.Text.Trim()))
{
// Reset password
string siteName = SiteContext.CurrentSiteName;
bool success;
string result;
// Prepare URL to which may user return after password reset
string returnUrl = RequestContext.CurrentURL;
if (!string.IsNullOrEmpty(Login1.UserName))
{
returnUrl = URLHelper.AddParameterToUrl(returnUrl, "username", Login1.UserName);
}
result = AuthenticationHelper.ForgottenEmailRequest(txtUserName.Text.Trim(), siteName, "Logon page", SettingsKeyInfoProvider.GetValue(siteName + ".CMSSendPasswordEmailsFrom"), null, AuthenticationHelper.GetResetPasswordUrl(siteName), out success, returnUrl);
if (!success)
{
DisplayError(result);
}
else
{
DisplayWarning(result);
}
}
else
{
DisplayError(GetString("logonform.nouser"));
}
}
}
private void lnkPassword_Click(object sender, EventArgs e)
{
if (!IsForgottenPassword)
{
SetForgottenPasswordMode();
}
else
{
string url = URLHelper.RemoveParameterFromUrl(RequestContext.CurrentURL, "forgottenpassword");
url = URLHelper.AddParameterToUrl(url, "username", Login1.UserName);
URLHelper.Redirect(url);
}
}
private void Login1_LoginError(object sender, EventArgs e)
{
bool showError = true;
if (FailureLabel != null)
{
// Ban IP addresses which are blocked for login
if (MembershipContext.UserIsBanned)
{
DisplayError(GetString("banip.ipisbannedlogin"));
}
else if (AuthenticationHelper.DisplayAccountLockInformation(SiteContext.CurrentSiteName) && MembershipContext.UserAccountLockedDueToInvalidLogonAttempts)
{
DisplayAccountLockedError(GetString("invalidlogonattempts.unlockaccount.accountlocked"));
}
else if (AuthenticationHelper.DisplayAccountLockInformation(SiteContext.CurrentSiteName) && MembershipContext.UserAccountLockedDueToPasswordExpiration)
{
DisplayAccountLockedError(GetString("passwordexpiration.accountlocked"));
}
else if (MembershipContext.UserIsPartiallyAuthenticated && !MembershipContext.UserAuthenticationFailedDueToInvalidPasscode)
{
if (MembershipContext.MFAuthenticationTokenNotInitialized && MFAuthenticationHelper.DisplayTokenID)
{
var lblTokenID = Login1.FindControl("lblTokenID") as LocalizedLabel;
var plcTokenInfo = Login1.FindControl("plcTokenInfo");
if ((lblTokenID != null) && (plcTokenInfo != null))
{
DisplayWarning(string.Format("<strong>{0}</strong> {1}", GetString("mfauthentication.isRequired"), GetString("mfauthentication.token.get")));
lblTokenID.Text = MFAuthenticationHelper.GetTokenIDForUser(Login1.UserName);
plcTokenInfo.Visible = true;
}
}
showError = false;
}
else if (!MembershipContext.UserIsPartiallyAuthenticated)
{
// Show login and password screen
var plcPasscodeBox = Login1.FindControl("plcPasscodeBox");
var plcLoginInputs = Login1.FindControl("plcLoginInputs");
var plcTokenInfo = Login1.FindControl("plcTokenInfo");
if (plcLoginInputs != null)
{
plcLoginInputs.Visible = true;
}
if (plcPasscodeBox != null)
{
plcPasscodeBox.Visible = false;
}
if (plcTokenInfo != null)
{
plcTokenInfo.Visible = false;
}
}
if (showError && string.IsNullOrEmpty(FailureLabel.Text))
{
DisplayError(GetString("Login_FailureText"));
}
}
}
private void Login1_LoggedIn(object sender, EventArgs e)
{
// ScreenLock - unlock screen
IsScreenLocked = false;
// Ensure response cookie
CookieHelper.EnsureResponseCookie(FormsAuthentication.FormsCookieName);
// Set cookie expiration
if (Login1.RememberMeSet)
{
CookieHelper.ChangeCookieExpiration(FormsAuthentication.FormsCookieName, DateTime.Now.AddYears(1), false);
}
else
{
// Extend the expiration of the authentication cookie if required
if (!AuthenticationHelper.UseSessionCookies && (HttpContext.Current != null) && (HttpContext.Current.Session != null))
{
CookieHelper.ChangeCookieExpiration(FormsAuthentication.FormsCookieName, DateTime.Now.AddMinutes(Session.Timeout), false);
}
}
// Current username
string userName = Login1.UserName;
// Get info on the authenticated user
UserInfo ui = UserInfoProvider.GetUserInfoForSitePrefix(userName, SiteContext.CurrentSite);
String siteName = SiteContext.CurrentSiteName;
// For site prefix user, authenticate manually
if (ui != null)
{
if (UserInfoProvider.UserNameSitePrefixEnabled(siteName) && UserInfoProvider.IsSitePrefixedUser(ui.UserName))
{
AuthenticationHelper.AuthenticateUser(ui.UserName, Login1.RememberMeSet);
}
}
// Check whether safe user name is required and if so get safe username
else if (RequestHelper.IsMixedAuthentication() && UserInfoProvider.UseSafeUserName)
{
userName = ValidationHelper.GetSafeUserName(userName, SiteContext.CurrentSiteName);
if (UserInfoProvider.UserNameSitePrefixEnabled(siteName))
{
// Check for site prefix
ui = UserInfoProvider.GetUserInfoForSitePrefix(userName, SiteContext.CurrentSite);
if (ui != null)
{
userName = ui.UserName;
}
}
AuthenticationHelper.AuthenticateUser(userName, Login1.RememberMeSet);
}
// Set culture
CMSDropDownList drpCulture = (CMSDropDownList)Login1.FindControl("drpCulture");
if (drpCulture != null)
{
string selectedCulture = drpCulture.SelectedValue;
// Not the default culture
if (selectedCulture != "")
{
// Update the user
if (ui != null)
{
ui.PreferredUICultureCode = selectedCulture;
UserInfoProvider.SetUserInfo(ui);
}
// Update current user
MembershipContext.AuthenticatedUser.PreferredUICultureCode = selectedCulture;
}
}
string returnUrl = ReturnUrl;
// Return url is not specified or is relative path or hash is valid
if (!string.IsNullOrEmpty(returnUrl) && !returnUrl.StartsWithCSafe("~") && !returnUrl.StartsWithCSafe("/") && !QueryHelper.ValidateHash("hash"))
{
URLHelper.Redirect(UIHelper.GetErrorPageUrl("dialogs.badhashtitle", "dialogs.badhashtext"));
}
}
private void Login1_LoggingIn(object sender, LoginCancelEventArgs e)
{
// Ensure all cookies
if (CookieHelper.CurrentCookieLevel <= CookieLevel.All)
{
CookieHelper.ChangeCookieLevel(CookieLevel.All);
}
Login1.RememberMeSet = ((CMSCheckBox)Login1.FindControl("chkRememberMe")).Checked;
}
/// <summary>
/// Handling login authenticate event.
/// </summary>
/// <param name="sender">Sender.</param>
/// <param name="e">Authenticate event arguments.</param>
private void Login1_Authenticate(object sender, AuthenticateEventArgs e)
{
if (MFAuthenticationHelper.IsMultiFactorRequiredForUser(Login1.UserName))
{
var plcPasscodeBox = Login1.FindControl("plcPasscodeBox");
var plcLoginInputs = Login1.FindControl("plcLoginInputs");
var txtPasscode = Login1.FindControl("txtPasscode") as CMSTextBox;
if (txtPasscode == null)
{
return;
}
if (plcPasscodeBox == null)
{
return;
}
if (plcLoginInputs == null)
{
return;
}
// Handle passcode
string passcode = txtPasscode.Text;
txtPasscode.Text = string.Empty;
var provider = new CMSMembershipProvider();
// Validate username and password
if (plcLoginInputs.Visible)
{
if (provider.MFValidateCredentials(Login1.UserName, Login1.Password))
{
// Show passcode screen
plcLoginInputs.Visible = false;
plcPasscodeBox.Visible = true;
}
}
// Validate passcode
else
{
if (provider.MFValidatePasscode(Login1.UserName, passcode))
{
e.Authenticated = true;
}
}
}
else
{
e.Authenticated = Membership.Provider.ValidateUser(Login1.UserName, Login1.Password);
}
}
/// <summary>
/// Load UI cultures for the dropdown list.
/// </summary>
private void LoadCultures()
{
CMSDropDownList drpCulture = (CMSDropDownList)Login1.FindControl("drpCulture");
if (drpCulture != null)
{
DataSet ds = CultureInfoProvider.GetUICultures();
DataView dvCultures = ds.Tables[0].DefaultView;
dvCultures.Sort = "CultureName ASC";
drpCulture.DataValueField = "CultureCode";
drpCulture.DataTextField = "CultureName";
drpCulture.DataSource = dvCultures;
drpCulture.DataBind();
// Add default value
drpCulture.Items.Insert(0, new ListItem(GetString("LogonForm.DefaultCulture"), ""));
LocalizedLabel lblCulture = (LocalizedLabel)Login1.FindControl("lblCulture");
if (lblCulture != null)
{
lblCulture.AssociatedControlID = drpCulture.ID;
lblCulture.Text = ResHelper.GetString("general.select");
lblCulture.Display = false;
}
}
}
/// <summary>
/// Registers the script to handle the CAPSLOCK check.
/// </summary>
private void RegisterCAPSLOCKScript()
{
string script =
"function OnCapslockOn() {\n " +
"document.getElementById('JavaScript-Errors').innerHTML = " +
"'<div class=\"alert alert-warning\"><span class=\"alert-icon\"><i class=\"icon-exclamation-triangle\"></i></span><div class=\"alert-label\">" + GetString(GetString("General.Capslock")) + "</div>'" +
"; \n" +
"} \n" +
"function OnCapslockOff() {\n " +
"var elem = document.getElementById('JavaScript-Errors');\n" +
"if(elem.innerHTML != ''){elem.innerHTML = '';} \n" +
"} \n";
ScriptHelper.RegisterClientScriptBlock(Page, typeof(string), "CapsLockHandling", ScriptHelper.GetScript(script) + ScriptHelper.CapslockScript);
}
/// <summary>
/// Sets forgotten password mode.
/// </summary>
private void SetForgottenPasswordMode()
{
var plcPasscodeBox = Login1.FindControl("plcPasscodeBox");
if (plcPasscodeBox != null)
{
plcPasscodeBox.Visible = false;
}
var plcLoginInputs = Login1.FindControl("plcLoginInputs");
if (plcLoginInputs != null)
{
plcLoginInputs.Visible = true;
}
var plcTokenInfo = Login1.FindControl("plcTokenInfo");
if (plcTokenInfo != null)
{
plcTokenInfo.Visible = false;
}
LocalizedButton btnItem = (LocalizedButton)Login1.FindControl("LoginButton");
if (btnItem != null)
{
btnItem.ResourceString = "LogonForm.SendRequest";
btnItem.CommandName = string.Empty;
}
TextBox txtPassword = (TextBox)Login1.FindControl("Password");
if (txtPassword != null)
{
txtPassword.Visible = false;
}
LocalizedLabel lblItem = (LocalizedLabel)Login1.FindControl("lblPassword");
if (lblItem != null)
{
lblItem.Visible = false;
}
CMSCheckBox chkRemeber = (CMSCheckBox)Login1.FindControl("chkRememberMe");
if (chkRemeber != null)
{
chkRemeber.Visible = false;
}
MainTextResString = "logonform.lnkpasswordretrieval";
LocalizedLabel lblUserName = (LocalizedLabel)Login1.FindControl("lblUserName");
if (lblUserName != null)
{
lblUserName.ResourceString = "logonform.lblpasswordretrieval";
}
RequiredFieldValidator rfvUserName = (RequiredFieldValidator)Login1.FindControl("rfvUserNameRequired");
if (rfvUserName != null)
{
rfvUserName.ToolTip = GetString("LogonForm.NameOrEmailRequired");
rfvUserName.Text = rfvUserName.ErrorMessage = GetString("logonform.rqvalue");
}
var lnkPassword = Login1.FindControl("lnkPassword") as LocalizedLinkButton;
if (lnkPassword != null)
{
lnkPassword.ResourceString = "LogonForm.BackToLogon";
}
IsForgottenPassword = true;
}
/// <summary>
/// Displays error.
/// </summary>
/// <param name="msg">Message.</param>
private void DisplayError(string msg)
{
var plcError = Login1.FindControl("plcError");
if (plcError != null)
{
FailureLabel.Text = msg;
plcError.Visible = !string.IsNullOrEmpty(msg);
}
}
/// <summary>
/// Hides error and warning messages.
/// </summary>
private void HideErrorWarnings()
{
var plcWarning = Login1.FindControl("plcWarning");
var plcError = Login1.FindControl("plcError");
if (plcWarning != null)
{
plcWarning.Visible = false;
}
if (plcError != null)
{
plcError.Visible = false;
}
}
/// <summary>
/// Displays error.
/// </summary>
/// <param name="msg">Message.</param>
private void DisplayWarning(string msg)
{
var plcWarning = Login1.FindControl("plcWarning");
var txtWarning = (LocalizedLabel)Login1.FindControl("txtWarning");
if (plcWarning != null)
{
plcWarning.Visible = true;
txtWarning.Text = msg;
}
}
/// <summary>
/// Displays locked account error message.
/// </summary>
/// <param name="specificMessage">Specific part of the message.</param>
private void DisplayAccountLockedError(string specificMessage)
{
string link = "<a href=\"#\" onclick=\"" + Page.ClientScript.GetCallbackEventReference(this, "null", "UpdateLabel_" + ClientID, "'" + FailureLabel.ClientID + "'") + ";\">" + GetString("general.clickhere") + "</a>";
DisplayError(string.Format(specificMessage + " " + GetString("invalidlogonattempts.unlockaccount.accountlockedlink"), link));
}
#region "ICallbackEventHandler Members"
public string GetCallbackResult()
{
string result = "";
UserInfo ui = UserInfoProvider.GetUserInfo(Login1.UserName);
if (ui != null)
{
string siteName = SiteContext.CurrentSiteName;
// Prepare return URL
string returnUrl = RequestContext.CurrentURL;
if (!string.IsNullOrEmpty(Login1.UserName))
{
returnUrl = URLHelper.AddParameterToUrl(returnUrl, "username", Login1.UserName);
}
switch (UserAccountLockCode.ToEnum(ui.UserAccountLockReason))
{
case UserAccountLockEnum.MaximumInvalidLogonAttemptsReached:
result = AuthenticationHelper.SendUnlockAccountRequest(ui, siteName, "USERLOGON", SettingsKeyInfoProvider.GetValue(siteName + ".CMSSendPasswordEmailsFrom"), null, returnUrl);
break;
case UserAccountLockEnum.PasswordExpired:
bool outParam = true;
result = AuthenticationHelper.SendPasswordRequest(ui, siteName, "USERLOGON", SettingsKeyInfoProvider.GetValue(siteName + ".CMSSendPasswordEmailsFrom"), "Membership.PasswordExpired", null, AuthenticationHelper.GetResetPasswordUrl(siteName), out outParam, returnUrl);
break;
}
}
return result;
}
public void RaiseCallbackEvent(string eventArgument)
{
}
#endregion
}