【问题标题】:Change OWIN Auth Middleware Per Request (Multi-tenant, oauth API keys per tenant)根据请求更改 OWIN Auth 中间件(多租户,每个租户的 oauth API 密钥)
【发布时间】:2014-10-13 03:11:41
【问题描述】:

我有一个多租户应用程序。每个租户都可以通过 Facebook、Twitter、Google 等使用 OAUTH-2 对其用户进行身份验证。每个租户都有自己的 API 密钥用于上述服务。

设置 OWIN 管道的典型方法是在 Startup 中“使用”身份验证提供程序,但这会在应用启动时设置 API 密钥。我需要能够为每个请求更改每个 oauth API 使用的密钥。

        app.UseCookieAuthentication(new CookieAuthenticationOptions
        {
            AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
            Provider = cookieAuthProvider,
            CookieName = "VarsityAuth",
        });

        app.UseExternalSignInCookie(DefaultAuthenticationTypes.ExternalCookie);

        app.UseMicrosoftAccountAuthentication(
            clientId: "lkjhlkjkl",
            clientSecret: "kjhjkk");

我需要能够根据租户的每个请求更改这些设置。我该怎么做?

【问题讨论】:

  • 到目前为止,我遇到了障碍。似乎 AuthenticationOptions 的典型实现(例如 FacebookAuthenticationOptions)被标记为内部,并且 Facebook auth 中间件使用 FacebookAuthenticationOptions(而不是接口)。因此,不可能覆盖 FacebookAuthenticationOptions 的 AppId 和 AppSecret 属性,因此我不得不推出自己几乎相同版本的 Facebook Auth 中间件。 :((悲伤的熊猫)
  • 你好,我刚刚问了一个类似的问题,我需要根据租户是谁使用多个 OpenIdConnect 配置。如果您可以分享一些有关如何处理此问题的代码 sn-ps,那就太棒了!供您参考,是我的问题:stackoverflow.com/questions/25417620/…
  • 您好 kingdangoless,请分享您的解决方案。社区将永远感激不尽。

标签: oauth-2.0 asp.net-mvc-5 owin owin-middleware


【解决方案1】:

编辑 - 我现在可以确认此解决方案对我有用。

我正在为我自己的项目调查此问题,该项目需要基于主机名或请求的第一个文件夹段支持多租户,具体取决于配置。

我尚未对此进行测试,但我认为在启动时使用类似这样的代码可能会奏效:

例如,我想为每个租户使用不同的 auth cokie 名称,并且我认为启动时的代码可能会起作用:

// for first folder segment represents the tenant
app.Map("/branch1", app1 =>
{
    app1.UseCookieAuthentication(new CookieAuthenticationOptions
    {
        AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
        LoginPath = new PathString("/Account/Login"),
        Provider = new CookieAuthenticationProvider
       {
            OnValidateIdentity = SecurityStampValidator.OnValidateIdentity<SiteUserManager, SiteUser>(
            validateInterval: TimeSpan.FromMinutes(30),
            regenerateIdentity: (manager, user) => user.GenerateUserIdentityAsync(manager))
    },

        CookieName = "branch1-app"
    });

});

// for when the host name of the request identifies the tenant
app.MapWhen(IsDomain1, app2 =>
{
    app2.UseCookieAuthentication(new CookieAuthenticationOptions
    {
        AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
        LoginPath = new PathString("/Account/Login"),
        Provider = new CookieAuthenticationProvider
        {
            OnValidateIdentity = SecurityStampValidator.OnValidateIdentity<SiteUserManager, SiteUser>(
            validateInterval: TimeSpan.FromMinutes(30),
            regenerateIdentity: (manager, user) => user.GenerateUserIdentityAsync(manager))
        },

        CookieName = "domain1-app"
    });

});

private bool IsDomain1(IOwinContext context)
{
    return (context.Request.Host.Value == "domain1");
}

【讨论】:

    猜你喜欢
    • 2016-11-02
    • 1970-01-01
    • 1970-01-01
    • 2021-08-06
    • 2020-11-27
    • 1970-01-01
    • 2015-08-25
    • 1970-01-01
    • 2023-03-20
    相关资源
    最近更新 更多