public async Task <RouteValidationResponse> VerifyRequest(HttpContext context) { var routes = RouteManager.Routes .Where(info => context.Request.Path.Value.StartsWith(info.PathMatcher)) .ToList(); // add more validation as needed if (!routes.Any()) { return(RouteValidationResponse.CreateFailure(HttpStatusCode.NotFound)); } var route = routes.First(); if (!route.IsOpen && !context.User.Identity.IsAuthenticated) { return(RouteValidationResponse.CreateFailure(HttpStatusCode.Unauthorized)); } var tenantContext = await _resolver.ResolveAsync(context).ConfigureAwait(false); if (tenantContext == null && !route.IsOpen) { return(RouteValidationResponse.CreateFailure(HttpStatusCode.Forbidden)); } if (route.IsPartitioned && tenantContext == null) { return(RouteValidationResponse.CreateFailure(HttpStatusCode.BadRequest)); } return(RouteValidationResponse.CreateSuccess(tenantContext?.Tenant ?? IdentityModel.Empty, route)); }
private async Task <ISiteSettings> GetSite() { if (multiTenantOptions.UseRelatedSitesMode) { if (multiTenantOptions.Mode == MultiTenantMode.FolderName) { CancellationToken cancellationToken = contextAccessor.HttpContext?.RequestAborted ?? CancellationToken.None; site = await siteRepo.Fetch(multiTenantOptions.RelatedSiteId, cancellationToken); return(site); } } TenantContext <SiteSettings> tenantContext = await siteResolver.ResolveAsync(contextAccessor.HttpContext); if (tenantContext != null && tenantContext.Tenant != null) { site = tenantContext.Tenant; } return(site); }
public async Task ValidatePrincipal(CookieValidatePrincipalContext context) { // TODO: uncomment this after next release of aspnet core // and fix the broken // it needs to resolve options per tenant //await securityStampValidator.ValidateAsync(context); TenantContext <SiteSettings> siteContext = await siteResolver.ResolveAsync(contextAccessor.HttpContext); if (siteContext == null) { context.RejectPrincipal(); } if (siteContext.Tenant == null) { context.RejectPrincipal(); } Claim siteGuidClaim = new Claim("SiteGuid", siteContext.Tenant.SiteGuid.ToString()); if (!context.Principal.HasClaim(siteGuidClaim.Type, siteGuidClaim.Value)) { log.LogInformation("rejecting principal because it does not have siteguid"); context.RejectPrincipal(); } // return Task.FromResult(0); }
public async Task <TenantContext <TTenant> > ResolveAsync(IDictionary <string, object> environment) { Ensure.Argument.NotNull(environment, "environment"); // Obtain the cache key from the environment var cacheKey = requestIdentification(environment); var tenantContext = cache.Get(cacheKey) as TenantContext <TTenant>; if (tenantContext == null) { tenantContext = await tenantResolver.ResolveAsync(environment); if (tenantContext != null) { var tenantIdentifiers = tenantIdentification(tenantContext.Tenant); var policy = new CacheItemPolicy(); // TODO foreach (var identifier in tenantIdentifiers) { cache.Set(new CacheItem(identifier, tenantContext), policy); } } } return(tenantContext); }
public async Task Invoke(HttpContext context, ITenantResolver tenantResolver) { tenantsHost.Initialize(); logger.LogDebug($"Resolving Tenant using \"{tenantResolver.GetType().FullName}\"."); var tenant = await tenantResolver.ResolveAsync(context); if (tenant != null) { logger.LogDebug("Start getting TenantContext from TenantsHost."); var tenantContext = tenantsHost.GetOrCreateContext(tenant); context.SetTenantContext(tenantContext); logger.LogDebug("Getting TenantContext successful and set to HttpContext."); using (var scope = tenantContext.EnterServiceScope()) { await next.Invoke(context); } } else if (tenantsConfig.UnresolvedRedirect.HasValue()) { logger.LogDebug($"Tenant resolved failure. Now redreact to url: {tenantsConfig.UnresolvedRedirect}."); Redirect(context, tenantsConfig.UnresolvedRedirect, tenantsConfig.RedirectPermanent); } else { await next.Invoke(context); } }
public async Task Invoke(HttpContext context, ITenantResolver tenantResolver) { var tenantContext = await tenantResolver.ResolveAsync(context); if (tenantContext != null) { context.SetTenantContext(tenantContext); } await _next.Invoke(context); }
private async Task <SiteSettings> GetSite() { TenantContext <SiteSettings> tenantContext = await siteResolver.ResolveAsync(contextAccessor.HttpContext); if (tenantContext != null && tenantContext.Tenant != null) { return(tenantContext.Tenant); } return(null); }
/// <summary> /// /// </summary> /// <param name="environment"></param> /// <returns></returns> public async Task Invoke(IDictionary <string, object> environment) { Ensure.Argument.NotNull(environment, nameof(environment)); ITenantResolver <TTenant> tenantResolver = _tenantResolverFactory(); TenantContext <TTenant> tenantContext = await tenantResolver.ResolveAsync(environment); if (tenantContext != null) { environment.SetTenantContext(tenantContext); } await _next(environment); }
//private string requiredSiteFolder; //public SiteFolderRouteConstraint(string folderParam) //{ // requiredSiteFolder = folderParam; //} //TODO: is this getting called on requests for static resources? // want to make sure it is not, we don't want to hit the db on requests for static files public bool Match( HttpContext httpContext, IRouter route, string parameterName, IDictionary <string, object> values, RouteDirection routeDirection) { // TODO: problem here is we want to make this method be async //https://github.com/aspnet/Mvc/issues/1094 // but may have to jump through some hoops // for now we are calling an async method synchronously while blocking the thread string requestFolder = httpContext.Request.Path.StartingSegment(); //return string.Equals(requiredSiteFolder, requestFolder, StringComparison.CurrentCultureIgnoreCase); ITenantResolver <SiteSettings> siteResolver = httpContext.ApplicationServices.GetService <ITenantResolver <SiteSettings> >(); if (siteResolver != null) { try { // exceptions expected here until db install scripts have run or if db connection error //ISiteSettings site = siteResolver.Resolve(); //var siteContext = Task.Run<TenantContext<SiteSettings>>(fun => siteResolver.ResolveAsync(httpContext)); // Func <Task <TenantContext <SiteSettings> > > f = delegate() { return(siteResolver.ResolveAsync(httpContext)); }; //http://stackoverflow.com/questions/22628087/calling-async-method-synchronously var siteContext = Task.Run <TenantContext <SiteSettings> >(f).Result; if ((siteContext != null) && (siteContext.Tenant != null) && (siteContext.Tenant.SiteFolderName == requestFolder)) { return(true); } } catch { // do we need to log this? } } return(false); }
public async Task <TimeZoneInfo> GetSiteTimeZone() { TenantContext <SiteSettings> siteContext = await siteResolver.ResolveAsync(contextAccessor.HttpContext); if ((siteContext != null) && (siteContext.Tenant != null)) { if ((siteContext.Tenant.TimeZoneId.Length > 0)) { return(TimeZoneInfo.FindSystemTimeZoneById(siteContext.Tenant.TimeZoneId)); } } return(TimeZoneInfo.Utc); }
public async Task Process(IncomingStepContext context, Func <Task> next) { _logger.LogDebug("Resolving TenantContext using {loggerType}."); var message = context.Load <Message>(); var tenantContext = await _tenantResolver.ResolveAsync(context); if (tenantContext != null) { Console.WriteLine("TenantContext Resolved. Adding to IncomingStepContext."); context.SetTenantContext(tenantContext); } else { Console.WriteLine("TenantContext Not Resolved."); } await next(); }
public async Task Invoke(HttpContext context, ITenantResolver tenantResolver) { logger.LogDebug("Resolving TenantContext using {loggerType}.", tenantResolver.GetType().Name); var tenantContext = await tenantResolver.ResolveAsync(context); if (tenantContext != null) { logger.LogDebug("TenantContext resolved successful. Adding to HttpContext."); context.SetTenantContext(tenantContext); } else if (tenantsConfig.UnresolvedRedirect.HasValue()) { logger.LogDebug("TenantContext resolved failure. Now redreact to unresolved url."); Redirect(context, tenantsConfig.UnresolvedRedirect, tenantsConfig.RedirectPermanent); return; } await next.Invoke(context); }
public async Task Invoke(HttpContext context, ITenantResolver <TTenant> tenantResolver) { Ensure.Argument.NotNull(context, nameof(context)); Ensure.Argument.NotNull(tenantResolver, nameof(tenantResolver)); log.LogDebug("Resolving TenantContext using {loggerType}.", tenantResolver.GetType().Name); var tenantContext = await tenantResolver.ResolveAsync(context); if (tenantContext != null) { log.LogDebug("TenantContext Resolved. Adding to HttpContext."); context.SetTenantContext(tenantContext); } else { log.LogDebug("TenantContext Not Resolved."); } await next.Invoke(context); }
public async Task Invoke( HttpContext httpContext, ILog <TenantResolutionMiddleware <TTenant> > logger, ITenantResolver <TTenant> tenantResolver) { logger?.Debug($"Resolving TenantContext using {tenantResolver.GetType().Name}."); TenantContext <TTenant> tenantContext = await tenantResolver.ResolveAsync(httpContext).ConfigureAwait(false); if (tenantContext != null) { logger?.Debug("TenantContext Resolved. Adding to HttpContext."); httpContext?.SetTenantContext(tenantContext); } else { logger?.Warn("TenantContext Not Resolved."); } //using (logger.Info($"Tenant:{httpContext.GetTenant<TTenant>()}")) { await next.Invoke(httpContext).ConfigureAwait(false); } }
public async Task Invoke(HttpContext context, ITenantResolver resolver, IConfiguration configuration, IServiceProvider provider) { Guard.ArgumentNotNull(context, nameof(context)); Guard.ArgumentNotNull(resolver, nameof(resolver)); Guard.ArgumentNotNull(configuration, nameof(configuration)); _logger.LogDebug("Resolving Tenant using {loggerType}.", resolver.GetType().Name); var tenant = await resolver.ResolveAsync(context); if (tenant != null) { _logger.LogDebug("Tenant Resolved. Adding to HttpContext."); context.SetTenantContext(new TenantContext(tenant)); } else { _logger.LogDebug("Tenant Not Resolved."); } await _next.Invoke(context); }