コード例 #1
0
        public async Task <IActionResult> Index(IndexViewModel model)
        {
            if (!ModelState.IsValid)
            {
                return(View(model));
            }

            var user = await _userManager.GetUserAsync(User);

            if (user == null)
            {
                throw new ApplicationException($"Benuter '{_userManager.GetUserId(User)}' konnte nicht gefunden werden.");
            }

            var email = user.Email;

            if (model.Email != email)
            {
                var setEmailResult = await _userManager.SetEmailAsync(user, model.Email);

                if (!setEmailResult.Succeeded)
                {
                    throw new ApplicationException($"Beim Einstellen der E-Mail-Adresse für einen Benutzer mit ID '{user.Id}' ist ein unerwarteter Fehler aufgetreten.");
                }
            }

            var phoneNumber = user.PhoneNumber;

            if (model.PhoneNumber != phoneNumber)
            {
                var setPhoneResult = await _userManager.SetPhoneNumberAsync(user, model.PhoneNumber);

                if (!setPhoneResult.Succeeded)
                {
                    throw new ApplicationException($"Unexpected error occurred setting phone number for user with ID '{user.Id}'.");
                }
            }

            Customer customer = await _context.Customers.SingleAsync(c => c.UserId.Equals(user.Id));

            if (customer.CustomerID.Equals(model.CustomerID))
            {
                customer.FirstName         = model.FirstName;
                customer.Name              = model.Name;
                customer.CompanyName       = model.CompanyName;
                customer.Address           = model.Address;
                customer.AdditionalAddress = model.AdditionalAddress;
                customer.City              = model.City;
                customer.CountryId         = model.CountryID;
                customer.PostCode          = model.PostCode;

                bool countryIsAllowedForSipping = await countryHelper.GetAllowedForShipping(model.CountryID);

                var shipTo = await _context.ShippingAddresses.SingleOrDefaultAsync(s => s.IsInvoiceAddress && s.CustomerID == model.CustomerID);

                if (countryIsAllowedForSipping && shipTo != null)
                {
                    shipTo.CompanyName       = model.CompanyName;
                    shipTo.AdditionalAddress = model.AdditionalAddress;
                    shipTo.Address           = model.Address;
                    shipTo.City      = model.City;
                    shipTo.PostCode  = model.PostCode;
                    shipTo.LastName  = model.Name;
                    shipTo.FirstName = model.FirstName;

                    _context.Entry(shipTo).State = EntityState.Modified;
                }
                if (countryIsAllowedForSipping && shipTo == null)
                {
                    shipTo = new ShippingAddress {
                        CountryID = model.CountryID, AdditionalAddress = model.AdditionalAddress, Address = model.Address, City = model.City, CompanyName = model.CompanyName, CustomerID = customer.CustomerID, FirstName = model.FirstName, IsInvoiceAddress = true, LastName = model.Name, PostCode = model.PostCode
                    };
                    _context.ShippingAddresses.Add(shipTo);
                }


                _context.Entry(customer).State = EntityState.Modified;

                await _context.SaveChangesAsync();
            }

            StatusMessage = "Dein Konto wurde aktualisiert!";
            return(RedirectToAction(nameof(Index)));
        }
コード例 #2
0
        public async Task <IActionResult> Register(RegisterViewModel model, string returnUrl = null)
        {
            ViewData["ReturnUrl"] = returnUrl;

            if (ModelState.IsValid)
            {
                var user = new ApplicationUser {
                    UserName = model.Email, Email = model.Email
                };
                string UserID = user.Id;

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

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

                    var cartId = HttpContext.Session.GetString("ShoppingCartId");
                    var code   = await _userManager.GenerateEmailConfirmationTokenAsync(user);

                    var callbackUrl = Url.EmailConfirmationLink(user.Id, code, cartId, Request.Scheme);

                    var customers = await _context.Customers.ToListAsync();

                    bool countryIsAllowedForSipping = await countryHelper.GetAllowedForShipping(model.CountryID);

                    Customer customer = new Customer {
                        CustomerID = Guid.NewGuid(), AdditionalAddress = model.AdditionalAddress, Address = model.Address, City = model.City, CustomerNo = $"{DateTime.Now.Year}{DateTime.Now.Month}{DateTime.Now.Day}C{customers.Count + 1}", FirstName = model.FirstName, Name = model.Name, PostCode = model.PostCode, UserId = user.Id, CountryId = model.CountryID, CompanyName = model.CompanyName
                    };
                    _context.Add(customer);

                    if (countryIsAllowedForSipping)
                    {
                        var shipToAddress = new ShippingAddress
                        {
                            AdditionalAddress = model.AdditionalAddress,
                            Address           = model.Address,
                            City             = model.City,
                            CompanyName      = model.CompanyName,
                            CountryID        = model.CountryID,
                            CustomerID       = customer.CustomerID,
                            FirstName        = model.FirstName,
                            IsInvoiceAddress = true,
                            IsMainAddress    = true,
                            LastName         = model.Name,
                            PostCode         = model.PostCode
                        };
                        _context.ShippingAddresses.Add(shipToAddress);
                    }
                    await _context.SaveChangesAsync();

                    AddUserCustomerToShoppingCart(user.Id, HttpContext.Session.GetString("ShoppingCartId"));

                    _logger.LogInformation("User created a new account with password.");

                    string subject = "Registrierung abschließen";
                    string message = $"<h2>Herzlich Willkommen bei marelibuDesign!</h2>" +
                                     $"<p>Bitte bestätigen Sie Ihre Registrierung durch Klicken dieses Links: <a href='{HtmlEncoder.Default.Encode(callbackUrl)}'>Registrierung abschließen.</a><br />Danach können Sie Sich in Ihr Kundenkonto einloggen.</p><p>Sie haben Sich nicht auf www.marelibuDesign.de angemeldet? Dann klicken Sie den Link bitte nicht, die eingegebenen Daten werden nach Ablauf von 7 Tagen automatisch gelöscht.</p><br /> ";

                    await _emailSender.SendEmailAsync(user.Email, subject, message);

                    return(RedirectToAction("PleaseConfirmEmail", "Home"));
                }
                AddErrors(result);
            }

            // If we got this far, something failed, redisplay form
            ViewData["ReturnUrl"]      = returnUrl;
            ViewData["CountryID"]      = new SelectList(countryHelper.GetVmList(), "ID", "Name");
            ViewData["ShoppingCartId"] = HttpContext.Session.GetString("ShoppingCartId");
            return(View(model));
        }