RandomNumberGenerator in ASP.NET5

旧城冷巷雨未停 提交于 2019-12-24 00:37:52

问题


I have created a new Web API project using the ASP.NET 5 Templates. I am wanting to generate random numbers using System.Security.Cryptography.RandomNumberGenerator but it is not available in the .NET Platform 5.4 (see screenshot).

Compiling it also generates 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 which is cryptographically secure?


回答1:


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

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



来源:https://stackoverflow.com/questions/34345271/randomnumbergenerator-in-asp-net5

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!