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

Commit 4f7eb91

Browse files
driesengjkotalik
authored andcommitted
Do not check if key is present before removing item. (#1064)
Use Nullable<T>.GetValueOrDefault() instead of Nullable<T>.Value when it is known to have a value.
1 parent 0fd20a2 commit 4f7eb91

File tree

2 files changed

+99
-5
lines changed

2 files changed

+99
-5
lines changed

src/Microsoft.AspNetCore.Authentication.Abstractions/AuthenticationProperties.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ public void SetString(string key, string value)
122122
{
123123
Items[key] = value;
124124
}
125-
else if (Items.ContainsKey(key))
125+
else
126126
{
127127
Items.Remove(key);
128128
}
@@ -169,9 +169,9 @@ protected void SetBool(string key, bool? value)
169169
{
170170
if (value.HasValue)
171171
{
172-
Items[key] = value.Value.ToString();
172+
Items[key] = value.GetValueOrDefault().ToString();
173173
}
174-
else if (Items.ContainsKey(key))
174+
else
175175
{
176176
Items.Remove(key);
177177
}
@@ -201,9 +201,9 @@ protected void SetDateTimeOffset(string key, DateTimeOffset? value)
201201
{
202202
if (value.HasValue)
203203
{
204-
Items[key] = value.Value.ToString(UtcDateTimeFormat, CultureInfo.InvariantCulture);
204+
Items[key] = value.GetValueOrDefault().ToString(UtcDateTimeFormat, CultureInfo.InvariantCulture);
205205
}
206-
else if (Items.ContainsKey(key))
206+
else
207207
{
208208
Items.Remove(key);
209209
}

test/Microsoft.AspNetCore.Authentication.Core.Test/AuthenticationPropertiesTests.cs

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.Collections.Generic;
3+
using System.Globalization;
34
using System.Linq;
45
using Xunit;
56

@@ -73,6 +74,10 @@ public void GetSetString()
7374
props.SetString("foo", null);
7475
Assert.Null(props.GetString("foo"));
7576
Assert.Equal(1, props.Items.Count);
77+
78+
props.SetString("doesntexist", null);
79+
Assert.False(props.Items.ContainsKey("doesntexist"));
80+
Assert.Equal(1, props.Items.Count);
7681
}
7782

7883
[Fact]
@@ -203,5 +208,94 @@ public void AllowRefresh_Test()
203208
props.Items.Clear();
204209
Assert.Null(props.AllowRefresh);
205210
}
211+
212+
[Fact]
213+
public void SetDateTimeOffset()
214+
{
215+
var props = new MyAuthenticationProperties();
216+
217+
props.SetDateTimeOffset("foo", new DateTimeOffset(new DateTime(2018, 03, 19, 12, 34, 56, DateTimeKind.Utc)));
218+
Assert.Equal("Mon, 19 Mar 2018 12:34:56 GMT", props.Items["foo"]);
219+
220+
props.SetDateTimeOffset("foo", null);
221+
Assert.False(props.Items.ContainsKey("foo"));
222+
223+
props.SetDateTimeOffset("doesnotexist", null);
224+
Assert.False(props.Items.ContainsKey("doesnotexist"));
225+
}
226+
227+
[Fact]
228+
public void GetDateTimeOffset()
229+
{
230+
var props = new MyAuthenticationProperties();
231+
var dateTimeOffset = new DateTimeOffset(new DateTime(2018, 03, 19, 12, 34, 56, DateTimeKind.Utc));
232+
233+
props.Items["foo"] = dateTimeOffset.ToString("r", CultureInfo.InvariantCulture);
234+
Assert.Equal(dateTimeOffset, props.GetDateTimeOffset("foo"));
235+
236+
props.Items.Remove("foo");
237+
Assert.Null(props.GetDateTimeOffset("foo"));
238+
239+
props.Items["foo"] = "BAR";
240+
Assert.Null(props.GetDateTimeOffset("foo"));
241+
Assert.Equal("BAR", props.Items["foo"]);
242+
}
243+
244+
[Fact]
245+
public void SetBool()
246+
{
247+
var props = new MyAuthenticationProperties();
248+
249+
props.SetBool("foo", true);
250+
Assert.Equal(true.ToString(), props.Items["foo"]);
251+
252+
props.SetBool("foo", false);
253+
Assert.Equal(false.ToString(), props.Items["foo"]);
254+
255+
props.SetBool("foo", null);
256+
Assert.False(props.Items.ContainsKey("foo"));
257+
}
258+
259+
[Fact]
260+
public void GetBool()
261+
{
262+
var props = new MyAuthenticationProperties();
263+
264+
props.Items["foo"] = true.ToString();
265+
Assert.True(props.GetBool("foo"));
266+
267+
props.Items["foo"] = false.ToString();
268+
Assert.False(props.GetBool("foo"));
269+
270+
props.Items["foo"] = null;
271+
Assert.Null(props.GetBool("foo"));
272+
273+
props.Items["foo"] = "BAR";
274+
Assert.Null(props.GetBool("foo"));
275+
Assert.Equal("BAR", props.Items["foo"]);
276+
}
277+
278+
public class MyAuthenticationProperties : AuthenticationProperties
279+
{
280+
public new DateTimeOffset? GetDateTimeOffset(string key)
281+
{
282+
return base.GetDateTimeOffset(key);
283+
}
284+
285+
public new void SetDateTimeOffset(string key, DateTimeOffset? value)
286+
{
287+
base.SetDateTimeOffset(key, value);
288+
}
289+
290+
public new void SetBool(string key, bool? value)
291+
{
292+
base.SetBool(key, value);
293+
}
294+
295+
public new bool? GetBool(string key)
296+
{
297+
return base.GetBool(key);
298+
}
299+
}
206300
}
207301
}

0 commit comments

Comments
 (0)