Exemple #1
0
        protected override async Task ApplyResponseChallengeAsync()
        {
            IResponseMessage responseMessage = new OwinResponseMessage(Response);

            var header = await server.CreateServerAuthorizationAsync(responseMessage);

            if (header != null)
            {
                responseMessage.AddHeader(header.Item1, header.Item2);
            }
        }
        protected async override Task <HttpResponseMessage> SendAsync(
            HttpRequestMessage request, CancellationToken cancellationToken)
        {
            Tracing.Start("HawkAuthenticationHandler");

            try
            {
                var 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;
                    }

                    request.SetUserPrincipal(principal);

                    var requestContext = request.Properties["MS_RequestContext"] as System.Web.Http.Controllers.HttpRequestContext;
                    if (requestContext != null)
                    {
                        requestContext.Principal = 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);
            }
        }
        protected override async 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.SetUserPrincipal(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 = new HttpResponseMessage()
                {
                    StatusCode     = HttpStatusCode.Unauthorized,
                    RequestMessage = request
                };

                response.Headers.WwwAuthenticate.Add(new AuthenticationHeaderValue(HawkConstants.Scheme));

                return(response);
            }
        }
        protected async override Task <HttpResponseMessage> SendAsync(
            HttpRequestMessage request, CancellationToken cancellationToken)
        {
            Tracing.Start("HawkAuthenticationHandler");

            try
            {
                HawkServer server = new HawkServer(request, credentialsCallback, verificationCallback);

                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);

                await server.CreateServerAuthorizationAsync(response, this.normalizationCallback);

                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);
            }
        }
        protected override async Task ApplyResponseChallengeAsync()
        {
            // In case of 401, we do not add WWW-Authenticate, if authentication mode is passive.
            if (Response.StatusCode == 401)
            {
                var challenge = Helper.LookupChallenge(Options.AuthenticationType, Options.AuthenticationMode);

                if (challenge == null)
                {
                    return;
                }
            }

            IResponseMessage responseMessage = new OwinResponseMessage(Response);

            var header = await server.CreateServerAuthorizationAsync(responseMessage);

            if (header != null)
            {
                responseMessage.AddHeader(header.Item1, header.Item2);
            }
        }