示例#1
0
        public Task <IEnumerable <TViewModel> > GetCommentsForCurrentUser <TViewModel>(
            UnprofessionalsAppUser currentUser)
        {
            var commentTask = Task.Run(() =>
            {
                var source = this.commentsRepository.All()
                             .Where(c => !c.IsDeleted && c.UserId == currentUser.Id);

                var destination = this.mapper.ProjectTo <TViewModel>(source);

                var result = destination as IEnumerable <TViewModel>;

                return(result);
            });

            return(commentTask);
        }
示例#2
0
        private async Task LoadSharedKeyAndQrCodeUriAsync(UnprofessionalsAppUser user)
        {
            // Load the authenticator key & QR code URI to display on the form
            var unformattedKey = await _userManager.GetAuthenticatorKeyAsync(user);

            if (string.IsNullOrEmpty(unformattedKey))
            {
                await _userManager.ResetAuthenticatorKeyAsync(user);

                unformattedKey = await _userManager.GetAuthenticatorKeyAsync(user);
            }

            SharedKey = FormatKey(unformattedKey);

            var email = await _userManager.GetEmailAsync(user);

            AuthenticatorUri = GenerateQrCodeUri(email, unformattedKey);
        }
示例#3
0
        public Task <IEnumerable <TViewModel> > GetPostsByUsernameAsync <TViewModel>(
            UnprofessionalsAppUser currentUser)
        {
            var postTask = Task.Run(() =>
            {
                var source = this.postsRepository.All()
                             .Where(p => !p.IsDeleted && p.UserId == currentUser.Id);
                //.To<TViewModel>()
                //.FirstOrDefault()

                var destination = this.mapper.ProjectTo <TViewModel>(source);

                var result = destination as IEnumerable <TViewModel>;

                return(result);
            });

            return(postTask);
        }
示例#4
0
        public async Task <IActionResult> OnPostConfirmationAsync(string returnUrl = null)
        {
            returnUrl = returnUrl ?? Url.Content("~/");
            // Get the information about the user from the external login provider
            var info = await _signInManager.GetExternalLoginInfoAsync();

            if (info == null)
            {
                ErrorMessage = "Error loading external login information during confirmation.";
                return(RedirectToPage("./Login", new { ReturnUrl = returnUrl }));
            }

            if (ModelState.IsValid)
            {
                var user = new UnprofessionalsAppUser {
                    UserName = Input.Email, Email = Input.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("User created an account using {Name} provider.", info.LoginProvider);
                        return(LocalRedirect(returnUrl));
                    }
                }
                foreach (var error in result.Errors)
                {
                    ModelState.AddModelError(string.Empty, error.Description);
                }
            }

            LoginProvider = info.LoginProvider;
            ReturnUrl     = returnUrl;
            return(Page());
        }
        public async Task <IActionResult> OnPostAsync(string returnUrl = null)
        {
            returnUrl = returnUrl ?? Url.Content("~/");

            if (this.User.Identity.IsAuthenticated)
            {
                _logger.LogInformation("You are already logged in.");
                return(LocalRedirect(returnUrl));
            }

            if (ModelState.IsValid)
            {
                var user = new UnprofessionalsAppUser {
                    UserName = Input.Username, Email = Input.Email
                };

                if (Input.ImageFile != null)
                {
                    var filePath = await this.filesService.ReadFile(Input.ImageFile);

                    var uploadResult =
                        await this.imageService.UploadImageFromFilePath(filePath);

                    var imageUrl = this.imageService.GetUrlPath(uploadResult);

                    var image = await this.imageService.CreateImage(imageUrl);

                    user.Image = image;
                }
                else
                {
                    user.ImageId = GlobalConstants.DefaultUserImageId;
                }


                var result = await this.userManager.CreateAsync(user, Input.Password);

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


                    //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 this.userManager.AddToRoleAsync(user, GlobalConstants.UserRole);

                    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());
        }
        private static async Task CreatePowerUser(IServiceProvider serviceProvider, UnprofessionalsDbContext context)
        {
            if (context.Users.Any())
            {
                return;
            }

            var userManager = serviceProvider.GetRequiredService <UserManager <UnprofessionalsAppUser> >();
            //Here you could create a super user who will maintain the web app
            var poweruser = new UnprofessionalsAppUser
            {
                UserName    = /*Configuration["AppSettings:UserName"]*/ "admin",
                Email       = /*Configuration["AppSettings:UserEmail"]*/ "*****@*****.**",
                Description = "bai ivan",
                ImageId     = 6
            };
            //Ensure you have these values in your appsettings.json file
            string userPWD = /*Configuration["AppSettings:UserPassword"]*/ "asd123";
            var    _user   = await userManager.FindByNameAsync("admin");

            if (_user == null)
            {
                var createPowerUser = await userManager.CreateAsync(poweruser, userPWD);

                if (createPowerUser.Succeeded)
                {
                    //here we tie the new user to the role
                    await userManager.AddToRoleAsync(poweruser, GlobalConstants.AdminRole);
                }
            }

            var user = new UnprofessionalsAppUser
            {
                UserName    = "******",
                Email       = "*****@*****.**",
                Description = "tuk i tam",
                ImageId     = 6
            };

            var user1 = new UnprofessionalsAppUser
            {
                UserName    = "******",
                Email       = "*****@*****.**",
                Description = "tuk i tam",
                ImageId     = 6
            };
            var user2 = new UnprofessionalsAppUser
            {
                UserName    = "******",
                Email       = "*****@*****.**",
                Description = "tuk i tam",
                ImageId     = 6
            };
            var user3 = new UnprofessionalsAppUser
            {
                UserName    = "******",
                Email       = "*****@*****.**",
                Description = "tuk i tam",
                ImageId     = 6
            };
            var user4 = new UnprofessionalsAppUser
            {
                UserName    = "******",
                Email       = "*****@*****.**",
                Description = "tuk i tam",
                ImageId     = 6
            };

            var createUser = await userManager.CreateAsync(user, userPWD);

            if (createUser.Succeeded)
            {
                //here we tie the new user to the role
                await userManager.AddToRoleAsync(user, GlobalConstants.UserRole);
            }

            var createUser1 = await userManager.CreateAsync(user1, userPWD);

            if (createUser.Succeeded)
            {
                //here we tie the new user to the role
                await userManager.AddToRoleAsync(user1, GlobalConstants.UserRole);
            }

            var createUser2 = await userManager.CreateAsync(user2, userPWD);

            if (createUser.Succeeded)
            {
                //here we tie the new user to the role
                await userManager.AddToRoleAsync(user2, GlobalConstants.UserRole);
            }

            var createUser3 = await userManager.CreateAsync(user3, userPWD);

            if (createUser.Succeeded)
            {
                //here we tie the new user to the role
                await userManager.AddToRoleAsync(user3, GlobalConstants.UserRole);
            }

            var createUser4 = await userManager.CreateAsync(user4, userPWD);

            if (createUser.Succeeded)
            {
                //here we tie the new user to the role
                await userManager.AddToRoleAsync(user4, GlobalConstants.UserRole);
            }
        }