예제 #1
0
        public override void OnAuthorization(
            System.Web.Http.Controllers.HttpActionContext actionContext)
        {
            base.OnAuthorization(actionContext);
            var token = actionContext.Request.Headers.Authorization;

            if (token != null)
            {
                string authenticationToken = Convert.ToString(token);
                if (!TokenStorage.GetInstance().ValidateToken(authenticationToken))
                {
                    HttpContext.Current.Response.AddHeader("Authorization", authenticationToken);
                    HttpContext.Current.Response.AddHeader("AuthenticationStatus", "NotAuthorized");
                    actionContext.Response = new HttpResponseMessage(System.Net.HttpStatusCode.Forbidden);
                    return;
                }

                HttpContext.Current.Response.AddHeader("Authorization", authenticationToken);
                HttpContext.Current.Response.AddHeader("AuthenticationStatus", "Authorized");
                return;
            }
            actionContext.Response =
                actionContext.Request.CreateResponse(HttpStatusCode.ExpectationFailed);
            actionContext.Response.ReasonPhrase = "Please provide valid authorization";
        }
예제 #2
0
 public static TokenStorage GetInstance()
 {
     if (instance == null)
     {
         if (instance == null)
         {
             instance = new TokenStorage();
         }
     }
     return(instance);
 }