Beispiel #1
0
            public async Task <UserDto> Handle(Query request, CancellationToken cancellationToken)
            {
                var user = await userManager.FindByEmailAsync(request.Email);

                if (user == null)
                {
                    throw new RestException(HttpStatusCode.NotFound, new { error = "User not found. Check email address and try again." });
                }
                if (!user.EmailConfirmed)
                {
                    throw new RestException(HttpStatusCode.BadRequest, new { email = "Email is not confirmed." });
                }

                var result = await signInManager.CheckPasswordSignInAsync(user, request.Password, false);

                if (result.Succeeded)
                {
                    var role = await roleAccessor.GetUserRoleAsync(user);

                    var refreshToken = await userManager.GenerateUserTokenAsync(user, TokenOptions.DefaultProvider, "RefreshToken");

                    return(new UserDto(user, role, tokenGenerator, refreshToken));
                }

                throw new RestException(HttpStatusCode.Unauthorized, new { error = "Email or password is invalid." });
            }
Beispiel #2
0
            public async Task <UserDto> Handle(Query request, CancellationToken cancellationToken)
            {
                var user = await userManager.FindByIdAsync(userAccessor.GetCurrentUserId());

                if (user == null)
                {
                    return(null);
                }

                var role = await roleAccessor.GetUserRoleAsync(user);

                var refreshToken = await userManager.GenerateUserTokenAsync(user, TokenOptions.DefaultProvider, "RefreshToken");

                return(new UserDto(user, role, tokenGenerator, refreshToken));
            }
            public async Task <UserDto> Handle(Command request, CancellationToken cancellationToken)
            {
                var user = await userManager.FindByIdAsync(userAccessor.GetCurrentUserId());

                if (user == null)
                {
                    return(null);
                }

                var role = await roleAccessor.GetUserRoleAsync(user);

                var tokenBytes   = WebEncoders.Base64UrlDecode(request.RefreshToken);
                var decodedToken = Encoding.UTF8.GetString(tokenBytes);
                var result       = await userAccessor.ConfirmRefreshTokenAsync(user, userManager, decodedToken);

                if (!result.Succeeded)
                {
                    throw new RestException(HttpStatusCode.Unauthorized);
                }

                var newRefreshToken = await userManager.GenerateUserTokenAsync(user, TokenOptions.DefaultProvider, "RefreshToken");

                return(new UserDto(user, role, tokenGenerator, newRefreshToken));
            }