【发布时间】:2014-03-25 23:25:53
【问题描述】:
我有一个支持子域的应用程序。每个子域都代表一家公司,因此每个子域的外观和感觉都可能像是他们自己网站的扩展。
这是使用通过此方法获得的 companyId 完成的:
/// <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;
}
所以,我现在已经构建了一个api,想使用OAuthAuthorizationServerOptions,但问题是每个公司的Users都不一样,是通过使用公司 ID。
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
};
}
}
}
我无法从 Startup 类访问 HttpContext,那么我是否真的可以从启动类访问当前请求的 URL?
【问题讨论】:
-
你找到解决方案了吗?