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;
}
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With