Ejemplo n.º 1
0
    protected override void HandleUnauthorizedRequest(HttpActionContext actionContext)
    {
        var tokenHasExpired = false;
        var owinContext     = OwinHttpRequestMessageExtensions.GetOwinContext(actionContext.Request);

        if (owinContext != null)
        {
            tokenHasExpired = owinContext.Environment.ContainsKey("oauth.token_expired");
        }

        if (tokenHasExpired)
        {
            actionContext.Response = new AuthenticationFailureMessage("unauthorized", actionContext.Request,
                                                                      new
            {
                error         = "invalid_token",
                error_message = "The Token has expired"
            });
        }
        else
        {
            actionContext.Response = new AuthenticationFailureMessage("unauthorized", actionContext.Request,
                                                                      new
            {
                error         = "invalid_request",
                error_message = "The Token is invalid"
            });
        }
    }
        public void SetOwinContext_Throws_WhenContextIsNull()
        {
            // Arrange
            var request = new HttpRequestMessage();

            // Act & Assert
            Assert.ThrowsArgumentNull(() => { OwinHttpRequestMessageExtensions.SetOwinContext(request, null); },
                                      "context");
        }
        public void SetOwinContext_Throws_WhenRequestIsNull()
        {
            // Arrange
            var context = new OwinContext();

            // Act & Assert
            Assert.ThrowsArgumentNull(() => { OwinHttpRequestMessageExtensions.SetOwinContext(null, context); },
                                      "request");
        }
        public void SetOwinEnvironment_Throws_WhenRequestIsNull()
        {
            // Arrange
            var environment = new Dictionary <string, object>();

            // Act & Assert
            Assert.ThrowsArgumentNull(
                () => { OwinHttpRequestMessageExtensions.SetOwinEnvironment(null, environment); }, "request");
        }
 public void GetOwinContext_Throws_WhenRequestIsNull()
 {
     // Act & Assert
     Assert.ThrowsArgumentNull(
         () =>
     {
         OwinHttpRequestMessageExtensions.GetOwinContext(null);
     },
         "request"
         );
 }
Ejemplo n.º 6
0
        public Task <HttpResponseMessage> ExecuteAsync(CancellationToken cancellationToken)
        {
            // Owin.System.Web
            //HttpRequestBase req = Request
            //HttpContextBase ctx =
            //var owinCtx = HttpContextBaseExtensions.GetOwinContext(Request);
            //owinCtx.Authentication.Challenge(LoginProvider);

            var httpOwin = OwinHttpRequestMessageExtensions.GetOwinContext(Request);

            // Request.GetOwinContext()
            httpOwin.Authentication.Challenge(LoginProvider);

            HttpResponseMessage response = new HttpResponseMessage(HttpStatusCode.Unauthorized);

            response.RequestMessage = Request;
            return(Task.FromResult(response));
        }
        protected override void HandleUnauthorizedRequest(HttpActionContext actionContext)
        {
            var tokenHasExpired = false;
            var owinContext     = OwinHttpRequestMessageExtensions.GetOwinContext(actionContext.Request);

            if (owinContext != null)
            {
                tokenHasExpired = owinContext.Environment.ContainsKey("oauth.token_expired");
            }
            string authtokenKey = "";

            if (owinContext.Request.Headers.ContainsKey("Authorization"))
            {
                authtokenKey = owinContext.Request.Headers.Get("Authorization").Replace("Bearer ", "");
            }

            if (tokenHasExpired)
            {
                string   requestPath    = owinContext.Request.Environment["owin.RequestPath"].ToString();
                string[] pathComponents = requestPath.Split('/');

                new BusinessLogic().CreateLog(pathComponents[pathComponents.Length - 1], pathComponents[pathComponents.Length - 1], "0", "webapi", "Token Expired", "0", requestPath, authtokenKey, Constants.GetConnectionString());
                actionContext.Response = new AuthenticationFailureMessage("unauthorized", actionContext.Request,
                                                                          new
                {
                    error         = "invalid_token",
                    error_message = "The Token has expired"
                });
            }
            else
            {
                actionContext.Response = new AuthenticationFailureMessage("unauthorized", actionContext.Request,
                                                                          new
                {
                    error         = "invalid_request",
                    error_message = "The Token is invalid"
                });
            }
        }
Ejemplo n.º 8
0
 private IAuthenticationManager AuthenticationManager()
 {
     return(OwinHttpRequestMessageExtensions.GetOwinContext(Request).Authentication);
 }