Skip to content

Commit e3253d4

Browse files
martincostelloHaoK
authored andcommitted
More RNG.Fill() and UnixEpoch usage (#18132)
Apply more changes omitted from #18126 and #18128, and use RandomNumberGenerator.Fill() and DateTimeOffset.UnixEpoch for supported TFMs.
1 parent 840e10d commit e3253d4

File tree

3 files changed

+25
-4
lines changed

3 files changed

+25
-4
lines changed

src/DataProtection/DataProtection/src/Managed/ManagedGenRandomImpl.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@ namespace Microsoft.AspNetCore.DataProtection.Managed
88
{
99
internal unsafe sealed class ManagedGenRandomImpl : IManagedGenRandom
1010
{
11+
#if NETSTANDARD2_0
1112
private static readonly RandomNumberGenerator _rng = RandomNumberGenerator.Create();
13+
#endif
1214
public static readonly ManagedGenRandomImpl Instance = new ManagedGenRandomImpl();
1315

1416
private ManagedGenRandomImpl()
@@ -18,7 +20,11 @@ private ManagedGenRandomImpl()
1820
public byte[] GenRandom(int numBytes)
1921
{
2022
var bytes = new byte[numBytes];
23+
#if NETSTANDARD2_0
2124
_rng.GetBytes(bytes);
25+
#else
26+
RandomNumberGenerator.Fill(bytes);
27+
#endif
2228
return bytes;
2329
}
2430
}

src/Identity/Extensions.Core/src/Rfc6238AuthenticationService.cs

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,32 @@
11
// Copyright (c) .NET Foundation. All rights reserved.
22
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
33

4+
using System;
45
using System.Diagnostics;
56
using System.Net;
67
using System.Security.Cryptography;
8+
using System.Text;
79

810
namespace Microsoft.AspNetCore.Identity
911
{
10-
using System;
11-
using System.Text;
12-
1312
internal static class Rfc6238AuthenticationService
1413
{
15-
private static readonly DateTime _unixEpoch = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
1614
private static readonly TimeSpan _timestep = TimeSpan.FromMinutes(3);
1715
private static readonly Encoding _encoding = new UTF8Encoding(false, true);
16+
#if NETSTANDARD2_0
17+
private static readonly DateTime _unixEpoch = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
1818
private static readonly RandomNumberGenerator _rng = RandomNumberGenerator.Create();
19+
#endif
1920

2021
// Generates a new 80-bit security token
2122
public static byte[] GenerateRandomKey()
2223
{
2324
byte[] bytes = new byte[20];
25+
#if NETSTANDARD2_0
2426
_rng.GetBytes(bytes);
27+
#else
28+
RandomNumberGenerator.Fill(bytes);
29+
#endif
2530
return bytes;
2631
}
2732

@@ -63,7 +68,11 @@ private static byte[] ApplyModifier(byte[] input, string modifier)
6368
// More info: https://tools.ietf.org/html/rfc6238#section-4
6469
private static ulong GetCurrentTimeStepNumber()
6570
{
71+
#if NETSTANDARD2_0
6672
var delta = DateTime.UtcNow - _unixEpoch;
73+
#else
74+
var delta = DateTimeOffset.UtcNow - DateTimeOffset.UnixEpoch;
75+
#endif
6776
return (ulong)(delta.Ticks / _timestep.Ticks);
6877
}
6978

src/Identity/Extensions.Core/src/UserManager.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,9 @@ public class UserManager<TUser> : IDisposable where TUser : class
4343

4444
private TimeSpan _defaultLockout = TimeSpan.Zero;
4545
private bool _disposed;
46+
#if NETSTANDARD2_0
4647
private static readonly RandomNumberGenerator _rng = RandomNumberGenerator.Create();
48+
#endif
4749
private IServiceProvider _services;
4850

4951
/// <summary>
@@ -2428,7 +2430,11 @@ private IUserRoleStore<TUser> GetUserRoleStore()
24282430
private static string NewSecurityStamp()
24292431
{
24302432
byte[] bytes = new byte[20];
2433+
#if NETSTANDARD2_0
24312434
_rng.GetBytes(bytes);
2435+
#else
2436+
RandomNumberGenerator.Fill(bytes);
2437+
#endif
24322438
return Base32.ToBase32(bytes);
24332439
}
24342440

0 commit comments

Comments
 (0)