internal static IPrincipal BindHeadersToPrincipal <Identity>(WebCheckOptions options) where Identity : WebIdentity, new()
        {
            var headers = HttpContext.Current.Request.Headers;

            var identity = new Identity()
            {
                CustomInformation = (headers[WebCustomHeaderKey.Key] ?? string.Empty).Split(',').ToList(),
                Token             = headers[WebCustomHeaderKey.Token] ?? string.Empty
            };

            WebIdentityExpando _identityExpando = new WebIdentityExpando {
                Expansions = "Identity Object for Web"
            };

            foreach (var key in ValidateHeader(headers, options))
            {
                _identityExpando[key.Replace("-", "")] = headers[key];
            }
            _identityExpando.SetValuesToOtherType(identity);

            var _principal = new Principal(identity);

            Thread.CurrentPrincipal  = _principal;
            HttpContext.Current.User = _principal;

            return(_principal);
        }
        protected void Application_Start()
        {
            AreaRegistration.RegisterAllAreas();
            GlobalConfiguration.Configure(WebApiConfig.Register);
            FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
            BundleConfig.RegisterBundles(BundleTable.Bundles);
            RouteConfig.RegisterRoutes(RouteTable.Routes);

            var options = new WebCheckOptions {
                BreadCrumbOption = new RequestMessageHandler.Entities.BreadCrumbs.BreadCrumbOptions {
                    CreateBreadCrumbs = true
                }
            };

            ControllerBuilder.Current.SetControllerFactory(new WebMvcDefaultControllerFactory <MessageInterceptors.ApiKeyInterceptor>(options));
            //OR//
            //ControllerBuilder.Current.SetControllerFactory(new WebMvcIControllerFactory<MessageInterceptors.ApiKeyInterceptor>());
        }
Пример #3
0
        public static void Register(HttpConfiguration config)
        {
            // Web API configuration and services

            // Web API routes
            config.MapHttpAttributeRoutes();

            var options = new WebCheckOptions { /*TokenMustExist = true*/
            };

            config.Routes.MapHttpRoute(
                name: "apiDefault",
                routeTemplate: "api/{controller}/{id}",
                defaults: new { id = RouteParameter.Optional },
                handler: new WebApiRequestHandler <ApiKeyInterceptor>(config, options),
                constraints: null
                );
        }
 public WebMvcIControllerFactory(WebCheckOptions options = null)
 {
     Options = options;
 }
        private static IEnumerable <string> ValidateHeader(NameValueCollection headers, WebCheckOptions options)
        {
            var keys = headers.AllKeys.Where(k => !IsKnownKey(k));

            if (options != null)
            {
                string _token = keys.FirstOrDefault(k => k.Equals(options.TokenIdentity, StringComparison.CurrentCultureIgnoreCase));

                if (options.TokenMustExist && !options.TokenIdentity.Empty() && _token == null)
                {
                    throw new WebRequestInterceptorException("Token Identity not present during current request.");
                }

                _token = !_token.Empty() ? headers[options.TokenIdentity] : headers[WebCustomHeaderKey.Token];

                if (options.TokenMustExist && _token.Empty())
                {
                    throw new WebRequestInterceptorException("Token value not present during current request.");
                }

                if (!options.TokenExactValue.Empty() && !_token.Equals(options.TokenExactValue))
                {
                    throw new WebRequestInterceptorException("Token value expected is incorrect during current request.");
                }


                foreach (var chead in options.CustomHeaders)
                {
                    if (!chead.MustExist)
                    {
                        continue;
                    }

                    string _value = headers[chead.Name];
                    if (_value.Empty())
                    {
                        throw new WebRequestInterceptorException($"Header '{chead.Name}' not present during current request.");
                    }
                }
            }

            return(keys);
        }
Пример #6
0
 public WebApiRequestHandler(HttpConfiguration config, WebCheckOptions options = null)   : base(config)
 {
     Options = options;
 }
 public WebApiDelegateHandler(WebCheckOptions options = null)
 {
     Options = options;
 }