Unpredictable Unique Identifier

前端 未结 4 917
情书的邮戳
情书的邮戳 2021-01-03 03:40

For an application I am working on, I need to generate a session token which must have following properties:

  • it should be unique for the application at least,
4条回答
  •  南方客
    南方客 (楼主)
    2021-01-03 04:34

    You could use the cryptographic RandomNumberGenerator and get however many bytes you want to generate an identifier of suitable length.

        RandomNumberGenerator rng = RandomNumberGenerator.Create();
    
        byte[] bytes = new byte[8];
        rng.GetBytes(bytes);
    
        // produces a string like "4jpKc52ArXU="
        var s = Convert.ToBase64String(bytes);
    

提交回复
热议问题