public void Generator_WithValidLength_ShouldCreateUniqueKeyOnEachCall()
        {
            string random1 = RandomKeyGenerator.GenerateRandomKey(32);
            string random2 = RandomKeyGenerator.GenerateRandomKey(32);

            random1.Should().NotBe(random2, "beucase we expect each call to create unique keys");
        }
        public async Task <IActionResult> CreateAccount(CreateAccountModel model)
        {
            if (ModelState.IsValid)
            {
                UserModel user = new UserModel()
                {
                    UserName             = model.UserName,
                    Email                = model.Email,
                    PhoneNumber          = model.PhoneNumber,
                    EmailConfirmed       = false,
                    EmailVerificationKey = RandomKeyGenerator.RandomString(25),
                };

                var result = await _userManager.CreateAsync(user, model.Password);

                if (result.Succeeded)
                {
                    var send = await _messageService.SendNewVerificationCode(user.Email, user.EmailVerificationKey, user.UserName);

                    if (send)
                    {
                        return(RedirectToAction("NeedVerification", new { userName = user.UserName, email = user.Email }));
                    }
                    return(View(model));
                }
                else
                {
                    foreach (var error in result.Errors)
                    {
                        ModelState.AddModelError("", error.Description);
                    }
                }
            }
            return(View(model));
        }
示例#3
0
        public IActionResult AddProject(AddProjectModel model)
        {
            var blockchain = dbContext.Blockchains.Where(b => b.Id == model.SelectedBlockchainId).SingleOrDefault();

            if (blockchain == null)
            {
                return(BadRequest("Blockchain not found with id: " + model.SelectedBlockchainId + "."));
            }

            var apiKey    = Guid.NewGuid();
            var apiSecret = RandomKeyGenerator.GetUniqueKey(128);

            var project = new Project
            {
                Name       = model.Name,
                UserId     = CurrentUser.Id,
                ApiKey     = apiKey,
                ApiSecret  = apiSecret,
                Blockchain = blockchain,
                CreatedUtc = DateTime.UtcNow
            };

            dbContext.Projects.Add(project);
            dbContext.SaveChanges();

            return(RedirectToAction(nameof(Project), new { projectId = project.Id }));
        }
        public async Task <bool> AddResetRequestAsync(string userId)
        {
            UserModel user = await _userManager.FindByIdAsync(userId);

            if (user != null)
            {
                var expirePeriod = Convert.ToInt32(_configuration["PasswordReset:KeyExpireHours"]);
                var time         = DateTime.Now;

                _dbContext.PasswordResetRequests.Add(new PasswordReset()
                {
                    UserIdentity  = user.Id,
                    EmailAdddress = user.Email,
                    ResetKey      = RandomKeyGenerator.RandomString(25),
                    RequestTime   = time,
                    KeyExpires    = time.AddHours(expirePeriod),
                    Attempts      = 0,
                    WasVerified   = user.EmailConfirmed,
                });

                await _dbContext.SaveChangesAsync();

                return(true);
            }
            return(false);
        }
示例#5
0
 public PasswordRepository(DataContext context, IConfiguration config)
 {
     _context            = context;
     _config             = config;
     _sendEmail          = new SendEmail();
     _randomKeyGenerator = new RandomKeyGenerator();
 }
示例#6
0
        public static object randomString(int size = 12)
        {
            RandomKeyGenerator KeyGen;

            KeyGen            = new RandomKeyGenerator();
            KeyGen.KeyLetters = "abcdefghijklmnopqrstuvwxyz";
            KeyGen.KeyNumbers = "0123456789";
            KeyGen.KeyChars   = Conversions.ToInteger(size);
            return(KeyGen.Generate());
        }
示例#7
0
        public void GenerateRandomKey_ReturnsRandomResults()
        {
            // Arrange
            const int iterations = 100;
            const int byteLength = 32;

            var randomKeyGenerator = new RandomKeyGenerator();

            var results = new HashSet <string>();

            // Act
            for (int i = 0; i < iterations; i++)
            {
                var result = randomKeyGenerator.GenerateRandomKey(byteLength);
                results.Add(result);
            }

            // Assert
            Assert.Equal(iterations, results.Count);
        }
示例#8
0
 public Product()
 {
     this.Id = RandomKeyGenerator.GenerateKey();
 }
        public void Generator_WithNegativeLength_ShouldThrow_ArgumentOutOfRangeException()
        {
            Action invalidAction = () => RandomKeyGenerator.GenerateRandomKey(-1);

            invalidAction.ShouldThrow <ArgumentOutOfRangeException>("because we passed 0 as input");
        }
示例#10
0
 public Repository(DataContext context)
 {
     _context            = context;
     _sendEmail          = new SendEmail();
     _randomKeyGenerator = new RandomKeyGenerator();
 }
示例#11
0
 public Category()
 {
     this.Id       = RandomKeyGenerator.GenerateKey();
     this.Products = new HashSet <Product>();
 }