Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I create a unique activation code?

I am trying to create a unique activation code that doesn't already exist in the database. My question is how can I test this?

I tried using a breakpoint then changing the db table to the new result but it doesn't pick up

private string CreateActivationCode()
{
    string chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    Random random = new Random();
    string result = new string(
        Enumerable.Repeat(chars, 4)
                  .Select(s => s[random.Next(s.Length)])
                  .ToArray());

    IEnumerable<string> existingActivationKeys = _mobileDeviceService.GetAll().Select(x => x.UsageKey).ToList();

    if (existingActivationKeys.Contains(result))
    {
        //USE GO TO????
        CreateUsageKey();
    }

    return result;
}
like image 886
John Avatar asked Apr 02 '14 22:04

John


People also ask

What is unique activation code?

Your unique activation code is what we use to identify your DNA sample. For privacy and security, your DNA sample is only identifiable by the activation code, not your personal information.

How are activation codes generated?

Activation codes are based on Site/MID codes from remote computers so in order to be able to generate activation codes you need to obtain these codes from end user.

What does an activation code look like?

An activation key is a code that is used to register or activate a software application. It is typically composed of letters and numbers, often with hyphens in between activation key segments.


1 Answers

As Dean Ward suggested in his comment, you could instead use a GUID as your activation key.

An example of how this could be done is as follows:

private string CreateActivationKey()
{
    var activationKey = Guid.NewGuid().ToString();

    var activationKeyAlreadyExists = 
     mobileDeviceService.GetActivationKeys().Any(key => key == activationKey);

    if (activationKeyAlreadyExists)
    {
        activationKey = CreateActivationKey();
    }

    return activationKey;
}

I've used "GetActivationKeys" to keep my solution in-line with your "GetAll" method; However I'd probably implement a method to perform a database query to check for the existence of a key (bringing back all the activation keys to your service is not the most performant solution).

The likelihood of generating a duplicate GUID is very low. A nice article about GUIDs is here.

like image 82
Ben Smith Avatar answered Sep 17 '22 13:09

Ben Smith