Generating a unique *and* random URL in C#

前端 未结 5 791
孤街浪徒
孤街浪徒 2021-02-08 04:44

My ultimate goal is to create a URL that is unique and cannot be guessed/predicted. The purpose of this URL is to allow users to perform operations like verifying their email ad

5条回答
  •  遇见更好的自我
    2021-02-08 05:29

    [Edit: I missed the call to RNGCryptoServiceProvider above. Apologies about that.]

    The problem with RNG generators like the RNGCryptoServiceProvider for your case is they do not guarantee uniqueness. Guids, as you know, are statistically unlikely to be unique but do not guarantee it. The only real way to guarantee both uniqueness and randomness is to generate the GUID like you have then put it in a searchable store, like a database table. Whenever you generate a new value, check it is not already present. If it is, discard it and generate a new value.

提交回复
热议问题