/// <summary>
        /// Adds an Authentication header to the default request headers of the <see cref="HttpClient"/>.
        /// Uses the provided claims and "TestServer" as authentication scheme.
        /// </summary>
        /// <param name="httpClient">The httpClient instance</param>
        /// <param name="claims">The claims collection that represents the user identity</param>
        /// <returns></returns>
        public static HttpClient WithDefaultIdentity(this HttpClient httpClient, IEnumerable <Claim> claims)
        {
            var headerName =
                AuthenticationHeaderHelper.GetHeaderName(TestServerDefaults.AuthenticationScheme);

            httpClient.DefaultRequestHeaders.Add(
                name: headerName,
                value: $"{TestServerDefaults.AuthenticationScheme} {DefautClaimsEncoder.Encode(claims)}");

            return(httpClient);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Adds an Authentication header to the <see cref="RequestBuilder"/> with the provided claims using
        /// "TestServer" as authentication scheme.
        /// </summary>
        /// <param name="requestBuilder">The requestBuilder instance</param>
        /// <param name="claims">The claims collection that represents the user identity</param>
        /// <returns></returns>
        public static RequestBuilder WithIdentity(this RequestBuilder requestBuilder, IEnumerable <Claim> claims)
        {
            var headerName =
                AuthenticationHeaderHelper.GetHeaderName(TestServerAuthenticationDefaults.AuthenticationScheme);

            requestBuilder.AddHeader(
                headerName,
                $"{TestServerAuthenticationDefaults.AuthenticationScheme} {DefautClaimsEncoder.Encode(claims)}");

            return(requestBuilder);
        }
        /// <summary>
        /// Get the header string for the provided claims
        /// </summary>
        /// <param name="httpClient">The httpClient instance</param>
        /// <param name="claims">The claims collection that represents the user identity</param>
        /// <param name="scheme">The authentication scheme</param>
        /// <returns></returns>
        public static string GetHeaderForIdentity(this HttpClient httpClient, IEnumerable <Claim> claims, string scheme)
        {
            if (string.IsNullOrWhiteSpace(scheme))
            {
                throw new ArgumentNullException(nameof(scheme));
            }

            var headerName = AuthenticationHeaderHelper.GetHeaderName(scheme);

            var header = new NameValueHeaderValue(
                headerName,
                $"{TestServerDefaults.AuthenticationScheme} {DefautClaimsEncoder.Encode(claims)}");

            return(header.ToString());
        }
Exemplo n.º 4
0
        /// <summary>
        /// Adds an Authentication header to the <see cref="RequestBuilder"/> with the provided claims
        /// and authentication scheme.
        /// </summary>
        /// <param name="requestBuilder">The requestBuilder instance</param>
        /// <param name="claims">The claims collection that represents the user identity</param>
        /// <param name="scheme">The authentication scheme</param>
        /// <returns></returns>
        public static RequestBuilder WithIdentity(this RequestBuilder requestBuilder, IEnumerable <Claim> claims, string scheme)
        {
            if (string.IsNullOrWhiteSpace(scheme))
            {
                throw new ArgumentNullException(nameof(scheme));
            }

            var headerName = AuthenticationHeaderHelper.GetHeaderName(scheme);

            requestBuilder.AddHeader(
                headerName,
                $"{scheme} {DefautClaimsEncoder.Encode(claims)}");

            return(requestBuilder);
        }
        /// <summary>
        /// Adds an Authentication header to the default request headers of the <see cref="HttpClient"/>.
        /// Uses the provided claims and authentication scheme.
        /// </summary>
        /// <param name="httpClient">The httpClient instance</param>
        /// <param name="claims">The claims collection that represents the user identity</param>
        /// <param name="scheme">The authentication scheme</param>
        /// <returns></returns>
        public static HttpClient WithDefaultIdentity(this HttpClient httpClient, IEnumerable <Claim> claims, string scheme)
        {
            if (string.IsNullOrWhiteSpace(scheme))
            {
                throw new ArgumentNullException(nameof(scheme));
            }

            var headerName =
                AuthenticationHeaderHelper.GetHeaderName(scheme);

            httpClient.DefaultRequestHeaders.Add(
                name: headerName,
                value: $"{scheme} {DefautClaimsEncoder.Encode(claims)}");

            return(httpClient);
        }
Exemplo n.º 6
0
        public async Task <IActionResult> Profile()
        {
            User currentUser = null;

            try
            {
                currentUser = await _graphServiceClient.Me.Request().GetAsync();
            }
            catch (System.Exception ex) // Catch CAE exception from Graph SDK
            {
                if (ex is ServiceException && ex.Message.Trim().Contains("Continuous access evaluation resulted in claims challenge"))
                {
                    try
                    {
                        ServiceException svcex = ex as ServiceException;
                        Console.WriteLine($"{svcex}");
                        var claimChallenge = AuthenticationHeaderHelper.ExtractClaimChallengeFromHttpHeader(svcex.ResponseHeaders);
                        _consentHandler.ChallengeUser(_graphScopes, claimChallenge);
                        return(new EmptyResult());
                    }
                    catch (Exception ex2)
                    {
                        _consentHandler.HandleException(ex2);
                    }
                }
            }

            try
            {
                // Get user photo
                using (var photoStream = await _graphServiceClient.Me.Photo.Content.Request().GetAsync())
                {
                    byte[] photoByte = ((MemoryStream)photoStream).ToArray();
                    ViewData["Photo"] = Convert.ToBase64String(photoByte);
                }
            }
            catch (Exception pex)
            {
                Console.WriteLine($"{pex}");
                ViewData["Photo"] = null;
            }

            ViewData["Me"] = currentUser;
            return(View());
        }