/// <summary> /// To the credential string. Format: {domain}\{user}:{password} /// </summary> /// <param name="credential">The credential.</param> /// <returns></returns> public static string ToCredentialString(this HttpCredential credential) { return(credential == null ? null : string.IsNullOrWhiteSpace(credential.Domain) ? ToUserPassword(credential) : string.Format("{0}\\{1}", credential.Domain, ToUserPassword(credential))); }
/// <summary> /// To the URL. Format: {protocol}{user}:{password}@{domain} /// </summary> /// <param name="credential">The credential.</param> /// <param name="protocol">The protocol.</param> /// <returns></returns> public static string ToUrl(this HttpCredential credential, string protocol = null) { credential.Domain.CheckEmptyString(nameof(credential.Domain)); var userPassword = ToUserPassword(credential, EncodingOrSecurityExtension.ToUrlEncodedText); return(credential == null ? null : string.IsNullOrWhiteSpace(userPassword) ? string.Format("{0}{1}", protocol.SafeToString(HttpConstants.HttpProtocols.HttpProtocol), credential.Domain) : string.Format("{0}{1}@{2}", protocol.SafeToString(HttpConstants.HttpProtocols.HttpProtocol), userPassword, credential.Domain)); }
/// <summary> /// To the user password. Format: {user}:{password} /// </summary> /// <param name="credential">The credential.</param> /// <param name="tokenPrepareAction">The token prepare action.</param> /// <returns></returns> private static string ToUserPassword(this HttpCredential credential, Func <string, string> tokenPrepareAction = null) { string result = null; if (credential != null) { result = string.Format(HttpConstants.UserPasswordFormat, credential.Account, tokenPrepareAction == null ? credential.Token : tokenPrepareAction(credential.Token)); if (result[0] == ':') { result = result.Substring(1); } } return(result); }
/// <summary> /// Consists the context. /// </summary> /// <param name="token">The token.</param> /// <param name="setting">The setting.</param> /// <param name="ipAddress">The ip address.</param> /// <param name="userAgent">The user agent.</param> /// <param name="cultureCode">The culture code.</param> /// <param name="currentUri">The current URI.</param> /// <param name="basicAuthentication">The basic authentication.</param> /// <param name="apiUniqueIdentifier">The API unique identifier.</param> internal static void ConsistContext(string token, RestApiSettings setting, string ipAddress, string userAgent, string cultureCode, Uri currentUri, HttpCredential basicAuthentication, ApiUniqueIdentifier apiUniqueIdentifier) { IRestApiEventHandlers restApiEventHandlers = setting?.EventHandlers; var apiContext = ContextHelper.ApiContext; apiContext.UserAgent = userAgent; apiContext.IpAddress = ipAddress; apiContext.CultureCode = cultureCode; apiContext.CurrentUri = currentUri; apiContext.HttpAuthorization = basicAuthentication; apiContext.UniqueIdentifier = apiUniqueIdentifier; if (restApiEventHandlers != null && !string.IsNullOrWhiteSpace(token)) { apiContext.CurrentCredential = restApiEventHandlers.GetCredentialByToken(token); apiContext.Token = token; } else { apiContext.CurrentCredential = null; apiContext.Token = null; } }
/// <summary> /// Consists the context. /// </summary> /// <param name="token">The token.</param> /// <param name="settingName">Name of the setting.</param> /// <param name="ipAddress">The ip address.</param> /// <param name="userAgent">The user agent.</param> /// <param name="cultureCode">The culture code.</param> /// <param name="currentUri">The current URI.</param> /// <param name="basicAuthentication">The basic authentication.</param> /// <param name="apiUniqueIdentifier">The API unique identifier.</param> internal static void ConsistContext(string token, string settingName, string ipAddress, string userAgent, string cultureCode, Uri currentUri, HttpCredential basicAuthentication, ApiUniqueIdentifier apiUniqueIdentifier) { ConsistContext(token, RestApiSettingPool.GetRestApiSettingByName(settingName, true), ipAddress, userAgent, cultureCode, currentUri, basicAuthentication, apiUniqueIdentifier); }