Esempio n. 1
0
        /// <summary>
        /// Initializes static members of the <see cref="TenantFilterAttribute"/> class.
        /// </summary>
        static TenantFilterAttribute()
        {
            string allowedTenantSetting = ConfigurationManager.AppSettings["AllowedTenants"];

            if (!string.IsNullOrEmpty(allowedTenantSetting))
            {
                tenantFiltering = new TenantFiltering(allowedTenantSetting.Split(new char[1] {
                    ','
                }, StringSplitOptions.RemoveEmptyEntries).ToList());
            }
        }
        /// <summary>
        /// Called when request is received.
        /// </summary>
        /// <param name="context">The action context.</param>
        /// <param name="next">The next delegate.</param>
        /// <returns>Task tracking async operation.</returns>
        public override async Task OnActionExecutionAsync(ActionExecutingContext context, ActionExecutionDelegate next)
        {
            var activities = TenantFiltering.GetActivities(context.ActionArguments);

            if (activities.Any(activity => !tenantFiltering.IsFromAllowedTenant(activity)))
            {
                context.Result = new StatusCodeResult((int)HttpStatusCode.Forbidden);
            }
            else
            {
                await next();
            }
        }
Esempio n. 3
0
        /// <summary>
        /// Called when request is received.
        /// </summary>
        /// <param name="actionContext">The action context.</param>
        /// <param name="cancellationToken">The cancellation token.</param>
        /// <returns>Task tracking operation.</returns>
        public override async Task OnActionExecutingAsync(HttpActionContext actionContext, CancellationToken cancellationToken)
        {
            if (tenantFiltering != null)
            {
                await base.OnActionExecutingAsync(actionContext, cancellationToken);

                var activities = TenantFiltering.GetActivities(actionContext.ActionArguments);

                if (activities.Any())
                {
                    if (!tenantFiltering.IsFromAllowedTenant(activities.First()))
                    {
                        actionContext.Response = new HttpResponseMessage(HttpStatusCode.Forbidden);
                    }
                }
            }
        }
        /// <summary>
        /// Initializes static members of the <see cref="TenantFilterAttribute"/> class.
        /// </summary>
        /// <exception cref="System.Exception">
        /// Service provider registration is missing please use app.UseBotConnector in Startup.cs to register service
        /// or
        /// Failed to get list of allowed tenants. Ensure that configuration has AllowedTenants element with the comma separated list of tenant Ids. Tenant Ids must be Guid.
        /// </exception>
        static TenantFilterAttribute()
        {
            if (!ServiceProvider.IsRegistered)
            {
                throw new Exception("Service provider registration is missing please use app.UseBotConnector in Startup.cs to register service");
            }

            try
            {
                tenantFiltering = new TenantFiltering(ServiceProvider.Instance.ConfigurationRoot["AllowedTenants"].Split(',').ToList());
            }
            catch (Exception ex)
            {
                throw new Exception(
                          "Failed to get list of allowed tenants. Ensure that configuration has AllowedTenants element with the comma separated list of tenant Ids. Tenant Ids must be Guid.",
                          ex);
            }
        }