Click here to Skip to main content
15,943,089 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm trying to implement a registration prozess for my webapp. I am using this process for generating a EmailConfirmationToken and sending it via email to the user.

if (result.Succeeded)
     string code = await userManager.GenerateEmailConfirmationTokenAsync(user.Id);
     Uri baseUri = new Uri(OpenIdConnectAuthenticationAuthority);
     Uri callbackUrl = new Uri(baseUri + "/Register/ConfirmEmail/?userId=" + user.Id + "&code=" + HttpUtility.UrlEncode(code));
     await userManager.SendEmailAsync(user.Id, "BIMcube eMail Confirmation", "Please confirm your account by clicking this link: <a href=\"" + callbackUrl + "\">link</a>");

The generated token is very long. This is a example token:


My UserTokenProvider is implemented like this:
var dataProtectionProvider = new DpapiDataProtectionProvider("IdentityServer3");

        if (dataProtectionProvider != null)
            UserTokenProvider = new DataProtectorTokenProvider<User>(dataProtectionProvider.Create("ASP.NET Identity"))
                TokenLifespan = TimeSpan.FromHours(3)

Is there any possibility for generating a short code for email confirmation? Do you have an idea what I need to implement?

1 solution

For unique token create GUID

string strToken = Convert.ToBase64String(Guid.NewGuid().ToByteArray());

Or you can use date time time stamp and generate token.
byte[] time = BitConverter.GetBytes(DateTime.UtcNow.ToBinary());
byte[] key = Guid.NewGuid().ToByteArray();
string token = Convert.ToBase64String(time.Concat(key).ToArray());
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900