Example #1
0
        protected override void Seed(RoboBraille.WebApi.Models.RoboBrailleDataContext context)
        {
            //This method will be called after migrating to the latest version.

            //You can use the DbSet<T>.AddOrUpdate() helper extension method
            //to avoid creating duplicate seed data. E.g.

            Guid   apiGuid = Guid.NewGuid();
            String apiKey  = apiGuid.ToString();

            byte[] apiKeyByteArray = Encoding.UTF8.GetBytes(apiKey);
            //when creating a new user save the uid and apiKey strings to pass to the user so that he may access the db
            //context.ServiceUsers.AddOrUpdate(new ServiceUser { EmailAddress = "*****@*****.**", UserId = Guid.NewGuid(), ApiKey = Encoding.UTF8.GetBytes("7b76ae41-def3-e411-8030-0c8bfd2336cd"), FromDate = new DateTime(2015, 1, 1), ToDate = new DateTime(2020, 1, 1), UserName = "******", Jobs = null });
            context.ServiceUsers.AddOrUpdate(new ServiceUser {
                EmailAddress = "*****@*****.**", Password = Encrypt.CreateHash("Fishb0ne"), UserId = Guid.NewGuid(), ApiKey = apiKeyByteArray, FromDate = DateTime.Now, ToDate = DateTime.Now.AddYears(100), UserName = "******", IsApproved = true, UserRole = UserRole.Administrator, Jobs = null
            });
            context.ServiceUsers.AddOrUpdate(new ServiceUser {
                EmailAddress = "*****@*****.**", Password = Encrypt.CreateHash("Fishb0ne"), UserId = Guid.NewGuid(), ApiKey = Encoding.UTF8.GetBytes(Guid.NewGuid().ToString()), FromDate = DateTime.Now, ToDate = DateTime.Now.AddYears(100), UserName = "******", IsApproved = true, UserRole = UserRole.User, Jobs = null
            });
        }
Example #2
0
        public async Task <ActionResult> Create([Bind(Exclude = "UserId,ApiKey")] ServiceUser serviceUser)
        {
            ServiceUser user = await _context.ServiceUsers.SingleOrDefaultAsync(u => u.EmailAddress.Equals(serviceUser.EmailAddress.Trim(), StringComparison.InvariantCultureIgnoreCase));

            if (user != null)
            {
                ModelState.AddModelError("EmailAddress", "Email already exists! Please use another email address.");
            }
            serviceUser.UserId = Guid.NewGuid();
            serviceUser.ApiKey = Encoding.UTF8.GetBytes(Guid.NewGuid().ToString());
            TryUpdateModel(serviceUser);
            if (ModelState.IsValid || serviceUser.ApiKey != null)
            {
                serviceUser.Password = Encrypt.CreateHash(serviceUser.Password);
                _context.ServiceUsers.Add(serviceUser);
                await _context.SaveChangesAsync();

                return(RedirectToAction("Details", new { id = serviceUser.UserId }));
            }

            return(View(serviceUser));
        }