//----------------------------------------------------------------------- // // Copyright (c) Microsoft. All rights reserved. // //----------------------------------------------------------------------- namespace DotNetOpenAuth.AspNet { using System; using System.Text.RegularExpressions; using System.Web; using DotNetOpenAuth.Messaging; /// /// The uri helper. /// internal static class UriHelper { #region Public Methods and Operators /// /// The attach query string parameter. /// /// /// The url. /// /// /// The parameter name. /// /// /// The parameter value. /// /// An absolute URI. public static Uri AttachQueryStringParameter(this Uri url, string parameterName, string parameterValue) { UriBuilder builder = new UriBuilder(url); string query = builder.Query; if (query.Length > 1) { // remove the '?' character in front of the query string query = query.Substring(1); } string parameterPrefix = parameterName + "="; string encodedParameterValue = Uri.EscapeDataString(parameterValue); string newQuery = Regex.Replace(query, parameterPrefix + "[^\\&]*", parameterPrefix + encodedParameterValue); if (newQuery == query) { if (newQuery.Length > 0) { newQuery += "&"; } newQuery = newQuery + parameterPrefix + encodedParameterValue; } builder.Query = newQuery; return builder.Uri; } /// /// Converts an app-relative url, e.g. ~/Content/Return.cshtml, to a full-blown url, e.g. http://mysite.com/Content/Return.cshtml /// /// /// The return URL. /// /// /// The context. /// /// An absolute URI. public static Uri ConvertToAbsoluteUri(string returnUrl, HttpContextBase context) { if (Uri.IsWellFormedUriString(returnUrl, UriKind.Absolute)) { return new Uri(returnUrl, UriKind.Absolute); } if (!VirtualPathUtility.IsAbsolute(returnUrl)) { returnUrl = VirtualPathUtility.ToAbsolute(returnUrl); } Uri publicUrl = context.Request.GetPublicFacingUrl(); return new Uri(publicUrl, returnUrl); } #endregion } }