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

Commit 08a1c50

Browse files
committed
Responding to comments : var changes
1 parent 20fdbe2 commit 08a1c50

File tree

6 files changed

+21
-18
lines changed

6 files changed

+21
-18
lines changed

src/Microsoft.AspNet.Mvc.Core/AntiForgery/AntiForgery.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,8 @@ public AntiForgery([NotNull] IClaimUidExtractor claimUidExtractor,
3535
/// <returns>An HTML string corresponding to an &lt;input type="hidden"&gt;
3636
/// element. This element should be put inside a &lt;form&gt;.</returns>
3737
/// <remarks>
38-
/// This method has a side effect: A response cookie is set if there is no valid cookie associated with the request.
38+
/// This method has a side effect:
39+
/// A response cookie is set if there is no valid cookie associated with the request.
3940
/// </remarks>
4041
public HtmlString GetHtml([NotNull] HttpContext context)
4142
{

src/Microsoft.AspNet.Mvc.Core/AntiForgery/AntiForgeryTokenSerializer.cs

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ internal AntiForgeryTokenSerializer([NotNull] IDataProtector cryptoSystem)
1919

2020
public AntiForgeryToken Deserialize(string serializedToken)
2121
{
22+
Exception innerException = null;
2223
try
2324
{
2425
using (MemoryStream stream = new MemoryStream(UrlTokenDecode(serializedToken)))
@@ -33,13 +34,14 @@ public AntiForgeryToken Deserialize(string serializedToken)
3334
}
3435
}
3536
}
36-
catch
37+
catch(Exception ex)
3738
{
3839
// swallow all exceptions - homogenize error if something went wrong
40+
innerException = ex;
3941
}
4042

4143
// if we reached this point, something went wrong deserializing
42-
throw new InvalidOperationException(Resources.AntiForgeryToken_DeserializationFailed);
44+
throw new InvalidOperationException(Resources.AntiForgeryToken_DeserializationFailed, innerException);
4345
}
4446

4547
/* The serialized format of the anti-XSRF token is as follows:
@@ -57,14 +59,14 @@ public AntiForgeryToken Deserialize(string serializedToken)
5759
private static AntiForgeryToken DeserializeImpl(BinaryReader reader)
5860
{
5961
// we can only consume tokens of the same serialized version that we generate
60-
byte embeddedVersion = reader.ReadByte();
62+
var embeddedVersion = reader.ReadByte();
6163
if (embeddedVersion != TokenVersion)
6264
{
6365
return null;
6466
}
6567

66-
AntiForgeryToken deserializedToken = new AntiForgeryToken();
67-
byte[] securityTokenBytes = reader.ReadBytes(AntiForgeryToken.SecurityTokenBitLength / 8);
68+
var deserializedToken = new AntiForgeryToken();
69+
var securityTokenBytes = reader.ReadBytes(AntiForgeryToken.SecurityTokenBitLength / 8);
6870
deserializedToken.SecurityToken = new BinaryBlob(AntiForgeryToken.SecurityTokenBitLength, securityTokenBytes);
6971
deserializedToken.IsSessionToken = reader.ReadBoolean();
7072

@@ -73,7 +75,7 @@ private static AntiForgeryToken DeserializeImpl(BinaryReader reader)
7375
bool isClaimsBased = reader.ReadBoolean();
7476
if (isClaimsBased)
7577
{
76-
byte[] claimUidBytes = reader.ReadBytes(AntiForgeryToken.ClaimUidBitLength / 8);
78+
var claimUidBytes = reader.ReadBytes(AntiForgeryToken.ClaimUidBitLength / 8);
7779
deserializedToken.ClaimUid = new BinaryBlob(AntiForgeryToken.ClaimUidBitLength, claimUidBytes);
7880
}
7981
else
@@ -96,9 +98,9 @@ private static AntiForgeryToken DeserializeImpl(BinaryReader reader)
9698

9799
public string Serialize([NotNull] AntiForgeryToken token)
98100
{
99-
using (MemoryStream stream = new MemoryStream())
101+
using (var stream = new MemoryStream())
100102
{
101-
using (BinaryWriter writer = new BinaryWriter(stream))
103+
using (var writer = new BinaryWriter(stream))
102104
{
103105
writer.Write(TokenVersion);
104106
writer.Write(token.SecurityToken.GetData());

src/Microsoft.AspNet.Mvc.Core/AntiForgery/AntiForgeryTokenStore.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public async Task<AntiForgeryToken> GetFormTokenAsync(HttpContext httpContext)
4646
public void SaveCookieToken(HttpContext httpContext, AntiForgeryToken token)
4747
{
4848
string serializedToken = _serializer.Serialize(token);
49-
CookieOptions options = new CookieOptions() { HttpOnly = true };
49+
var options = new CookieOptions() { HttpOnly = true };
5050

5151
// Note: don't use "newCookie.Secure = _config.RequireSSL;" since the default
5252
// value of newCookie.Secure is poulated out of band.

src/Microsoft.AspNet.Mvc.Core/AntiForgery/AntiForgeryWorker.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -180,8 +180,8 @@ public async Task ValidateAsync([NotNull] HttpContext httpContext)
180180
CheckSSLConfig(httpContext);
181181

182182
// Extract cookie & form tokens
183-
AntiForgeryToken cookieToken = _tokenStore.GetCookieToken(httpContext);
184-
AntiForgeryToken formToken = await _tokenStore.GetFormTokenAsync(httpContext);
183+
var cookieToken = _tokenStore.GetCookieToken(httpContext);
184+
var formToken = await _tokenStore.GetFormTokenAsync(httpContext);
185185

186186
// Validate
187187
_validator.ValidateTokens(httpContext, ExtractIdentity(httpContext), cookieToken, formToken);
@@ -195,8 +195,8 @@ public void Validate([NotNull] HttpContext httpContext, string cookieToken, stri
195195
CheckSSLConfig(httpContext);
196196

197197
// Extract cookie & form tokens
198-
AntiForgeryToken deserializedCookieToken = DeserializeToken(cookieToken);
199-
AntiForgeryToken deserializedFormToken = DeserializeToken(formToken);
198+
var deserializedCookieToken = DeserializeToken(cookieToken);
199+
var deserializedFormToken = DeserializeToken(formToken);
200200

201201
// Validate
202202
_validator.ValidateTokens(httpContext, ExtractIdentity(httpContext), deserializedCookieToken, deserializedFormToken);

src/Microsoft.AspNet.Mvc.Core/AntiForgery/DefaultClaimUidExtractor.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,9 @@ private static IEnumerable<string> GetUniqueIdentifierParameters(ClaimsIdentity
5353

5454
private static byte[] ComputeSHA256(IEnumerable<string> parameters)
5555
{
56-
using (MemoryStream ms = new MemoryStream())
56+
using (var ms = new MemoryStream())
5757
{
58-
using (BinaryWriter bw = new BinaryWriter(ms))
58+
using (var bw = new BinaryWriter(ms))
5959
{
6060
foreach (string parameter in parameters)
6161
{
@@ -64,7 +64,7 @@ private static byte[] ComputeSHA256(IEnumerable<string> parameters)
6464

6565
bw.Flush();
6666

67-
using (SHA256 sha256 = SHA256.Create())
67+
using (var sha256 = SHA256.Create())
6868
{
6969
byte[] retVal = sha256.ComputeHash(ms.ToArray(), 0, checked((int)ms.Length));
7070
return retVal;

src/Microsoft.AspNet.Mvc.Core/AntiForgery/TokenProvider.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public AntiForgeryToken GenerateFormToken(HttpContext httpContext,
3737
{
3838
Contract.Assert(IsCookieTokenValid(cookieToken));
3939

40-
AntiForgeryToken formToken = new AntiForgeryToken()
40+
var formToken = new AntiForgeryToken()
4141
{
4242
SecurityToken = cookieToken.SecurityToken,
4343
IsSessionToken = false

0 commit comments

Comments
 (0)