RandomNumberGenerator in ASP.NET5

I created a new web API project using ASP.NET 5 patterns. I want to generate random numbers using System.Security.Cryptography.RandomNumberGenerator, but it is not available in the .NET Platform 5.4 platform (see screenshot).

Error message

Compilation also leads to the following errors:

Error CS0234 The type or namespace name 'Cryptography' does not exist in the namespace 'System.Security' (are you missing an assembly reference?) Error CS0246 The type or namespace name 'RandomNumberGenerator' could not be found (are you missing a using directive or an assembly reference?) Error CS0103 The name 'RandomNumberGenerator' does not exist in the current context 

Is there a way to use this, or is there an alternative random generator that I can use that is cryptographically secure?

+2
source share
1 answer

I got it working thanks to Scott Chamberlain's comment by adding the System.Security.Cryptography.Algorithms NuGet package.

I also had to change project.json so that it was just a dotnet5.4 dependency, not a global dependency, otherwise the compiler would complain that RandomNumberGenerator exists in both System.Security.Cryptography.Algorithms and mscorlib.

+2
source

All Articles