protected async override Task<HttpResponseMessage> SendAsync(
                                        HttpRequestMessage request, CancellationToken cancellationToken)
        {
            try
            {
                HawkServer server = new HawkServer(new WebApiRequestMessage(request), options);

                var principal = await server.AuthenticateAsync();

                if (principal != null && principal.Identity.IsAuthenticated)
                {
                    request.GetRequestContext().Principal = principal;

                    HawkEventSource.Log.Debug("Authentication Successful and principal set for " + principal.Identity.Name);
                }

                var response = await base.SendAsync(request, cancellationToken);

                var header = await server.CreateServerAuthorizationAsync(new WebApiResponseMessage(response));
                if (header != null)
                    response.Headers.Add(header.Item1, header.Item2);

                return response;
            }
            catch (Exception exception)
            {
                HawkEventSource.Log.Exception(exception.ToString());

                var response = request.CreateResponse(HttpStatusCode.Unauthorized);
                response.Headers.WwwAuthenticate.Add(new AuthenticationHeaderValue(HawkConstants.Scheme));

                return response;
            }
        }
コード例 #2
0
        protected async override Task<HttpResponseMessage> SendAsync(
                                        HttpRequestMessage request, CancellationToken cancellationToken)
        {
            Tracing.Start("HawkAuthenticationHandler");

            try
            {
                HawkServer server = new HawkServer(new WebApiRequestMessage(request), options);

                var principal = await server.AuthenticateAsync();

                if (principal != null && principal.Identity.IsAuthenticated)
                {
                    Thread.CurrentPrincipal = principal;

                    if (HttpContext.Current != null)
                        HttpContext.Current.User = principal;

                    Tracing.Verbose("Authentication Successful and principal set for " + principal.Identity.Name);
                }

                var response = await base.SendAsync(request, cancellationToken);

                var header = await server.CreateServerAuthorizationAsync(new WebApiResponseMessage(response));
                if (header != null)
                    response.Headers.Add(header.Item1, header.Item2);

                return response;
            }
            catch (Exception ex)
            {
                Tracing.Error("Exception: " + ex.ToString());

                var response = request.CreateResponse(HttpStatusCode.Unauthorized);
                response.Headers.WwwAuthenticate.Add(new AuthenticationHeaderValue(HawkConstants.Scheme));

                return response;
            }
        }