I have an application that supports subdomains. Each subdomain represents a company and thus each subdomain potentially looks and feels like an extension of their own website.
This is done using a companyId which is obtained by this method:
/// <summary>
/// Get our company based from the URI host
/// </summary>
/// <returns>A company</returns>
public Company GetTenant()
{
var host = ConfigurationManager.AppSettings["domain"];
var currentHost = HttpContext.Current.Request.Headers["HOST"];
var defaultUri = GetUriFromUrl(host);
var currentUri = GetUriFromUrl(currentHost);
foreach (var company in this.GetAll("Settings"))
if (CheckCompanyByUri(company, currentUri))
return company;
if (!currentUri.IsLoopback && !CompareUrls(currentUri, defaultUri))
throw new Exception("The URL you have specified is not in our systems: " + currentUri.Host);
return null;
}
So, I have now built an api and want to use OAuthAuthorizationServerOptions but the problem is that the Users for each company are different and are obtained by using the CompanyId.
static Startup()
{
using (var uow = new UnitOfWork<SkipstoneContext>())
{
var service = new CompanyService(uow, null);
var company = service.GetTenant(); // HttpContext is not available at this point (so getting the url is impossible)
if (company != null)
{
var companyId = company.Id;
UserService = new UserService(uow, null, companyId);
PublicClientId = companyId;
OAuthOptions = new OAuthAuthorizationServerOptions
{
TokenEndpointPath = new PathString("/Token"),
Provider = new ApplicationOAuthProvider(PublicClientId, UserService),
AuthorizeEndpointPath = new PathString("/api/Account/ExternalLogin"),
AccessTokenExpireTimeSpan = TimeSpan.FromDays(14),
AllowInsecureHttp = true
};
}
}
}
I don't have access to HttpContext from the Startup class, so is there anyway I can actually get access to the current requested URL from startup?