/// <summary>
        /// Set context user.
        /// </summary>
        /// <exception cref="ArgumentNullException">
        /// Thrown by <see cref="UserManager{TUser}.GetUserAsync(ClaimsPrincipal)"/>.
        /// </exception>
        protected async Task <bool> TrySetContextUserAsync(BearerSignInManagerContext <UserType, BearerTokenType> context)
        {
            try {
                var user = await userManager.GetUserAsync(context.Principal);

                if (!ReferenceEquals(user, null))
                {
                    context.User = user;
                    return(true);
                }
                else
                {
                    const string errorMessage = "The user could not be found.";
                    logger?.LogDebug(errorMessage);

                    context.SetResult()
                    .ToFailure(errorMessage)
                    .WithHttpStatusCode(HttpStatusCode.BadRequest);
                }
            } catch (Exception error) {
                var insensitiveErrorMessage = $"The user could not be loaded.";

                context.SetResult(errorDetailsProvider.LogCriticalThenBuildAppropiateError <object>(error, insensitiveErrorMessage)
                                  .WithHttpStatusCode(HttpStatusCode.InternalServerError));
            }

            context.User = null;
            return(false);
        }
Пример #2
0
        private async Task <IServiceResult <RoleType> > loadRoleByNameAsync(string roleName)
        {
            try {
                var createdRoleEntity = dbContext.Set <RoleType>().Local.SingleOrDefault(x => x.RoleName == roleName) ??
                                        await roleManager.FindByNameAsync(roleName);

                return(ServiceResult <RoleType>
                       .Success(createdRoleEntity)
                       .WithHttpStatusCode(HttpStatusCode.OK));
            } catch (Exception error) {
                return(errorDetailsProvider.LogCriticalThenBuildAppropiateError <RoleType>(error.ToJsonErrors(), $"The role '{roleName}' could not be loaded from the database.".ToJsonErrors())
                       .WithHttpStatusCode(HttpStatusCode.InternalServerError));
            }
        }