Query Parameters when Item or Layout Not Found - SSO implicit login

On the Sitecore Intranet site that I work on, I use implicit SSO login using the user's windows credentials. It works by hijacking the NoAccessUrl (6.5 uses this) and LayoutNotFoundUrl (7.1 uses this) to point to a page that handles the implicit login and redirects the user to the original requested page. This worked great except for when query parameters were needed on the page. Example: People Profile page uses id as a query parameter to look up the employee's information. In cases like this, the user typically received a blank page and had to click the link a 2nd time in order to reach the requested page (once to login and another after login). The problem was that Sitecore.Pipelines.HttpRequest.ExecuteRequest didn't pass along any of the existing query parameters. It only passes along Sitecore query parameters such as item, user, site, etc.

In order to resolve this problem, I had to override the existing ExecuteRequest with a custom version. In the custom version, I added logic to append the original request parameters. In the SSO Login side, I needed to make sure the original request parameters were appended back to the original request. In order to not display the Sitecore query parameters, I wrote logic to exclude them from the original request.

Custom Configuration:
<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/">
  <sitecore>
    <pipelines>
        <httpRequestBegin>
            <processor patch:instead="*[@type='Sitecore.Pipelines.HttpRequest.ExecuteRequest, Sitecore.Kernel']" type="MPRIntranet.Library.Baseline.Shared.Overrides.ExecuteRequestWithQueryParameters, MPRIntranet" />
        </httpRequestBegin>
    </pipelines>
  </sitecore>
</configuration>

Used Telerik Just Decompile to grab the original code for ExecuteRequest and added the additional piece to add the request parameters. Full class available ExecuteRequestWithQueryParameters.cs

private string AddExistingQueryParameters(string url, HttpRequestArgs args)
        {
            string returnURL = url;
            string queryString = args.Context.Request.QueryString.ToString();
            if (!string.IsNullOrEmpty(queryString))
            {
                string[] queryParameters = queryString.Split(parameterSeparator.ToCharArray());
                               
                foreach (string queryParameter in queryParameters)
                {
                    string[] parameterValue = queryParameter.Split(valueSeparator.ToCharArray());
                    if (parameterValue.Length == 2) //verify that there is a name and value
                    {
                        returnURL = WebUtil.AddQueryString(returnURL, parameterValue);
                    }
                }
            }

            return returnURL;
        }

In order to grab the original parameters excluding the Sitecore added parameters in the SSO login, I wrote a method which grabbed all query parameters that weren't in the ignore list. Note: I use the NoAccessUrl, etc. as a way to know what action triggered the redirect, so I have some parameters that others will not.

private string AppendNonSitecoreQueryParameters(string redirectUrl)
        {
            string[] sitecoreQueryParametersToIgnore = new string[] { "item", "user", "site", "NoAccessUrl", "LayoutNotFoundUrl", "ItemNotFound", "LinkNotFound" };
            string returnURL = redirectUrl;
            if (Request.QueryString.Count > 0)
            {
                for (int i = 0; i < Request.QueryString.Keys.Count; i++)
                {
                    string key = Request.QueryString.Keys[i];
                    string value = Request.QueryString[i];
                    if (!sitecoreQueryParametersToIgnore.Contains(key))
                    {
                        returnURL = WebUtil.AddQueryString(returnURL, new string[] { key, value });
                    }
                }
            }

            return returnURL;
        }

Part two: Handling access rights on Media Library items. Yes, another custom override needed! COMING SOON! 

Comments