Repository navigation
Expand file tree
/
Copy pathNameValueCollectionExtensions.cs
More file actions
147 lines (125 loc) · 4.48 KB
/
Copy pathNameValueCollectionExtensions.cs
File metadata and controls
147 lines (125 loc) · 4.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
// Copyright (c) Brock Allen & Dominick Baier. All rights reserved.
// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information.
using System;
using System.Linq;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Text;
using System.Text.Encodings.Web;
namespace IdentityServer4.Extensions
{
internal static class NameValueCollectionExtensions
{
public static string ToQueryString(this NameValueCollection collection)
{
if (collection.Count == 0)
{
return String.Empty;
}
var builder = new StringBuilder(128);
var first = true;
foreach (string name in collection)
{
var values = collection.GetValues(name);
if (values == null || values.Length == 0)
{
first = AppendNameValuePair(builder, first, true, name, String.Empty);
}
else
{
foreach (var value in values)
{
first = AppendNameValuePair(builder, first, true, name, value);
}
}
}
return builder.ToString();
}
public static string ToFormPost(this NameValueCollection collection)
{
var builder = new StringBuilder(128);
const string inputFieldFormat = "<input type='hidden' name='{0}' value='{1}' />\n";
foreach (string name in collection)
{
var values = collection.GetValues(name);
var value = values.First();
value = HtmlEncoder.Default.Encode(value);
builder.AppendFormat(inputFieldFormat, name, value);
}
return builder.ToString();
}
public static NameValueCollection ToNameValueCollection(this Dictionary<string, string> data)
{
var result = new NameValueCollection();
if (data == null || data.Count == 0)
{
return result;
}
foreach (var name in data.Keys)
{
var value = data[name];
if (value != null)
{
result.Add(name, value);
}
}
return result;
}
public static Dictionary<string, string> ToDictionary(this NameValueCollection collection)
{
return collection.ToScrubbedDictionary();
}
public static Dictionary<string, string> ToScrubbedDictionary(this NameValueCollection collection, params string[] nameFilter)
{
var dict = new Dictionary<string, string>();
if (collection == null || collection.Count == 0)
{
return dict;
}
foreach (string name in collection)
{
var value = collection.Get(name);
if (value != null)
{
if (nameFilter.Contains(name))
{
value = "***REDACTED***";
}
dict.Add(name, value);
}
}
return dict;
}
internal static string ConvertFormUrlEncodedSpacesToUrlEncodedSpaces(string str)
{
if ((str != null) && (str.IndexOf('+') >= 0))
{
str = str.Replace("+", "%20");
}
return str;
}
private static bool AppendNameValuePair(StringBuilder builder, bool first, bool urlEncode, string name, string value)
{
var effectiveName = name ?? String.Empty;
var encodedName = urlEncode ? UrlEncoder.Default.Encode(effectiveName) : effectiveName;
var effectiveValue = value ?? String.Empty;
var encodedValue = urlEncode ? UrlEncoder.Default.Encode(effectiveValue) : effectiveValue;
encodedValue = ConvertFormUrlEncodedSpacesToUrlEncodedSpaces(encodedValue);
if (first)
{
first = false;
}
else
{
builder.Append("&");
}
builder.Append(encodedName);
if (!String.IsNullOrEmpty(encodedValue))
{
builder.Append("=");
builder.Append(encodedValue);
}
return first;
}
}
}