【问题标题】:Owin startup class and urlOwin启动类和url
【发布时间】: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?

【问题讨论】:

标签: c# startup owin


【解决方案1】:

这在启动时不可用。您需要在 IIS 中设置单独的 虚拟目录(如果您正在使用它),并让实际不同的应用程序处理每个虚拟目录。

否则,您将需要过滤每个单独的请求(大多数网络框架都有某种路由引擎)。

【讨论】:

  • 我不能使用虚拟目录,因为可以随时使用任何子域设置公司。您提到的过滤器,它将如何解决我的问题?
  • 被请求的整个 URL 仅在请求时可用。如果你能告诉我你正在使用什么网络框架,我可能会提供进一步的帮助。
  • 在这种情况下,这可能是 (stackoverflow.com/questions/278668/…) 的副本,已回答。
  • 我不认为路由是这里的问题。我已经为我的子域处理了路由。问题是我在尝试实例化 UserService 时需要公司 ID
  • 如果子域是在运行时添加的,您将不得不在请求时实例化一个 UserServer -- 只是无法在启动时知道稍后可能会调用什么。
【解决方案2】:

正如其名称所指出的,启动在您的应用程序启动时运行。它是 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/

【讨论】:

  • 你有什么例子可以指点我吗?如果有这样的例子可以帮助我写一些东西:D
猜你喜欢
  • 2013-12-02
  • 1970-01-01
  • 1970-01-01
  • 2015-11-07
  • 2017-11-23
  • 1970-01-01
  • 1970-01-01
  • 2013-12-02
  • 1970-01-01
相关资源
最近更新 更多