示例#1
0
        public void Should_return_claim_value_unknown()
        {
            var claimsAd = new[]
            {
                new Claim(ClaimTypes.Name, "username"),
            };

            var result = ClaimsHelper.FindAdId(claimsAd);

            result.Should().Be("unknown");
        }
示例#2
0
        public async Task <IActionResult> GetUserProfile()
        {
            try
            {
                var participant = await _userApiClient.GetUserByAdUserNameAsync(User.Claims.FirstOrDefault(c => c.Type == "preferred_username")?.Value);

                return(Ok(new UserProfileResponse {
                    Email = participant.DisplayName, Role = participant.UserRole
                }));
            }
            catch (UserApiException ex) when(ex.StatusCode == (int)HttpStatusCode.NotFound)
            {
                var userAdObjectId = ClaimsHelper.FindAdId(User.Claims);

                ApplicationLogger.TraceException
                (
                    TraceCategories.MissingResource,
                    $"Failed call to GetUserProfile(): [{userAdObjectId}]",
                    ex,
                    User
                );

                return(NotFound());
            }
            catch (UserApiException ex) when(ex.StatusCode == (int)HttpStatusCode.Unauthorized)
            {
                ApplicationLogger.TraceException
                (
                    TraceCategories.Authorization,
                    "Unauthorized call to GetUserProfile()",
                    ex,
                    User
                );

                return(Unauthorized(ex.Message));
            }
            catch (UserApiException ex)
            {
                ApplicationLogger.TraceException
                (
                    TraceCategories.Unhandled,
                    $"Failed call to GetUserProfile(): [{ex.Message}]",
                    ex,
                    User
                );

                return(StatusCode((int)HttpStatusCode.InternalServerError, ex.Message));
            }
            catch (Exception ex)
            {
                ApplicationLogger.TraceException
                (
                    TraceCategories.Unhandled,
                    "Failed call to GetUserProfile()",
                    ex,
                    User
                );

                throw;
            }
        }