Esempio n. 1
0
        public async Task <IActionResult> Register(UserFormViewModel model)
        {
            if (User.Identity.IsAuthenticated)
            {
                TempData.AddErrorMessage(WebConstants.AlreadyRegistered);
                return(RedirectToHome());
            }

            if (!ModelState.IsValid || model.ImageFile == null)
            {
                TempData.AddErrorMessage(WebConstants.ErrorTryAgain);
                return(View(model));
            }

            Picture profilePicture      = new Picture();                                                  // Creates instance of Picture entity
            Image   profilePictureImage = PictureServiceHelpers.ConvertIFormFileToImage(model.ImageFile); // Converts the uploaded image to System.Drawing.Image

            if (profilePictureImage == null)                                                              // The uploaded file is not a picture
            {
                TempData.AddErrorMessage(WebConstants.ErrorTryAgain);
                return(View(model));
            }

            bool imageInsertSuccess = await pictureService.InsertAsync(profilePicture, profilePictureImage); // inserts image into database and file system

            if (!imageInsertSuccess)                                                                         // if something with the image goes wrong return error
            {
                TempData.AddErrorMessage(WebConstants.ErrorTryAgain);
                return(View(model));
            }

            User user = new User
            {
                UserName               = AuthenticationHelpers.GenerateUniqueUsername(model.FirstName, model.LastName),
                Email                  = model.Email,
                FirstName              = model.FirstName,
                LastName               = model.LastName,
                PhoneNumber            = model.PhoneNumber,
                ProfilePictureFileName = profilePicture.FileName
            };

            IdentityResult result = await userManager.CreateAsync(user, model.Password);

            if (!result.Succeeded)                                // If something with the insert of a user goes wrong return an error
            {
                await pictureService.DeleteAsync(profilePicture); // In that case delete the already inserted profile picture

                AddErrors(result);
                return(View(model));
            }

            await signInManager.SignInAsync(user, false); // Signs the new user

            TempData.AddSuccessMessage(WebConstants.SuccessfulRegistration);
            return(RedirectToHome());
        }
Esempio n. 2
0
        public async Task <IActionResult> ExternalLoginConfirmation(ExternalLoginViewModel model, string returnUrl = null)
        {
            if (ModelState.IsValid)
            {
                // Get the information about the user from the external login provider
                ExternalLoginInfo info = await signInManager.GetExternalLoginInfoAsync();

                if (info == null)
                {
                    TempData.AddErrorMessage(WebConstants.ErrorTryAgain);
                    return(RedirectToHome());
                }

                Picture profilePicture = new Picture();

                Stream stream             = new MemoryStream(model.ProfilePictureBytes);
                Image  pictureImage       = Image.FromStream(stream);
                bool   imageInsertSuccess = await pictureService.InsertAsync(profilePicture, pictureImage); // inserts image into database and file system

                if (!imageInsertSuccess)                                                                    // if something with the image goes wrong return error
                {
                    TempData.AddErrorMessage(WebConstants.ErrorTryAgain);
                    return(View(nameof(ExternalLogin), model));
                }

                string[] personalNames = AuthenticationHelpers.GetNamesFromExternalLogin(model.Username); // Extracts the first and last name of the person
                User     user          = new User
                {
                    UserName               = AuthenticationHelpers.GenerateUniqueUsername(personalNames[0], personalNames[1]),
                    Email                  = model.Email,
                    FirstName              = personalNames[0],
                    LastName               = personalNames[1],
                    PhoneNumber            = model.PhoneNumber,
                    ProfilePictureFileName = profilePicture.FileName
                };

                IdentityResult result = await userManager.CreateAsync(user);

                if (result.Succeeded)
                {
                    result = await userManager.AddLoginAsync(user, info);

                    if (result.Succeeded)
                    {
                        await signInManager.SignInAsync(user, isPersistent : false);

                        return(RedirectToLocal(returnUrl));
                    }
                }

                AddErrors(result);
            }

            ViewData["ReturnUrl"] = returnUrl;
            return(View(nameof(ExternalLogin), model));
        }