/// <summary> /// Return a <see cref="ContentResult"/> that automatically POSTs the values. /// </summary> /// <param name="url">Where to post the values.</param> /// <param name="values">The values to post.</param> /// <returns></returns> // ReSharper disable once UnusedMember.Local private ContentResult Post(string url, object values) { var response = HttpContext.Response; response.Clear(); var p = ValuesHelper.ObjectToDictionary(values); var s = new StringBuilder(); s.Append("<html><head><title></title></head>"); s.Append("<body onload='document.forms[\"form\"].submit()'>"); s.Append($"<form name='form' action='{url}' method='post'>"); foreach (var pair in p) { s.Append($"<input type='hidden' name='{pair.Key}' value='{pair.Value}' />"); } s.Append("</form></body></html>"); return(new ContentResult { Content = s.ToString(), ContentType = "text/html", StatusCode = StatusCodes.Status200OK }); }
/// <summary> /// Merges the explicitly provided values with the extra object /// </summary> /// <param name="explicitValues">The explicit values.</param> /// <param name="extra">The extra.</param> /// <returns></returns> public Dictionary <string, string> Merge(Dictionary <string, string> explicitValues, object extra = null) { var merged = explicitValues; if (AuthenticationStyle == AuthenticationStyle.PostValues) { merged.Add(OidcConstants.TokenRequest.ClientId, ClientId); if (ClientSecret.IsPresent()) { merged.Add(OidcConstants.TokenRequest.ClientSecret, ClientSecret); } } var additionalValues = ValuesHelper.ObjectToDictionary(extra); if (additionalValues != null) { merged = explicitValues.Concat(additionalValues.Where(add => !explicitValues.ContainsKey(add.Key))) .ToDictionary(final => final.Key, final => final.Value); } return(merged); }
/// <summary> /// Creates URL based on key/value input pairs. /// </summary> /// <param name="values">The values (either as a Dictionary of string/string or as a type with properties).</param> /// <returns></returns> public string Create(object values) { var dictionary = ValuesHelper.ObjectToDictionary(values); if (dictionary == null || !dictionary.Any()) { return(_baseUrl); } return(QueryHelpers.AddQueryString(_baseUrl, dictionary)); }
/// <summary> /// Creates URL based on key/value input pairs. /// </summary> /// <param name="values">The values (either as a Dictionary of string/string or as a type with properties).</param> /// <returns></returns> public string Create(object values) { var dictionary = ValuesHelper.ObjectToDictionary(values); if (dictionary == null || !dictionary.Any()) { return(_baseUrl); } var qs = string.Join("&", dictionary.Select(kvp => string.Format("{0}={1}", WebUtility.UrlEncode(kvp.Key), WebUtility.UrlEncode(kvp.Value))).ToArray()); return(string.Format("{0}?{1}", _baseUrl, qs)); }
/// <summary> /// Creates a end_session URL. /// </summary> /// <param name="request">The request.</param> /// <param name="idTokenHint">The id_token hint.</param> /// <param name="postLogoutRedirectUri">The post logout redirect URI.</param> /// <param name="state">The state.</param> /// <param name="extra">The extra parameters.</param> /// <returns></returns> public static string CreateEndSessionUrl(this RequestUrl request, string idTokenHint = null, string postLogoutRedirectUri = null, string state = null, object extra = null) { var values = new Dictionary <string, string>(); values.AddOptional(OidcConstants.EndSessionRequest.IdTokenHint, idTokenHint); values.AddOptional(OidcConstants.EndSessionRequest.PostLogoutRedirectUri, postLogoutRedirectUri); values.AddOptional(OidcConstants.EndSessionRequest.State, state); return(request.Create(ValuesHelper.Merge(values, ValuesHelper.ObjectToDictionary(extra)))); }
/// <summary> /// Creates URL based on key/value input pairs. /// </summary> /// <param name="values">The values (either as a Dictionary of string/string or as a type with properties).</param> /// <returns></returns> public string Create(object values) { var dictionary = ValuesHelper.ObjectToDictionary(values); if (dictionary == null || !dictionary.Any()) { return(_baseUrl); } var encoder = UrlEncoder.Default; var qs = string.Join("&", dictionary.Where(d => d.Value != null).Select(kvp => string.Format("{0}={1}", encoder.Encode(kvp.Key), encoder.Encode(kvp.Value))).ToArray()); return(string.Format("{0}?{1}", _baseUrl, qs)); }
/// <summary> /// Creates an authorize URL. /// </summary> /// <param name="request">The request.</param> /// <param name="clientId">The client identifier.</param> /// <param name="responseType">The response type.</param> /// <param name="scope">The scope.</param> /// <param name="redirectUri">The redirect URI.</param> /// <param name="state">The state.</param> /// <param name="nonce">The nonce.</param> /// <param name="loginHint">The login hint.</param> /// <param name="acrValues">The acr values.</param> /// <param name="prompt">The prompt.</param> /// <param name="responseMode">The response mode.</param> /// <param name="codeChallenge">The code challenge.</param> /// <param name="codeChallengeMethod">The code challenge method.</param> /// <param name="display">The display option.</param> /// <param name="maxAge">The max age.</param> /// <param name="uiLocales">The ui locales.</param> /// <param name="idTokenHint">The id_token hint.</param> /// <param name="extra">Extra parameters.</param> /// <returns></returns> public static string CreateAuthorizeUrl(this RequestUrl request, string clientId, string responseType, string scope = null, string redirectUri = null, string state = null, string nonce = null, string loginHint = null, string acrValues = null, string prompt = null, string responseMode = null, string codeChallenge = null, string codeChallengeMethod = null, string display = null, int?maxAge = null, string uiLocales = null, string idTokenHint = null, object extra = null) { var values = new Dictionary <string, string> { { OidcConstants.AuthorizeRequest.ClientId, clientId }, { OidcConstants.AuthorizeRequest.ResponseType, responseType } }; values.AddOptional(OidcConstants.AuthorizeRequest.Scope, scope); values.AddOptional(OidcConstants.AuthorizeRequest.RedirectUri, redirectUri); values.AddOptional(OidcConstants.AuthorizeRequest.State, state); values.AddOptional(OidcConstants.AuthorizeRequest.Nonce, nonce); values.AddOptional(OidcConstants.AuthorizeRequest.LoginHint, loginHint); values.AddOptional(OidcConstants.AuthorizeRequest.AcrValues, acrValues); values.AddOptional(OidcConstants.AuthorizeRequest.Prompt, prompt); values.AddOptional(OidcConstants.AuthorizeRequest.ResponseMode, responseMode); values.AddOptional(OidcConstants.AuthorizeRequest.CodeChallenge, codeChallenge); values.AddOptional(OidcConstants.AuthorizeRequest.CodeChallengeMethod, codeChallengeMethod); values.AddOptional(OidcConstants.AuthorizeRequest.Display, display); values.AddOptional(OidcConstants.AuthorizeRequest.MaxAge, maxAge?.ToString()); values.AddOptional(OidcConstants.AuthorizeRequest.UiLocales, uiLocales); values.AddOptional(OidcConstants.AuthorizeRequest.IdTokenHint, idTokenHint); return(request.Create(ValuesHelper.Merge(values, ValuesHelper.ObjectToDictionary(extra)))); }
/// <summary> /// Creates an authorize URL. /// </summary> /// <param name="request">The request.</param> /// <param name="values">The values (either using a string Dictionary or an object's properties).</param> /// <returns></returns> public static string Create(this RequestUrl request, object values) { return(request.Create(ValuesHelper.ObjectToDictionary(values))); }
/// <summary> /// Requests a token using a custom request /// </summary> /// <param name="client">The client.</param> /// <param name="values">The values.</param> /// <param name="cancellationToken">The cancellation token.</param> /// <returns></returns> public static Task <TokenResponse> RequestCustomAsync(this TokenClient client, object values, CancellationToken cancellationToken = default(CancellationToken)) { return(client.RequestAsync(client.Merge(ValuesHelper.ObjectToDictionary(values)), cancellationToken)); }