Skip to content
This repository was archived by the owner on Dec 13, 2018. It is now read-only.

Commit 35ad3ec

Browse files
committed
Code cleanup.
1 parent 07b3fe2 commit 35ad3ec

File tree

6 files changed

+54
-77
lines changed

6 files changed

+54
-77
lines changed

samples/CookieSessionSample/MemoryCacheSessionStore.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
using System;
22
using System.Threading.Tasks;
3-
using Microsoft.AspNet.MemoryCache;
43
using Microsoft.AspNet.Security;
54
using Microsoft.AspNet.Security.Cookies.Infrastructure;
5+
using Microsoft.Framework.Cache.Memory;
66

77
namespace CookieSessionSample
88
{
@@ -33,7 +33,7 @@ public Task RenewAsync(string key, AuthenticationTicket ticket)
3333
{
3434
context.SetAbsoluteExpiration(expiresUtc.Value);
3535
}
36-
context.SetSlidingExpiraiton(TimeSpan.FromHours(1)); // TODO: configurable.
36+
context.SetSlidingExpiration(TimeSpan.FromHours(1)); // TODO: configurable.
3737

3838
return (AuthenticationTicket)context.State;
3939
});

samples/CookieSessionSample/Startup.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ public void Configure(IApplicationBuilder app)
1717

1818
app.Run(async context =>
1919
{
20-
if (context.User == null || !context.User.Identity.IsAuthenticated)
20+
if (context.User.Identity == null || !context.User.Identity.IsAuthenticated)
2121
{
2222
// Make a large identity
2323
var claims = new List<Claim>(1001);

samples/CookieSessionSample/project.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@
44
"Microsoft.AspNet.Hosting": "1.0.0-*",
55
"Microsoft.AspNet.Http": "1.0.0-*",
66
"Microsoft.AspNet.HttpFeature": "1.0.0-*",
7-
"Microsoft.AspNet.MemoryCache": "1.0.0-*",
87
"Microsoft.AspNet.PipelineCore": "1.0.0-*",
98
"Microsoft.AspNet.RequestContainer": "1.0.0-*",
109
"Microsoft.AspNet.Security": "1.0.0-*",
1110
"Microsoft.AspNet.Security.Cookies": "1.0.0-*",
1211
"Microsoft.AspNet.Server.WebListener": "1.0.0-*",
12+
"Microsoft.Framework.Cache.Memory": "1.0.0-*",
1313
"Microsoft.Framework.DependencyInjection": "1.0.0-*"
1414
},
1515
"commands": { "web": "Microsoft.AspNet.Hosting server=Microsoft.AspNet.Server.WebListener server.urls=http://localhost:12345" },

src/Microsoft.AspNet.Security.Cookies/CookieAuthenticationHandler.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ protected override async Task<AuthenticationTicket> AuthenticateCoreAsync()
6363
Claim claim = ticket.Identity.Claims.FirstOrDefault(c => c.Type.Equals(SessionIdClaim));
6464
if (claim == null)
6565
{
66-
_logger.WriteWarning(@"SessoinId missing");
66+
_logger.WriteWarning(@"SessionId missing");
6767
return null;
6868
}
6969
_sessionKey = claim.Value;

src/Microsoft.AspNet.Security.Cookies/CookieAuthenticationMiddleware.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
using System;
66
using Microsoft.AspNet.Builder;
7-
using Microsoft.AspNet.Http;
87
using Microsoft.AspNet.Security.Cookies.Infrastructure;
98
using Microsoft.AspNet.Security.DataHandler;
109
using Microsoft.AspNet.Security.DataProtection;

src/Microsoft.AspNet.Security.Cookies/Infrastructure/ChunkingCookieManager.cs

Lines changed: 49 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ public class ChunkingCookieManager : ICookieManager
1717
{
1818
public ChunkingCookieManager()
1919
{
20+
// Lowest common denominator. Safari has the lowest known limit (4093), and we leave little extra just in case.
21+
// See http://browsercookielimits.x64.me/.
2022
ChunkSize = 4090;
2123
ThrowForPartialCookies = true;
2224
}
@@ -40,9 +42,8 @@ private static int ParseChunksCount(string value)
4042
{
4143
if (value != null && value.StartsWith("chunks:", StringComparison.Ordinal))
4244
{
43-
string chunksCountString = value.Substring("chunks:".Length);
44-
int chunksCount;
45-
if (int.TryParse(chunksCountString, NumberStyles.None, CultureInfo.InvariantCulture, out chunksCount))
45+
var chunksCountString = value.Substring("chunks:".Length);
46+
if (int.TryParse(chunksCountString, NumberStyles.None, CultureInfo.InvariantCulture, out var chunksCount))
4647
{
4748
return chunksCount;
4849
}
@@ -57,28 +58,23 @@ private static int ParseChunksCount(string value)
5758
/// <param name="context"></param>
5859
/// <param name="key"></param>
5960
/// <returns>The reassembled cookie, if any, or null.</returns>
60-
public string GetRequestCookie(HttpContext context, string key)
61+
public string GetRequestCookie([NotNull] HttpContext context, [NotNull] string key)
6162
{
62-
if (context == null)
63-
{
64-
throw new ArgumentNullException("context");
65-
}
66-
67-
IReadableStringCollection requestCookies = context.Request.Cookies;
68-
string value = requestCookies[key];
69-
int chunksCount = ParseChunksCount(value);
63+
var requestCookies = context.Request.Cookies;
64+
var value = requestCookies[key];
65+
var chunksCount = ParseChunksCount(value);
7066
if (chunksCount > 0)
7167
{
72-
bool quoted = false;
73-
string[] chunks = new string[chunksCount];
74-
for (int chunkId = 1; chunkId <= chunksCount; chunkId++)
68+
var quoted = false;
69+
var chunks = new string[chunksCount];
70+
for (var chunkId = 1; chunkId <= chunksCount; chunkId++)
7571
{
76-
string chunk = requestCookies[key + "C" + chunkId.ToString(CultureInfo.InvariantCulture)];
72+
var chunk = requestCookies[key + "C" + chunkId.ToString(CultureInfo.InvariantCulture)];
7773
if (chunk == null)
7874
{
7975
if (ThrowForPartialCookies)
8076
{
81-
int totalSize = 0;
77+
var totalSize = 0;
8278
for (int i = 0; i < chunkId - 1; i++)
8379
{
8480
totalSize += chunks[i].Length;
@@ -97,7 +93,7 @@ public string GetRequestCookie(HttpContext context, string key)
9793
}
9894
chunks[chunkId - 1] = chunk;
9995
}
100-
string merged = string.Join(string.Empty, chunks);
96+
var merged = string.Join(string.Empty, chunks);
10197
if (quoted)
10298
{
10399
merged = Quote(merged);
@@ -119,25 +115,16 @@ public string GetRequestCookie(HttpContext context, string key)
119115
/// <param name="key"></param>
120116
/// <param name="value"></param>
121117
/// <param name="options"></param>
122-
public void AppendResponseCookie(HttpContext context, string key, string value, CookieOptions options)
118+
public void AppendResponseCookie([NotNull] HttpContext context, [NotNull] string key, string value, [NotNull] CookieOptions options)
123119
{
124-
if (context == null)
125-
{
126-
throw new ArgumentNullException("context");
127-
}
128-
if (options == null)
129-
{
130-
throw new ArgumentNullException("options");
131-
}
120+
var domainHasValue = !string.IsNullOrEmpty(options.Domain);
121+
var pathHasValue = !string.IsNullOrEmpty(options.Path);
122+
var expiresHasValue = options.Expires.HasValue;
132123

133-
bool domainHasValue = !string.IsNullOrEmpty(options.Domain);
134-
bool pathHasValue = !string.IsNullOrEmpty(options.Path);
135-
bool expiresHasValue = options.Expires.HasValue;
124+
var escapedKey = Uri.EscapeDataString(key);
125+
var prefix = escapedKey + "=";
136126

137-
string escapedKey = Uri.EscapeDataString(key);
138-
string prefix = escapedKey + "=";
139-
140-
string suffix = string.Concat(
127+
var suffix = string.Concat(
141128
!domainHasValue ? null : "; domain=",
142129
!domainHasValue ? null : options.Domain,
143130
!pathHasValue ? null : "; path=",
@@ -148,19 +135,19 @@ public void AppendResponseCookie(HttpContext context, string key, string value,
148135
!options.HttpOnly ? null : "; HttpOnly");
149136

150137
value = value ?? string.Empty;
151-
bool quoted = false;
138+
var quoted = false;
152139
if (IsQuoted(value))
153140
{
154141
quoted = true;
155142
value = RemoveQuotes(value);
156143
}
157-
string escapedValue = Uri.EscapeDataString(value);
144+
var escapedValue = Uri.EscapeDataString(value);
158145

159146
// Normal cookie
160-
IHeaderDictionary responseHeaders = context.Response.Headers;
147+
var responseHeaders = context.Response.Headers;
161148
if (!ChunkSize.HasValue || ChunkSize.Value > prefix.Length + escapedValue.Length + suffix.Length + (quoted ? 2 : 0))
162149
{
163-
string setCookieValue = string.Concat(
150+
var setCookieValue = string.Concat(
164151
prefix,
165152
quoted ? Quote(escapedValue) : escapedValue,
166153
suffix);
@@ -180,18 +167,18 @@ public void AppendResponseCookie(HttpContext context, string key, string value,
180167
// Set-Cookie: CookieNameC1="Segment1"; path=/
181168
// Set-Cookie: CookieNameC2="Segment2"; path=/
182169
// Set-Cookie: CookieNameC3="Segment3"; path=/
183-
int dataSizePerCookie = ChunkSize.Value - prefix.Length - suffix.Length - (quoted ? 2 : 0) - 3; // Budget 3 chars for the chunkid.
184-
int cookieChunkCount = (int)Math.Ceiling(escapedValue.Length * 1.0 / dataSizePerCookie);
170+
var dataSizePerCookie = ChunkSize.Value - prefix.Length - suffix.Length - (quoted ? 2 : 0) - 3; // Budget 3 chars for the chunkid.
171+
var cookieChunkCount = (int)Math.Ceiling(escapedValue.Length * 1.0 / dataSizePerCookie);
185172

186173
responseHeaders.AppendValues(Constants.Headers.SetCookie, prefix + "chunks:" + cookieChunkCount.ToString(CultureInfo.InvariantCulture) + suffix);
187-
188-
string[] chunks = new string[cookieChunkCount];
189-
int offset = 0;
190-
for (int chunkId = 1; chunkId <= cookieChunkCount; chunkId++)
174+
175+
var chunks = new string[cookieChunkCount];
176+
var offset = 0;
177+
for (var chunkId = 1; chunkId <= cookieChunkCount; chunkId++)
191178
{
192-
int remainingLength = escapedValue.Length - offset;
193-
int length = Math.Min(dataSizePerCookie, remainingLength);
194-
string segment = escapedValue.Substring(offset, length);
179+
var remainingLength = escapedValue.Length - offset;
180+
var length = Math.Min(dataSizePerCookie, remainingLength);
181+
var segment = escapedValue.Substring(offset, length);
195182
offset += length;
196183

197184
chunks[chunkId - 1] = string.Concat(
@@ -215,34 +202,25 @@ public void AppendResponseCookie(HttpContext context, string key, string value,
215202
/// <param name="context"></param>
216203
/// <param name="key"></param>
217204
/// <param name="options"></param>
218-
public void DeleteCookie(HttpContext context, string key, CookieOptions options)
205+
public void DeleteCookie([NotNull] HttpContext context, [NotNull] string key, [NotNull] CookieOptions options)
219206
{
220-
if (context == null)
221-
{
222-
throw new ArgumentNullException("context");
223-
}
224-
if (options == null)
225-
{
226-
throw new ArgumentNullException("options");
227-
}
228-
229-
string escapedKey = Uri.EscapeDataString(key);
230-
List<string> keys = new List<string>();
207+
var escapedKey = Uri.EscapeDataString(key);
208+
var keys = new List<string>();
231209
keys.Add(escapedKey + "=");
232210

233-
string requestCookie = context.Request.Cookies[key];
234-
int chunks = ParseChunksCount(requestCookie);
211+
var requestCookie = context.Request.Cookies[key];
212+
var chunks = ParseChunksCount(requestCookie);
235213
if (chunks > 0)
236214
{
237215
for (int i = 1; i <= chunks + 1; i++)
238216
{
239-
string subkey = escapedKey + "C" + i.ToString(CultureInfo.InvariantCulture);
217+
var subkey = escapedKey + "C" + i.ToString(CultureInfo.InvariantCulture);
240218
keys.Add(subkey + "=");
241219
}
242220
}
243221

244-
bool domainHasValue = !string.IsNullOrEmpty(options.Domain);
245-
bool pathHasValue = !string.IsNullOrEmpty(options.Path);
222+
var domainHasValue = !string.IsNullOrEmpty(options.Domain);
223+
var pathHasValue = !string.IsNullOrEmpty(options.Path);
246224

247225
Func<string, bool> rejectPredicate;
248226
Func<string, bool> predicate = value => keys.Any(k => value.StartsWith(k, StringComparison.OrdinalIgnoreCase));
@@ -259,8 +237,8 @@ public void DeleteCookie(HttpContext context, string key, CookieOptions options)
259237
rejectPredicate = value => predicate(value);
260238
}
261239

262-
IHeaderDictionary responseHeaders = context.Response.Headers;
263-
IList<string> existingValues = responseHeaders.GetValues(Constants.Headers.SetCookie);
240+
var responseHeaders = context.Response.Headers;
241+
var existingValues = responseHeaders.GetValues(Constants.Headers.SetCookie);
264242
if (existingValues != null)
265243
{
266244
responseHeaders.SetValues(Constants.Headers.SetCookie, existingValues.Where(value => !rejectPredicate(value)).ToArray());
@@ -270,7 +248,7 @@ public void DeleteCookie(HttpContext context, string key, CookieOptions options)
270248
context,
271249
key,
272250
string.Empty,
273-
new CookieOptions
251+
new CookieOptions()
274252
{
275253
Path = options.Path,
276254
Domain = options.Domain,
@@ -283,7 +261,7 @@ public void DeleteCookie(HttpContext context, string key, CookieOptions options)
283261
context,
284262
key + "C" + i.ToString(CultureInfo.InvariantCulture),
285263
string.Empty,
286-
new CookieOptions
264+
new CookieOptions()
287265
{
288266
Path = options.Path,
289267
Domain = options.Domain,
@@ -292,17 +270,17 @@ public void DeleteCookie(HttpContext context, string key, CookieOptions options)
292270
}
293271
}
294272

295-
private static bool IsQuoted(string value)
273+
private static bool IsQuoted([NotNull] string value)
296274
{
297275
return value.Length >= 2 && value[0] == '"' && value[value.Length - 1] == '"';
298276
}
299277

300-
private static string RemoveQuotes(string value)
278+
private static string RemoveQuotes([NotNull] string value)
301279
{
302280
return value.Substring(1, value.Length - 2);
303281
}
304282

305-
private static string Quote(string value)
283+
private static string Quote([NotNull] string value)
306284
{
307285
return '"' + value + '"';
308286
}

0 commit comments

Comments
 (0)