/// <summary>
        /// Deletes the refresh token associated by <see cref="BearerSignInManagerContext.Principal"/>.
        /// </summary>
        protected async Task <bool> TryDeleteUserRefreshTokenAsync(BearerSignInManagerContext <UserType, BearerTokenType> context)
        {
            var findRefreshTokenIdResult = BearerSignInManagerTools.FindRefreshTokenId(context.Principal);

            if (findRefreshTokenIdResult.Succeeded)
            {
                try {
                    await TryDeleteExpiredRefreshTokensAsync(context);

                    if (await bearerTokenStore.TryDeleteAsync(findRefreshTokenIdResult.Content))
                    {
                        return(true);
                    }
                    else
                    {
                        context.SetResult()
                        .ToFailure("The user does not have the refresh token.")
                        .WithHttpStatusCode(HttpStatusCode.Unauthorized);
                    }
                } catch (Exception?error) {
                    context.SetResult(errorDetailsProvider.LogErrorThenBuildAppropiateError <object>(error, "The refresh token could not be deleted.")
                                      .WithHttpStatusCode(HttpStatusCode.InternalServerError));
                }
            }
            else
            {
                context.SetResult()
                .ToFailure(findRefreshTokenIdResult);
            }

            return(false);
        }
Exemplo n.º 2
0
        private async Task <IServiceResult <UserType> > loadUserByNameAsync(string userName)
        {
            try {
                var createdUserEntity = dbContext.Set <UserType>().Local.SingleOrDefault(x => x.UserName == userName) ??
                                        await userManager.FindByNameAsync(userName);

                //var createdUserEntry = dbContext.Entry(createdUserEntity);
                //createdUserEntry.State = EntityState.Detached;

                return(ServiceResult <UserType>
                       .Success(createdUserEntity)
                       .WithHttpStatusCode(HttpStatusCode.OK));
            } catch (Exception sensitiveError) {
                var insensitiveErrorMessage = $"The user '{userName}' could not be loaded from the database.";

                return(errorDetailsProvider.LogErrorThenBuildAppropiateError <UserType>(sensitiveError, insensitiveErrorMessage)
                       .WithHttpStatusCode(HttpStatusCode.InternalServerError));
            }
        }