コード例 #1
0
        /// <summary>
        /// Inicializace defaultnich hodnot pro nase entity
        /// </summary>
        /// <param name="context"></param>
        private void InitializeDefaults(DataDbContext context)
        {
            context.EGenders.AddOrUpdate(new Models.Enums.EGender()
            {
                Id = 1, Name = "Muž"
            });
            context.EGenders.AddOrUpdate(new Models.Enums.EGender()
            {
                Id = 2, Name = "Žena"
            });



            var organizer = new Models.Organizer()
            {
                Id   = 1,
                Name = "Pořadatel 1",
            };

            context.Organizers.AddOrUpdate(organizer);


            var user = CreateUserIfNotExists(context, "poradatel", "*****@*****.**", "Jaromír", "John",
                                             "test123", new[] { "Organizer" }, new DateTime(1991, 04, 26), 1, organizer.Id);
        }
コード例 #2
0
        public async Task <IActionResult> OnPostAsync(string returnUrl = null)
        {
            returnUrl = returnUrl ?? Url.Content("~/");
            if (ModelState.IsValid)
            {
                var user = new IdentityUser {
                    UserName = Input.Email, Email = Input.Email
                };
                var result = await _userManager.CreateAsync(user, Input.Password);

                if (result.Succeeded)
                {
                    _logger.LogInformation("User created a new account with password.");

                    Address address = new Address(Input.City, Input.Street, Input.BuildingNumber, Input.ApartmentNumber, Input.PostalCode);

                    AppUser appUser = null;
                    if (!string.IsNullOrEmpty(Input.PESEL))
                    {
                        appUser = new Models.Volunteer
                                  (
                            user.Id,
                            Input.FirstName,
                            Input.LastName,
                            Input.Email,
                            Input.PhoneNumber,
                            address,
                            Input.PESEL
                                  );
                        await _userManager.AddToRoleAsync(user, Roles.VolunteerRole);
                    }
                    else
                    {
                        appUser = new Models.Organizer
                                  (
                            user.Id,
                            Input.FirstName,
                            Input.LastName,
                            Input.Email,
                            Input.PhoneNumber,
                            address
                                  );
                        await _userManager.AddToRoleAsync(user, Roles.OrganizerRole);
                    }

                    _db.AppUsers.Add(appUser);
                    _db.SaveChanges();

                    var code = await _userManager.GenerateEmailConfirmationTokenAsync(user);

                    var callbackUrl = Url.Page(
                        "/Account/ConfirmEmail",
                        pageHandler: null,
                        values: new { userId = user.Id, code = code },
                        protocol: Request.Scheme);

                    await _emailSender.SendEmailAsync(Input.Email, "Confirm your email",
                                                      $"Please confirm your account by <a href='{HtmlEncoder.Default.Encode(callbackUrl)}'>clicking here</a>.");

                    await _signInManager.SignInAsync(user, isPersistent : false);

                    return(LocalRedirect(returnUrl));
                }
                foreach (var error in result.Errors)
                {
                    ModelState.AddModelError(string.Empty, error.Description);
                }
            }

            // If we got this far, something failed, redisplay form
            return(Page());
        }