Example #1
0
        public async static Task Initialize(IServiceProvider serviceProvider) {
            var context = serviceProvider.GetService<ApplicationDbContext>();
            var userManager = serviceProvider.GetService<UserManager<ApplicationUser>>();

            // Ensure db
            context.Database.EnsureCreated();

            // Ensure Stephen (IsAdmin)
            var stephen = await userManager.FindByNameAsync("*****@*****.**");
            if (stephen == null) {
                // create user
                stephen = new ApplicationUser {
                    UserName = "******",
                    Email = "*****@*****.**"
                };
                await userManager.CreateAsync(stephen, "Secret123!");

                // add claims
                await userManager.AddClaimAsync(stephen, new Claim("IsAdmin", "true"));
            }

            // Ensure Mike (not IsAdmin)
            var mike = await userManager.FindByNameAsync("*****@*****.**");
            if (mike == null) {
                // create user
                mike = new ApplicationUser {
                    UserName = "******",
                    Email = "*****@*****.**"
                };
                await userManager.CreateAsync(mike, "Secret123!");
            }

            if (!context.ComicBooks.Any()) {

                context.ComicBooks.AddRange(
                    new ComicBook() {
                        Title = "The Invincible Iron Man",
                        IssueNumber = 47,
                        CoverImageUrl = "http://www.greatgiftsandtoys.com/images/york/detail/RMK1662SLG.jpg",
                        Genre = "Action",
                        Price = 0.20m,
                        ReleaseDate = new DateTime(1970, 6, 1),
                        User = mike
                    },
                    new ComicBook() {
                        Title = "The Inventor",
                        IssueNumber = 1,
                        CoverImageUrl = "https://www.wired.com/images_blogs/underwire/2012/07/the-inventor-cover-sdcc-exclusive.jpg",
                        Genre = "Biography",
                        Price = 20m,
                        ReleaseDate = DateTime.Now,
                        User = stephen
                    }
                );
                context.SaveChanges();
            }
        }
Example #2
0
        public void AddComic(ComicBookDTO dto, string currentUser)
        {
            var user = new ApplicationUser(); // THIS IS JUST MOCK CODE. GET THE USER FROM THE DB

            var dbItem = new ComicBook() {

                Title = dto.Title,
                IssueNumber = dto.IssueNumber,
                CoverImageUrl = dto.CoverImageUrl,
                Genre = dto.Genre,
                Price = dto.Price,
                ReleaseDate = dto.ReleaseDate,
                User = user
            };

            _comicRepo.Add(dbItem);
        }
Example #3
0
        public async Task<IActionResult> Register([FromBody]RegisterViewModel model)
        {
            if (ModelState.IsValid)
            {
                var user = new ApplicationUser { UserName = model.Email, Email = model.Email };
                var result = await _userManager.CreateAsync(user, model.Password);
                if (result.Succeeded)
                {
                    // For more information on how to enable account confirmation and password reset please visit http://go.microsoft.com/fwlink/?LinkID=532713
                    // Send an email with this link
                    //var code = await _userManager.GenerateEmailConfirmationTokenAsync(user);
                    //var callbackUrl = Url.Action("ConfirmEmail", "Account", new { userId = user.Id, code = code }, protocol: HttpContext.Request.Scheme);
                    //await _emailSender.SendEmailAsync(model.Email, "Confirm your account",
                    //    "Please confirm your account by clicking this link: <a href=\"" + callbackUrl + "\">link</a>");
                    await _signInManager.SignInAsync(user, isPersistent: false);
                    _logger.LogInformation(3, "User created a new account with password.");
                    var userViewModel = await GetUser(user.UserName);
                    return Ok(userViewModel);
                }
                AddErrors(result);
            }

            // If we got this far, something failed
            return BadRequest(this.ModelState);
        }
Example #4
0
        public async Task<IActionResult> ExternalLoginConfirmation([FromBody]ExternalLoginConfirmationViewModel model, string returnUrl = null)
        {


            if (ModelState.IsValid)
            {
                // Get the information about the user from the external login provider
                var info = await _signInManager.GetExternalLoginInfoAsync();
                if (info == null)
                {
                    ModelState.AddModelError("", "ExternalLoginFailure");
                    return BadRequest(this.ModelState);
                    //return View("ExternalLoginFailure");
                }
                var user = new ApplicationUser { UserName = model.Email, Email = model.Email };
                var result = await _userManager.CreateAsync(user);
                if (result.Succeeded)
                {
                    result = await _userManager.AddLoginAsync(user, info);
                    if (result.Succeeded)
                    {
                        await _signInManager.SignInAsync(user, isPersistent: false);
                        _logger.LogInformation(6, "User created an account using {Name} provider.", info.LoginProvider);
                        var userViewModel = await GetUser(user.UserName);
                        return Ok(userViewModel);
                    }
                }
                AddErrors(result);
            }

           // ViewData["ReturnUrl"] = returnUrl;
            return BadRequest(this.ModelState);
        }