예제 #1
0
        public async Task <IEnumerable <string> > GetUserRolesAsync(string userId)
        {
            RadoHubUser user  = this.GetUserById(userId);
            var         users = await this.UserManager.GetRolesAsync(user);

            return(users);
        }
예제 #2
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 RadoHubUser {
                    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);

                        var userId = await _userManager.GetUserIdAsync(user);

                        var code = await _userManager.GenerateEmailConfirmationTokenAsync(user);

                        code = WebEncoders.Base64UrlEncode(Encoding.UTF8.GetBytes(code));
                        var callbackUrl = Url.Page(
                            "/Account/ConfirmEmail",
                            pageHandler: null,
                            values: new { area = "Identity", userId = userId, 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>.");

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

            LoginProvider = info.LoginProvider;
            ReturnUrl     = returnUrl;
            return(Page());
        }
예제 #3
0
        public IActionResult UserDetails(string id)
        {
            RadoHubUser user = this.userAccountService.GetUserById(id);

            UserAccountAdminViewModel viewModel = mapper.Map <UserAccountAdminViewModel>(user);

            viewModel.UserRole = this.userAccountService.GetUserStrongestRole(user.Id);

            return(this.View(viewModel));
        }
예제 #4
0
        private async Task LoadAsync(RadoHubUser user)
        {
            var email = await _userManager.GetEmailAsync(user);

            Email = email;

            Input = new InputModel
            {
                NewEmail = email,
            };

            IsEmailConfirmed = await _userManager.IsEmailConfirmedAsync(user);
        }
예제 #5
0
        public IActionResult DeleteUser(string id)
        {
            RadoHubUser user = this.userAccountService.GetUserById(id);
            var         operationSucceeded = this.userAccountService.DeleteUserAsync(user).GetAwaiter().GetResult().Succeeded;

            if (operationSucceeded)
            {
                TempData["statusMessage"] = $"User \"{user.UserName}\" was deleted successfully!";
                return(RedirectToAction("Index", "UserAccount"));
            }
            else
            {
                TempData["statusMessage"] = $"Action Failed! | Operation succeeded";
                return(RedirectToAction("Index", "UserAccount"));
            }
        }
예제 #6
0
        public IActionResult RemoveUserFromAdminRole(string id)
        {
            RadoHubUser user = this.userAccountService.GetUserById(id);
            var         operationSucceeded = this.userAccountService.RemoveUserFromRoleAsync(user, UserRoles.AdminRole).GetAwaiter().GetResult().Succeeded;

            if (operationSucceeded)
            {
                TempData["statusMessage"] = $"User \"{user.UserName}\" was removed from Admin role successfully!";
                return(RedirectToAction("Index", "UserAccount"));
            }
            else
            {
                TempData["statusMessage"] = $"Action Failed! | Operation succeeded";
                return(RedirectToAction("Index", "UserAccount"));
            }
        }
        private async Task LoadSharedKeyAndQrCodeUriAsync(RadoHubUser 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);
        }
예제 #8
0
        private async Task LoadAsync(RadoHubUser user)
        {
            var userName = await _userManager.GetUserNameAsync(user);

            var phoneNumber = await _userManager.GetPhoneNumberAsync(user);

            var firstName = user.FirstName;
            var lastName  = user.LastName;
            var city      = user.City;
            var company   = user.Company;

            Username = userName;

            Input = new InputModel
            {
                FirstName   = firstName,
                LastName    = lastName,
                PhoneNumber = phoneNumber,
                City        = city,
                Company     = company,
            };
        }
예제 #9
0
        private static async Task SeedUserInRoles(UserManager <RadoHubUser> userManager)
        {
            if (!userManager.Users.Any())
            {
                var user = new RadoHubUser
                {
                    UserName  = "******",
                    Email     = "*****@*****.**",
                    FirstName = "AdminFirstName",
                    LastName  = "AdminLastName",
                };

                var password = "******";

                var userCreated = await userManager.CreateAsync(user, password);

                if (userCreated.Succeeded)
                {
                    await userManager.AddToRoleAsync(user, UserRoles.AdminRole);

                    await userManager.AddToRoleAsync(user, UserRoles.DefaultUserRole);
                }
            }
        }
예제 #10
0
 public async Task <IdentityResult> RemoveUserFromRoleAsync(RadoHubUser user, string role)
 {
     return(await this.UserManager.RemoveFromRoleAsync(user, role));
 }
예제 #11
0
 public async Task <IdentityResult> AddUserInRoleAsync(RadoHubUser user, string role)
 {
     return(await this.UserManager.AddToRoleAsync(user, role));
 }
예제 #12
0
        public async Task <IdentityResult> DeleteUserAsync(RadoHubUser user)
        {
            await this.RemoveUserFromRoleAsync(user, UserRoles.AdminRole);

            return(await this.UserManager.DeleteAsync(user));
        }
예제 #13
0
        public async Task <IActionResult> OnPostAsync(string returnUrl = null)
        {
            returnUrl      = returnUrl ?? Url.Content("~/");
            ExternalLogins = (await _signInManager.GetExternalAuthenticationSchemesAsync()).ToList();
            if (ModelState.IsValid)
            {
                var user = new RadoHubUser
                {
                    UserName    = Input.UserName,
                    Email       = Input.Email,
                    FirstName   = Input.FirstName,
                    LastName    = Input.LastName,
                    City        = Input.City,
                    PhoneNumber = Input.PhoneNumber,
                    Company     = Input.Company,
                };
                var result = await _userManager.CreateAsync(user, Input.Password);

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

                    var addUserToRole = await _userManager
                                        .AddToRoleAsync(user, UserRoles.DefaultUserRole);

                    if (addUserToRole.Succeeded)
                    {
                        _logger.LogInformation("User added to default user role.");
                    }
                    else
                    {
                        _logger.LogInformation("User adding to default user role failed.");
                    }

                    var code = await _userManager.GenerateEmailConfirmationTokenAsync(user);

                    code = WebEncoders.Base64UrlEncode(Encoding.UTF8.GetBytes(code));
                    var callbackUrl = Url.Page(
                        "/Account/ConfirmEmail",
                        pageHandler: null,
                        values: new { area = "Identity", 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>.");

                    if (_userManager.Options.SignIn.RequireConfirmedAccount)
                    {
                        return(RedirectToPage("RegisterConfirmation", new { email = Input.Email }));
                    }
                    else
                    {
                        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());
        }