protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
        {
            try
            {
                string authHeader = request.Headers.GetValues("Authorization").First();

                const string header = "Bearer ";

                if (string.CompareOrdinal(authHeader, 0, header, 0, header.Length) == 0)
                {
                    using (var config = new AuthenticationConfiguration())
                    {
                        var resourceServer = new WebAPIResourceServer(
                            new StandardAccessTokenAnalyzer(
                                config.CreateAuthorizationServerSigningServiceProvider(),
                                config.CreateResourceServerEncryptionServiceProvider()));

                        var principal = resourceServer.GetPrincipal(request, request.RequestUri.AbsoluteUri);
                        if (principal != null)
                        {
                            SetPrincipal(principal);
                        }
                    }
                }
                else
                {
                    return SendUnauthorizedResponse();
                }
            }
            catch (SecurityTokenValidationException)
            {
                return SendUnauthorizedResponse();
            }

            return base.SendAsync(request, cancellationToken).ContinueWith(
                task =>
                {
                    var response = task.Result;

                    if (response.StatusCode == HttpStatusCode.Unauthorized)
                    {
                        SetAuthenticateHeader(response);
                    }

                    return response;
                }, TaskContinuationOptions.ExecuteSynchronously);    // ### Need to ExecuteSynchronously as doing Asyc hangs the app
        }
Example #2
0
        protected override Task <HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
        {
            try
            {
                string authHeader = request.Headers.GetValues("Authorization").First();

                string header = "Bearer ";

                if (string.CompareOrdinal(authHeader, 0, header, 0, header.Length) == 0)
                {
                    using (AuthenticationConfiguration config = new AuthenticationConfiguration())
                    {
                        var resourceServer = new WebAPIResourceServer(new StandardAccessTokenAnalyzer(config.CreateAuthorizationServerSigningServiceProvider(), config.CreateResourceServerEncryptionServiceProvider()));
                        var principal      = resourceServer.GetPrincipal(request, request.RequestUri.AbsoluteUri);
                        if (principal != null)
                        {
                            SetPrincipal(principal);
                        }
                    }
                }
                else
                {
                    return(SendUnauthorizedResponse());
                }
            }
            catch (SecurityTokenValidationException)
            {
                return(SendUnauthorizedResponse());
            }

            return(base.SendAsync(request, cancellationToken).ContinueWith(
                       (task) =>
            {
                var response = task.Result;

                if (response.StatusCode == HttpStatusCode.Unauthorized)
                {
                    SetAuthenticateHeader(response);
                }

                return response;
            }, TaskContinuationOptions.ExecuteSynchronously));       // ### Need to ExecuteSynchronously as doing Asyc hangs the app
        }