/// <summary> /// Gets the tenantId based on the domain. /// </summary> /// <param name="routeData"></param> /// <returns>The Tennant Id, which is the Account.Id associated with the domain in the route information.</returns> public static int GetTenantId(this System.Web.Routing.RouteData routeData) { DomainRoute dr = (DomainRoute)routeData.Route; ShoelaceDbContext db = new ShoelaceDbContext(); int tenantId = -1; var sub = routeData.Values["subdomain"] as string; Account acc = null; if (sub != null) { acc = db.Accounts.FirstOrDefault(x => x.Subdomain == sub); } if (null != acc) { tenantId = acc.Id; } else { acc = db.Accounts.FirstOrDefault(x => x.VanityDomain == dr.Domain); if (acc != null) { tenantId = acc.Id; } } return tenantId; }
protected override void OnAuthentication(System.Web.Mvc.Filters.AuthenticationContext filterContext) { using (var ctx = new ShoelaceDbContext()) { var tid = RouteData.GetTenantId(); if (User.Identity.IsAuthenticated) { var ct = ctx.Users.Where(x => x.AccountId == tid && x.UserName == User.Identity.Name).Count(); if (ct <= 0) { HttpContext.SignOut(); filterContext.Result = RedirectToAction("Index", "Home"); } } } base.OnAuthentication(filterContext); }