public AuthenticationResult Authenticate(HttpRequestMessage requestMessage)
        {
            var authenticationResult = _tokenAuthenticator.Authenticate(requestMessage);

            if (authenticationResult.IsAuthenticated || requestMessage.Method == HttpMethod.Options)
            {
                ConsoleHelper.Info("Authenticating with TOKEN", ConsoleColor.Cyan);
                return(authenticationResult);
            }

            ConsoleHelper.Info("Authenticating with DB Credentials", ConsoleColor.DarkYellow);
            return(_credentialsAuthenticator.Authenticate(requestMessage));
        }
Exemple #2
0
        public bool Authenticate(HttpActionContext actionContext, ITokenAuthenticator authenticator)
        {
            string token = string.Empty;

            try
            {
                token = actionContext.Request.Headers.GetValues(_authorizationHeaderKey).First();
            }
            catch (Exception)
            {
                actionContext.Response = new HttpResponseMessage(System.Net.HttpStatusCode.BadRequest)
                {
                    Content = new StringContent("Missing Authorization Header (" + _authorizationHeaderKey + ")")
                };
                return(false);
            }

            try
            {
                if (authenticator.Authenticate(token, actionContext))
                {
                    return(true);
                }
                else
                {
                    throw new Exception();
                }
            }
            catch (Exception e)
            {
                actionContext.Response = new HttpResponseMessage(System.Net.HttpStatusCode.Unauthorized)
                {
                    Content = new StringContent("Unauthorized User")
                };
                return(false);
            }
        }
        public bool Authenticate(HttpActionContext actionContext, ITokenAuthenticator authenticator)
        {
            string token = string.Empty;
    
            try
            {
                token = actionContext.Request.Headers.GetValues(_authorizationHeaderKey).First();
            }
            catch (Exception)
            {
                actionContext.Response = new HttpResponseMessage(System.Net.HttpStatusCode.BadRequest)
                {
                    Content = new StringContent("Missing Authorization Header (" + _authorizationHeaderKey + ")")
                };
                return false;
            }

            try
            {
                if (authenticator.Authenticate(token, actionContext))
                {
                    return true;
                }
                else
                {
                    throw new Exception();
                }
            }
            catch (Exception e)
            {
                actionContext.Response = new HttpResponseMessage(System.Net.HttpStatusCode.Unauthorized)
                {
                    Content = new StringContent("Unauthorized User")
                };
                return false;
            }
        }