コード例 #1
0
        protected override void Initialize(HttpControllerContext controllerContext)
        {
            var requestUrl = string.Empty;

            try
            {
                base.Initialize(controllerContext);
                requestUrl = controllerContext.Request?.RequestUri?.AbsolutePath;

                // Get referer
                var providedUri = Request.Headers.Referrer;

                if (providedUri == null)
                {
                    throw new BaseException("Referrer required");
                }

                Log.Info($"Starting request for {providedUri}");
                // Get app token
                var authKey = controllerContext.Request.Headers.Authorization;

                if (authKey == null)
                {
                    throw new BaseException("Authorizaton token required");
                }

                Log.Info("API key found");
                var apiKey = HttpUtility.UrlDecode(authKey.ToString());
                LoggedInApplication = _applicationsService.GetLoggedInApp(providedUri.ToString(), apiKey);
            }
            catch (BaseException ex)
            {
                Log.Info($"[{requestUrl}] {ex.Message}");
                throw new HttpResponseException(new HttpResponseMessage
                {
                    StatusCode = HttpStatusCode.Unauthorized,
                    Content    = new StringContent(ex.Message)
                });
            }
            catch (Exception ex)
            {
                Log.Error($"[{requestUrl}] {ex}");
                throw new HttpResponseException(new HttpResponseMessage
                {
                    StatusCode = HttpStatusCode.Unauthorized,
                    Content    = new StringContent("Unexpected error")
                });
            }
        }