我有一个 Multi-Tenancy 应用程序。每个租户都可以使用OAUTH-2与Facebook,Twitter,Google等对用户进行身份验证。每个租户都具有针对上述服务的自己的API key 。

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

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

        app.UseExternalSignInCookie(DefaultAuthenticationTypes.ExternalCookie);

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

我需要能够根据租户的请求更改这些设置。我怎样才能做到这一点?

最佳答案

编辑-现在,我可以确认此解决方案对我而言有效。

我正在为自己的项目调查此问题,该项目需要根据配置的主机名或请求的第一个文件夹段支持 Multi-Tenancy 。

我尚未对此进行测试,但我正在考虑在启动时使用类似的代码来解决问题:

例如,我想为每个租户使用一个不同的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");
}

关于oauth-2.0 - 更改每个请求的OWIN Auth中间件(每个租户, Multi-Tenancy ,oauth API key ),我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/25393234/

10-11 22:53
查看更多