[英]Owin startup class and url
我有一個支持子域的應用程序。 每個子域代表一家公司,因此每個子域可能看起來和感覺像是其自身網站的擴展。
這是通過使用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,但是問題是每個公司的用戶都不同,並且可以通過使用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
};
}
}
}
我沒有從Startup類訪問HttpContext的權限,因此我是否真的可以從啟動時訪問當前請求的URL?
啟動時不可用。 您將需要在IIS中設置單獨的虛擬目錄 (如果正在使用),並且需要使用實際不同的應用程序來處理每個虛擬目錄。
否則,您將需要針對每個單獨的請求進行過濾(大多數webframeworks為此具有某種路由引擎)。
顧名思義,啟動將在應用程序啟動時運行。 這是Web應用程序的入口點。 它設置了中間件堆棧,包括您的應用程序在內的中間件。 這意味着在此階段不一定有任何請求。
您想要編寫一個將在OWIN管道開始時插入的中間件 。
在該中間件中,您可以理解請求,分析所需的任何參數,並在命中任何應用程序代碼之前重定向用戶。
有什么幫助嗎?
編輯
偽代碼示例:
public class MyMiddleware : OwinMiddleware
{
public MyMiddleware(OwinMiddleware next)
: base(next) { }
public override Task Invoke(IOwinContext context)
{
//Analyze the request.
//find the client id
//RedirectToClientSpecificUrl?
}
}
在啟動時:
app.Use<MyMiddleware>();
但是您可能會通過一些快速的Google Fu找到更多相關的示例:
在這里,他負責身份驗證,並在中間件中進行了重定向: http : //blog.tomasjansson.com/owin-middleware/
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.