Beispiel #1
0
    /// <summary>
    /// Initializes the control properties.
    /// </summary>
    protected void SetupControl()
    {
        if (StopProcessing)
        {
            // Do nothing
        }
        else
        {
            if (SettingsKeyProvider.GetBoolValue(CMSContext.CurrentSiteName + ".CMSEnableWindowsLiveID"))
            {
                string siteName = CMSContext.CurrentSiteName;
                if (!string.IsNullOrEmpty(siteName))
                {
                    // Get LiveID settings
                    string appId = SettingsKeyProvider.GetStringValue(siteName + ".CMSApplicationID");
                    string secret = SettingsKeyProvider.GetStringValue(siteName + ".CMSApplicationSecret");

                    if (!WindowsLiveLogin.UseServerSideAuthorization)
                    {
                        // Add windows live ID script
                        ScriptHelper.RegisterClientScriptInclude(Page, typeof(string), "WLScript", "https://js.live.net/v5.0/wl.js");

                        // Add login functions
                        String loginLiveIDClientScript = @"

                            function signUserIn() {
                                var scopesArr = ['wl.signin'];
                                WL.login({ scope: scopesArr });
                            }

                            function refreshLiveID(param)
                            {
                                " + ControlsHelper.GetPostBackEventReference(btnHidden, "#").Replace("'#'", "param") + @"
                            }
                        ";

                        ScriptHelper.RegisterClientScriptBlock(this, typeof(string), "ClientInitLiveIDScript", ScriptHelper.GetScript(loginLiveIDClientScript));
                    }

                    // Check valid Windows LiveID parameters
                    if ((appId == string.Empty) || (secret == string.Empty))
                    {
                        lblError.Visible = true;
                        lblError.Text = GetString("liveid.incorrectsettings");
                        return;
                    }

                    WindowsLiveLogin wll = new WindowsLiveLogin(appId, secret);

                    // If user is already authenticated
                    if (CMSContext.CurrentUser.IsAuthenticated())
                    {
                        // If signout should be visible and user has LiveID registered
                        if ((ShowSignOut) && (!String.IsNullOrEmpty(CMSContext.CurrentUser.UserSettings.WindowsLiveID)))
                        {
                            // Get data from auth cookie
                            string[] userData = UserInfoProvider.GetUserDataFromAuthCookie();

                            // Check if user has truly logged in by LiveID
                            if ((userData != null) && (Array.IndexOf(userData, "liveidlogin") >= 0))
                            {
                                string navUrl = wll.GetLogoutUrl();

                                // If text is set use text/button link
                                if (!string.IsNullOrEmpty(SignOutText))
                                {
                                    // Button link
                                    if (ShowAsButton)
                                    {
                                        btnSignOut.CommandArgument = navUrl;
                                        btnSignOut.Text = SignOutText;
                                        btnSignOut.Visible = true;
                                    }
                                    // Text link
                                    else
                                    {
                                        btnSignOutLink.CommandArgument = navUrl;
                                        btnSignOutLink.Text = SignOutText;
                                        btnSignOutLink.Visible = true;
                                    }
                                }
                                // Image link
                                else
                                {
                                    btnSignOutImage.CommandArgument = navUrl;
                                    btnSignOutImage.ImageUrl = ResolveUrl(SignOutImageURL);
                                    btnSignOutImage.Visible = true;
                                    btnSignOut.Text = GetString("webparts_membership_signoutbutton.signout");
                                }
                            }
                        }
                        else
                        {
                            Visible = false;
                        }
                    }
                    // Sign In
                    else
                    {

                        // Create return URL
                        string returnUrl = QueryHelper.GetText("returnurl", "");
                        returnUrl = (returnUrl == String.Empty) ? URLHelper.CurrentURL : returnUrl;

                        // Create parameters for LiveID request URL
                        String[] parameters = new String[3];
                        parameters[0] = returnUrl;
                        parameters[1] = TrackConversionName;
                        parameters[2] = ConversionValue.ToString();
                        SessionHelper.SetValue("LiveIDInformtion", parameters);

                        returnUrl = wll.GetLoginUrl();

                        // Get App ID
                        appId = SettingsKeyProvider.GetStringValue(siteName + ".CMSApplicationID");

                        // Create full LiveID request URL
                        string navUrl = "https://oauth.live.com/authorize?&client_id=" + appId + "&redirect=true&scope=wl.signin&response_type=code&redirect_uri=" + HttpUtility.UrlEncode(returnUrl);

                        // If text is set use text/button link
                        if (!string.IsNullOrEmpty(SignInText))
                        {
                            // Button link
                            if (ShowAsButton)
                            {
                                AssignButtonControl(navUrl, returnUrl, appId);
                                btnSignIn.Text = SignInText;
                            }
                            // Text link
                            else
                            {
                                AssignHyperlinkControl(navUrl, returnUrl, appId);
                                lnkSignIn.Text = SignInText;
                            }
                        }
                        // Image link
                        else
                        {
                            AssignHyperlinkControl(navUrl, returnUrl, appId);
                            lnkSignIn.ImageUrl = ResolveUrl(SignInImageURL);
                            lnkSignIn.Text = GetString("webparts_membership_signoutbutton.signin");
                        }
                    }
                }
            }
            else
            {
                // Error label is displayed in Design mode when Windows Live ID is disabled
                if (CMSContext.ViewMode == ViewModeEnum.Design)
                {
                    StringBuilder parameter = new StringBuilder();
                    parameter.Append(GetString("header.sitemanager") + " -> ");
                    parameter.Append(GetString("settingscategory.cmssettings") + " -> ");
                    parameter.Append(GetString("settingscategory.cmsmembership") + " -> ");
                    parameter.Append(GetString("settingscategory.cmsmembershipauthentication") + " -> ");
                    parameter.Append(GetString("settingscategory.cmswindowsliveid"));
                    if (CMSContext.CurrentUser.UserSiteManagerAdmin)
                    {
                        // Make it link for SiteManager Admin
                        parameter.Insert(0, "<a href=\"" + URLHelper.GetAbsoluteUrl("~/CMSSiteManager/default.aspx?section=settings") + "\" target=\"_top\">");
                        parameter.Append("</a>");
                    }

                    lblError.Text = String.Format(GetString("mem.liveid.disabled"), parameter.ToString());
                    lblError.Visible = true;
                }
                else
                {
                    Visible = false;
                }
            }
        }
    }
Beispiel #2
0
    /// <summary>
    /// SignOut click event handler.
    /// </summary>
    protected void btnSignOut_Click(object sender, EventArgs e)
    {
        if (currentUser == null)
        {
            currentUser = CMSContext.CurrentUser;
        }
        if (CMSContext.CurrentUser.IsAuthenticated())
        {
            FormsAuthentication.SignOut();
            CMSContext.ClearShoppingCart();

            string redirectUrl = SignOutPath != "" ? GetUrl(SignOutPath) : URLHelper.CurrentURL;

            // If the user is Windows Live user
            if (!string.IsNullOrEmpty(currentUser.UserSettings.WindowsLiveID))
            {
                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);

                    // Redirect to Windows Live
                    redirectUrl = wll.GetLogoutUrl();
                }
            }

            CMSContext.CurrentUser = null;

            Response.Cache.SetNoStore();
            URLHelper.Redirect(redirectUrl);
        }
    }
    /// <summary>
    /// SignOut handler.
    /// </summary>
    protected void btnSignOut_Click(object sender, EventArgs e)
    {
        if (StopProcessing)
        {
            // Do not process
        }
        else
        {
            if (CMSContext.CurrentUser.IsAuthenticated())
            {
                FormsAuthentication.SignOut();
                CMSContext.ClearShoppingCart();

                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 = UserInfoProvider.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 != "")
                {
                    URLHelper.Redirect(URLHelper.AddParameterToUrl(signInUrl, "returnurl", returnUrl));
                }
            }
        }
    }
    /// <summary>
    /// SignOut handler.
    /// </summary>
    protected void btnSignOut_Click(object sender, EventArgs e)
    {
        if (StopProcessing)
        {
            // Do not process
        }
        else
        {
            if (AuthenticationHelper.IsAuthenticated())
            {
                string redirectUrl = RedirectToUrl;

                // If the user has registered Windows Live ID
                if (!String.IsNullOrEmpty(MembershipContext.AuthenticatedUser.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 = SiteContext.CurrentSiteName;

                        // Get LiveID settings
                        string appId = SettingsKeyInfoProvider.GetStringValue(siteName + ".CMSApplicationID");
                        string secret = SettingsKeyInfoProvider.GetStringValue(siteName + ".CMSApplicationSecret");

                        // Check valid Windows LiveID parameters
                        if ((appId != string.Empty) && (secret != string.Empty))
                        {
                            WindowsLiveLogin wll = new WindowsLiveLogin(appId, secret);

                            // Redirect to Windows Live
                            redirectUrl = wll.GetLogoutUrl();
                        }
                    }
                }

                PortalContext.ViewMode = ViewModeEnum.LiveSite;
                AuthenticationHelper.SignOut();

                Response.Cache.SetNoStore();
                URLHelper.Redirect(redirectUrl);
            }
            else
            {
                string returnUrl = null;
                string signInUrl = null;

                if (SignInUrl != "")
                {
                    signInUrl = ResolveUrl(DocumentURLProvider.GetUrl(MacroResolver.ResolveCurrentPath(SignInUrl)));
                }
                else
                {
                    signInUrl = SettingsKeyInfoProvider.GetStringValue(SiteContext.CurrentSiteName + ".CMSSecuredAreasLogonPage");
                }

                if (ReturnPath != "")
                {
                    returnUrl = ResolveUrl(DocumentURLProvider.GetUrl(MacroResolver.ResolveCurrentPath(ReturnPath)));
                }
                else
                {
                    returnUrl = RequestContext.CurrentURL;
                }

                if (signInUrl != "")
                {
                    // Prevent multiple returnUrl parameter
                    returnUrl = URLHelper.RemoveParameterFromUrl(returnUrl, "returnUrl");
                    URLHelper.Redirect(URLHelper.UpdateParameterInUrl(signInUrl, "returnurl", Server.UrlEncode(returnUrl)));
                }
            }
        }
    }
    /// <summary>
    /// SignOut click event handler.
    /// </summary>
    protected void btnSignOut_Click(object sender, EventArgs e)
    {
        if (currentUser == null)
        {
            currentUser = MembershipContext.AuthenticatedUser;
        }
        if (AuthenticationHelper.IsAuthenticated())
        {

            string redirectUrl = SignOutPath != "" ? GetUrl(SignOutPath) : RequestContext.CurrentURL;

            // If the user is Windows Live user
            if (!string.IsNullOrEmpty(currentUser.UserSettings.WindowsLiveID))
            {
                string siteName = SiteContext.CurrentSiteName;

                // Get LiveID settings
                string appId = SettingsKeyInfoProvider.GetValue(siteName + ".CMSApplicationID");
                string secret = SettingsKeyInfoProvider.GetValue(siteName + ".CMSApplicationSecret");

                // Check valid Windows LiveID parameters
                if ((appId != string.Empty) && (secret != string.Empty))
                {
                    WindowsLiveLogin wll = new WindowsLiveLogin(appId, secret);

                    // Redirect to Windows Live and back to "home" page
                    string defaultAliasPath = SettingsKeyInfoProvider.GetValue(siteName + ".CMSDefaultAliasPath");
                    string url = DocumentURLProvider.GetUrl(defaultAliasPath);
                    redirectUrl = wll.GetLogoutUrl(URLHelper.GetAbsoluteUrl(url));
                }
            }

            AuthenticationHelper.SignOut();

            Response.Cache.SetNoStore();
            URLHelper.Redirect(redirectUrl);
        }
    }