Unpredictable Unique Identifier

For the application I'm working on, I need to create a session token, which should have the following properties:

  • it must be unique to the application, at least globally unique would be even better
  • he must be unpredictable
  • it should not be too long, as it should be passed in the header of the HTTP request, therefore it is better better

I was thinking of adapting a Guid structure with a cryptographic random number, but it could be overkill. Does anyone know / created a data structure that matches all of these properties?

+5
source share
4 answers

. GUID , .

GUID , , . GUID , , GUID , GUID , MAC- . , GUID .

, , .

: , , . GUID , , . , GUID. GUID , .. GUID - - , , .

GUID - , , .. -, .

+28

RandomNumberGenerator , .

    RandomNumberGenerator rng = RandomNumberGenerator.Create();

    byte[] bytes = new byte[8];
    rng.GetBytes(bytes);

    // produces a string like "4jpKc52ArXU="
    var s = Convert.ToBase64String(bytes);
+7

, ASP.NET , :

using System;
using System.IO;
using System.Web;
using System.Web.SessionState;

class Program
{
    static void Main()
    {
        var request = new HttpRequest("", "http://localhost", "");
        var response = new HttpResponse(TextWriter.Null);
        var context = new HttpContext(request, response);
        var id = new SessionIDManager().CreateSessionID(context);
        Console.WriteLine(id);
    }
}
+3

, GUID - 128- , . , 64- - . , , -, .

, . - , , GUID, , , , , -.

+1

All Articles