Ejemplo n.º 1
0
        public async Task<ActionResult> ExternalLoginConfirmation(ExternalLoginConfirmationViewModel model, string returnUrl)
        {
            if (this.User.Identity.IsAuthenticated)
            {
                return this.RedirectToAction("Index", "Manage");
            }

            if (this.ModelState.IsValid)
            {
                // Get the information about the user from the external login provider
                var info = await this.AuthenticationManager.GetExternalLoginInfoAsync();
                if (info == null)
                {
                    return this.View("ExternalLoginFailure");
                }

                var user = new User { UserName = model.Email, Email = model.Email };
                var result = await this.UserManager.CreateAsync(user);
                if (result.Succeeded)
                {
                    result = await this.UserManager.AddLoginAsync(user.Id, info.Login);
                    if (result.Succeeded)
                    {
                        await this.SignInManager.SignInAsync(user, isPersistent: false, rememberBrowser: false);
                        return this.RedirectToLocal(returnUrl);
                    }
                }

                this.AddErrors(result);
            }

            this.ViewBag.ReturnUrl = returnUrl;
            return this.View(model);
        }
Ejemplo n.º 2
0
        public async Task<ActionResult> Register(RegisterViewModel model)
        {
            if (this.ModelState.IsValid)
            {
                // Save avatar
                var uploadsFolderPath = this.Server.MapPath(WebApplicationConstants.ImageFolder);
                var folderToBeuploadFiles = Path.Combine(uploadsFolderPath, model.Username);

                var originalFileName = Path.GetFileName(model.Avatar.FileName);
                var sizeInBytes = model.Avatar.ContentLength;
                var contentType = model.Avatar.ContentType;
                var extension = Path.GetExtension(originalFileName);
                var fileName = Guid.NewGuid().ToString();
                var fileNameWithExtension = fileName + extension;
                var filePath = Path.Combine(folderToBeuploadFiles, fileNameWithExtension);

                if (sizeInBytes < WebApplicationConstants.ImageMaxSizeInBytes && (extension == ".jpg" || extension == ".png"))
                {
                    if (!Directory.Exists(folderToBeuploadFiles))
                    {
                        Directory.CreateDirectory(folderToBeuploadFiles);
                    }

                    model.Avatar.SaveAs(filePath);
                    this.imageProccessorServices.ResizeAndSaveImage(
                        model.Avatar,
                        new int[] { WebApplicationConstants.ImageUserAvatarSmallWidth, WebApplicationConstants.ImageUserAvatarNormalWidth },
                        filePath,
                        extension);

                    var avatar = new Photo()
                    {
                        ContentType = contentType,
                        Extension = extension,
                        OriginalName = originalFileName,
                        SizeInBytes = sizeInBytes,
                        CreatedOn = DateTime.Now,
                        FileName = model.Username + '/' + fileName
                    };

                    var user = new User
                    {
                        UserName = model.Username,
                        Email = model.Email,
                        FirstName = model.FirstName,
                        LastName =
                        model.LastName,
                        Avatar = avatar,
                        Description = model.Description
                    };

                    if (model.Role == RoleConstants.DriverRole)
                    {
                        var car = new Car()
                        {
                            OwnerId = user.Id,
                            Brand = model.CarBrand,
                            Model = model.CarModel,
                            TotalSeats = model.CarTotalSeats,
                            Color = model.CarColor,
                            Year = model.CarYear,
                            SpaceForLugage = model.CarSpaceForLugage,
                            Description = model.CarDescription,
                            CreatedOn = DateTime.Now,
                            IsDeleted = false
                        };

                        user.Car = car;

                        // Save car photos
                        foreach (var carPhoto in model.CarPhotos)
                        {
                            var carOriginalFileName = Path.GetFileName(carPhoto.FileName);
                            var carSizeInBytes = carPhoto.ContentLength;
                            var carContentType = carPhoto.ContentType;
                            var carExtension = Path.GetExtension(carOriginalFileName);
                            var carFileName = Guid.NewGuid().ToString();
                            var carFileNameWithExtension = carFileName + carExtension;
                            var carFilePath = Path.Combine(folderToBeuploadFiles, carFileNameWithExtension);

                            if (carPhoto != null && carSizeInBytes < WebApplicationConstants.ImageMaxSizeInBytes && (extension == ".jpg" || extension == ".png"))
                            {
                                carPhoto.SaveAs(carFilePath);
                                this.imageProccessorServices.ResizeAndSaveImage(
                                    carPhoto,
                                    new int[] { WebApplicationConstants.ImageCarWidth },
                                    carFilePath,
                                    carExtension);

                                var carPhotoEntity = new Photo()
                                {
                                    ContentType = carContentType,
                                    Extension = carExtension,
                                    OriginalName = carOriginalFileName,
                                    SizeInBytes = carSizeInBytes,
                                    CreatedOn = DateTime.Now,
                                    FileName = model.Username + '/' + carFileName
                                };

                                car.Photos.Add(carPhotoEntity);
                            }
                            else
                            {
                                this.ModelState.AddModelError("CarPhotos", "Car images should be less than 1.5MB and in jpg or png format.");
                            }
                        }
                    }

                    var result = await this.UserManager.CreateAsync(user, model.Password);
                    if (result.Succeeded)
                    {
                        this.UserManager.AddToRole(user.Id, model.Role);
                        await this.SignInManager.SignInAsync(user, isPersistent: false, rememberBrowser: false);

                        // For more information on how to enable account confirmation and password reset please visit http://go.microsoft.com/fwlink/?LinkID=320771
                        // Send an email with this link
                        // string code = await UserManager.GenerateEmailConfirmationTokenAsync(user.Id);
                        // var callbackUrl = Url.Action("ConfirmEmail", "Account", new { userId = user.Id, code = code }, protocol: Request.Url.Scheme);
                        // await UserManager.SendEmailAsync(user.Id, "Confirm your account", "Please confirm your account by clicking <a href=\"" + callbackUrl + "\">here</a>");
                        return this.RedirectToAction("Index", "Home");
                    }

                    this.AddErrors(result);
                }
                else
                {
                    this.ModelState.AddModelError("Avatar", "Avatar image should be less than 1.5MB and in jpg or png format.");
                }
            }

            this.FillRequiredRegistrationSelectList(model);

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