using System;
using System.Data;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using CMS.CMSHelper;
using CMS.GlobalHelper;
using CMS.MembershipProvider;
using CMS.PortalControls;
using CMS.PortalEngine;
using CMS.SettingsProvider;
using CMS.SiteProvider;
using CMS.URLRewritingEngine;
public partial class CMSWebParts_Membership_Logon_SignOutButton : CMSAbstractWebPart
{
#region "Constants"
protected const string SESSION_NAME_USERDATA = "facebookid";
#endregion
#region "Variables"
private string mSignOutText = ResHelper.LocalizeString("{$Webparts_Membership_SignOutButton.SignOut$}");
private string mSignInText = ResHelper.LocalizeString("{$Webparts_Membership_SignOutButton.SignIn$}");
private bool mShowOnlyWhenAuthenticated = true;
private bool mShowAsLink = false;
#endregion
#region "Properties"
///
/// Gets or sets the sign out text.
///
public string SignOutText
{
get
{
return DataHelper.GetNotEmpty(GetValue("SignOutText"), mSignOutText);
}
set
{
SetValue("SignOutText", value);
mSignOutText = value;
}
}
///
/// Gets or sets the sign in text.
///
public string SignInText
{
get
{
return DataHelper.GetNotEmpty(GetValue("SignInText"), mSignInText);
}
set
{
SetValue("SignInText", value);
mSignInText = value;
}
}
///
/// Gets or sets the value that indicates whether the webpart is shown as button or as text link.
///
public bool ShowAsLink
{
get
{
return ValidationHelper.GetBoolean(GetValue("ShowAsLink"), mShowAsLink);
}
set
{
SetValue("ShowAsLink", value);
mShowAsLink = value;
}
}
///
/// Gets or sets the URL where user is redirected after sign out.
///
public string RedirectToUrl
{
get
{
return DataHelper.GetNotEmpty(GetValue("RedirectToURL"), URLRewriter.CurrentURL);
}
set
{
SetValue("RedirectToURL", value);
}
}
///
/// Gets or sets the path where user is redirected before sign in.
///
public string SignInUrl
{
get
{
return ValidationHelper.GetString(GetValue("SignInPageUrl"), "");
}
set
{
SetValue("SignInPageUrl", value);
}
}
///
/// Gets or sets the path where user is redirected after sign in.
///
public string ReturnPath
{
get
{
return ValidationHelper.GetString(GetValue("ReturnPath"), "");
}
set
{
SetValue("ReturnPath", value);
}
}
///
/// Gets or sets the value that indicates whether webpart is shown only when the user is authenticated.
///
public bool ShowOnlyWhenAuthenticated
{
get
{
return ValidationHelper.GetBoolean(GetValue("ShowOnlyWhenAuthenticated"), mShowOnlyWhenAuthenticated);
}
set
{
SetValue("ShowOnlyWhenAuthenticated", value);
mShowOnlyWhenAuthenticated = value;
}
}
///
/// Gets or sets the SkinID of the logon form.
///
public override string SkinID
{
get
{
return base.SkinID;
}
set
{
base.SkinID = value;
btnSignOut.SkinID = value;
}
}
#endregion
#region "Methods"
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
Visible = true;
}
protected override void OnPreRender(EventArgs e)
{
base.OnPreRender(e);
SetupControl();
}
///
/// Initializes the control properties.
///
protected void SetupControl()
{
if (StopProcessing)
{
// Do not process
}
else
{
// Facebook Connect sign out
if (CMSContext.CurrentUser.IsAuthenticated())
{
if (QueryHelper.GetInteger("logout", 0) > 0)
{
// Sign out from CMS
CMSContext.LogoutUser();
// Remove Facebook user object from session
SessionHelper.Remove(SESSION_NAME_USERDATA);
CMSContext.CurrentUser = null;
Response.Cache.SetNoStore();
URLHelper.Redirect(URLHelper.RemoveParameterFromUrl(URLHelper.CurrentURL, "logout"));
return;
}
}
// Show only desired button
btnSignOut.Visible = !ShowAsLink;
btnSignOutLink.Visible = ShowAsLink;
if (!CMSContext.CurrentUser.IsPublic())
{
// Hide for windows authentication
if (RequestHelper.IsWindowsAuthentication())
{
Visible = false;
}
else
{
// Set signout text
btnSignOutLink.Text = SignOutText;
btnSignOut.Text = SignOutText;
}
}
else
{
// Set signin text
btnSignOutLink.Text = SignInText;
btnSignOut.Text = SignInText;
}
}
if (!StandAlone && (PageCycle < PageCycleEnum.Initialized) && (ValidationHelper.GetString(Page.StyleSheetTheme, "") == ""))
{
btnSignOut.SkinID = SkinID;
}
// if user is not authenticated and ShowOnlyWhenAuthenticated is set
if (CMSContext.CurrentUser.IsPublic() && ShowOnlyWhenAuthenticated)
{
Visible = false;
}
}
///
/// SignOut handler.
///
protected void btnSignOut_Click(object sender, EventArgs e)
{
if (StopProcessing)
{
// Do not process
}
else
{
if (CMSContext.CurrentUser.IsAuthenticated())
{
CMSContext.LogoutUser();
string redirectUrl = RedirectToUrl;
// If the user has registered Windows Live ID
if (!String.IsNullOrEmpty(CMSContext.CurrentUser.UserSettings.WindowsLiveID))
{
// Get data from auth cookie
string[] userData = AuthenticationHelper.GetUserDataFromAuthCookie();
// If user has logged in using Windows Live ID, then sign him out from Live too
if ((userData != null) && (Array.IndexOf(userData, "liveidlogin") >= 0))
{
string siteName = CMSContext.CurrentSiteName;
// Get LiveID settings
string appId = SettingsKeyProvider.GetStringValue(siteName + ".CMSApplicationID");
string secret = SettingsKeyProvider.GetStringValue(siteName + ".CMSApplicationSecret");
// Check valid Windows LiveID parameters
if ((appId != string.Empty) && (secret != string.Empty))
{
WindowsLiveLogin wll = new WindowsLiveLogin(appId, secret);
// Store info about logout request, for validation logout request
SessionHelper.SetValue("liveidlogout", DateTime.Now);
// Redirect to Windows Live
redirectUrl = wll.GetLogoutUrl();
}
}
}
CMSContext.ViewMode = ViewModeEnum.LiveSite;
CMSContext.CurrentUser = null;
Response.Cache.SetNoStore();
URLHelper.Redirect(redirectUrl);
}
else
{
string returnUrl = null;
string signInUrl = null;
if (SignInUrl != "")
{
signInUrl = ResolveUrl(CMSContext.GetUrl(CMSContext.ResolveCurrentPath(SignInUrl)));
}
else
{
signInUrl = SettingsKeyProvider.GetStringValue(CMSContext.CurrentSiteName + ".CMSSecuredAreasLogonPage");
}
if (ReturnPath != "")
{
returnUrl = ResolveUrl(CMSContext.GetUrl(CMSContext.ResolveCurrentPath(ReturnPath)));
}
else
{
returnUrl = URLHelper.CurrentURL;
}
if (signInUrl != "")
{
// Prevent multiple returnUrl parameter
returnUrl = URLHelper.RemoveParameterFromUrl(returnUrl, "returnUrl");
URLHelper.Redirect(URLHelper.UpdateParameterInUrl(signInUrl, "returnurl", Server.UrlEncode(returnUrl)));
}
}
}
}
///
/// Applies given stylesheet skin.
///
/// Page
public override void ApplyStyleSheetSkin(Page page)
{
btnSignOut.SkinID = SkinID;
base.ApplyStyleSheetSkin(page);
}
///
/// PreInit handler.
///
protected void CMSWebParts_Search_cmssearchboxl_PreInit(object sender, EventArgs e)
{
// Set SkinID property
if (!StandAlone && (PageCycle < PageCycleEnum.Initialized))
{
btnSignOut.SkinID = SkinID;
}
}
#endregion
}