How to uniquely idenitfy requests?

时间:2017-08-04 12:39:10

标签: .net random

Remote API that I'm using, requires referenceId parameter that should uniquely identify each request. I don't want to increment numbers because I might send concurrent requests and moreover, I would need to save those numbers somewhere (for example if I decide to restart the application). referenceId should be all numbers, up to 48 characters. Could I generate a random 48-place number using RNGCryptoServiceProvider, would that be unique?

2 个答案:

答案 0 :(得分:1)

You can generate unique integers based on GUID. Look on that other topic : How generate unique Integers based on GUIDs

答案 1 :(得分:1)

Yes, That will be unique..

 public static byte[] GenerateKey(int keySize)
        {
            using (var randomNumberGenerator = new RNGCryptoServiceProvider())
            {
                var randomNumber = new byte[keySize];
                randomNumberGenerator.GetBytes(randomNumber);

                return randomNumber;
            }
        }

Aside from that, Do you have a problem in storing that value?