From 49a13ba3b08674928af23b9e4fbcf65d7c74ac8b Mon Sep 17 00:00:00 2001 From: alexandre-spieser Date: Thu, 30 Nov 2017 22:54:26 +0000 Subject: [PATCH] Updated packages to 2.0.1 and added support for primary keys of type long. --- .../MongoIdentitySample.Mvc.csproj | 27 ++++---- src/AspNetCore.Identity.MongoDbCore.csproj | 6 +- src/Extensions/RandomExtensions.cs | 61 +++++++++++++++++++ src/Models/MongoIdentityRole.cs | 9 ++- src/Models/MongoIdentityUser.cs | 7 +++ ...entity.MongoDbCore.IntegrationTests.csproj | 22 +++---- .../UserStoreLongKeyTest.cs | 31 ++++++++++ 7 files changed, 136 insertions(+), 27 deletions(-) create mode 100644 src/Extensions/RandomExtensions.cs create mode 100644 test/AspNetCore.Identity.MongoDbCore.IntegrationTests/AspNetCore.Identity.MongoDbCore.Test/UserStoreLongKeyTest.cs diff --git a/sample/MongoIdentitySample.Mvc/MongoIdentitySample.Mvc.csproj b/sample/MongoIdentitySample.Mvc/MongoIdentitySample.Mvc.csproj index a6d52b8..25cd7d3 100644 --- a/sample/MongoIdentitySample.Mvc/MongoIdentitySample.Mvc.csproj +++ b/sample/MongoIdentitySample.Mvc/MongoIdentitySample.Mvc.csproj @@ -8,21 +8,20 @@ - - - - + + + - - - - - - + + + + + + - - + + @@ -31,4 +30,8 @@ + + + + diff --git a/src/AspNetCore.Identity.MongoDbCore.csproj b/src/AspNetCore.Identity.MongoDbCore.csproj index d3231fc..de25841 100644 --- a/src/AspNetCore.Identity.MongoDbCore.csproj +++ b/src/AspNetCore.Identity.MongoDbCore.csproj @@ -14,10 +14,10 @@ - - + + - + diff --git a/src/Extensions/RandomExtensions.cs b/src/Extensions/RandomExtensions.cs new file mode 100644 index 0000000..a63328d --- /dev/null +++ b/src/Extensions/RandomExtensions.cs @@ -0,0 +1,61 @@ +using System; + +namespace AspNetCore.Identity.MongoDbCore.Extensions +{ + // Thanks BlueRaja - Danny Pflughoeft https://stackoverflow.com/a/6651656/5103354 + /// + /// Extensions for the random number generator + /// + public static class RandomExtensions + { + + /// + /// Returns a random long from min (inclusive) to max (exclusive) + /// + /// The given random instance + /// The inclusive minimum bound + /// The exclusive maximum bound. Must be greater than min + public static long NextLong(this Random random, long min, long max) + { + if (max <= min) + throw new ArgumentOutOfRangeException("max", "max must be > min!"); + + //Working with ulong so that modulo works correctly with values > long.MaxValue + ulong uRange = (ulong)(max - min); + + //Prevent a modulo bias; see https://stackoverflow.com/a/10984975/238419 + //for more information. + //In the worst case, the expected number of calls is 2 (though usually it's + //much closer to 1) so this loop doesn't really hurt performance at all. + ulong ulongRand; + do + { + byte[] buf = new byte[8]; + random.NextBytes(buf); + ulongRand = (ulong)BitConverter.ToInt64(buf, 0); + } while (ulongRand > ulong.MaxValue - ((ulong.MaxValue % uRange) + 1) % uRange); + + return (long)(ulongRand % uRange) + min; + } + + /// + /// Returns a random long from 0 (inclusive) to max (exclusive) + /// + /// The given random instance + /// The exclusive maximum bound. Must be greater than 0 + public static long NextLong(this Random random, long max) + { + return random.NextLong(0, max); + } + + /// + /// Returns a random long over all possible values of long (except long.MaxValue, similar to + /// random.Next()) + /// + /// The given random instance + public static long NextLong(this Random random) + { + return random.NextLong(long.MinValue, long.MaxValue); + } + } +} diff --git a/src/Models/MongoIdentityRole.cs b/src/Models/MongoIdentityRole.cs index 9769bfc..3ee76a7 100644 --- a/src/Models/MongoIdentityRole.cs +++ b/src/Models/MongoIdentityRole.cs @@ -1,4 +1,5 @@ -using AspNetCore.Identity.MongoDbCore.Interfaces; +using AspNetCore.Identity.MongoDbCore.Extensions; +using AspNetCore.Identity.MongoDbCore.Interfaces; using Microsoft.AspNetCore.Identity; using MongoDbGenericRepository.Models; using System; @@ -67,9 +68,15 @@ namespace AspNetCore.Identity.MongoDbCore.Models case "Guid": Id = (TKey)(object)guidValue; break; + case "Int16": + Id = (TKey)(object)GlobalVariables.Random.Next(1, short.MaxValue); + break; case "Int32": Id = (TKey)(object)GlobalVariables.Random.Next(1, int.MaxValue); break; + case "Int64": + Id = (TKey)(object)(GlobalVariables.Random.NextLong(1, long.MaxValue)); + break; case "String": Id = (TKey)(object)guidValue.ToString(); break; diff --git a/src/Models/MongoIdentityUser.cs b/src/Models/MongoIdentityUser.cs index cf00272..77ce722 100644 --- a/src/Models/MongoIdentityUser.cs +++ b/src/Models/MongoIdentityUser.cs @@ -5,6 +5,7 @@ using System.Linq; using MongoDB.Driver; using AspNetCore.Identity.MongoDbCore.Interfaces; using Microsoft.AspNetCore.Identity; +using AspNetCore.Identity.MongoDbCore.Extensions; namespace AspNetCore.Identity.MongoDbCore.Models { @@ -373,9 +374,15 @@ namespace AspNetCore.Identity.MongoDbCore.Models case "Guid": Id = (TKey)(object)guidValue; break; + case "Int16": + Id = (TKey)(object)GlobalVariables.Random.Next(1, short.MaxValue); + break; case "Int32": Id = (TKey)(object)GlobalVariables.Random.Next(1, int.MaxValue); break; + case "Int64": + Id = (TKey)(object)(GlobalVariables.Random.NextLong(1, long.MaxValue)); + break; case "String": Id = (TKey)(object)guidValue.ToString(); break; diff --git a/test/AspNetCore.Identity.MongoDbCore.IntegrationTests/AspNetCore.Identity.MongoDbCore.IntegrationTests.csproj b/test/AspNetCore.Identity.MongoDbCore.IntegrationTests/AspNetCore.Identity.MongoDbCore.IntegrationTests.csproj index 625a0d9..d09052a 100644 --- a/test/AspNetCore.Identity.MongoDbCore.IntegrationTests/AspNetCore.Identity.MongoDbCore.IntegrationTests.csproj +++ b/test/AspNetCore.Identity.MongoDbCore.IntegrationTests/AspNetCore.Identity.MongoDbCore.IntegrationTests.csproj @@ -7,21 +7,21 @@ - - + + - + - - + + - - - - + + + + - - + + diff --git a/test/AspNetCore.Identity.MongoDbCore.IntegrationTests/AspNetCore.Identity.MongoDbCore.Test/UserStoreLongKeyTest.cs b/test/AspNetCore.Identity.MongoDbCore.IntegrationTests/AspNetCore.Identity.MongoDbCore.Test/UserStoreLongKeyTest.cs new file mode 100644 index 0000000..4026a52 --- /dev/null +++ b/test/AspNetCore.Identity.MongoDbCore.IntegrationTests/AspNetCore.Identity.MongoDbCore.Test/UserStoreLongKeyTest.cs @@ -0,0 +1,31 @@ +// Copyright (c) .NET Foundation. All rights reserved. +// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. + +using System; +using AspNetCore.Identity.MongoDbCore.Models; + +namespace AspNetCore.Identity.MongoDbCore.Test +{ + public class LongUser : MongoIdentityUser + { + public LongUser() : base() + { + } + } + + public class LongRole : MongoIdentityRole + { + public LongRole() : base() + { + Name = Guid.NewGuid().ToString(); + } + } + + public class UserStoreLongTest : MongoDbStoreTestBase + { + public UserStoreLongTest(MongoDatabaseFixture fixture) + : base(fixture) + { + } + } +} \ No newline at end of file