首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >根据每个请求更改oauth中间件(多租户,每个租户的oauth密钥)

根据每个请求更改oauth中间件(多租户,每个租户的oauth密钥)
EN

Stack Overflow用户
提问于 2014-08-19 21:25:38
回答 1查看 2.6K关注 0票数 10

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

安装OWIN管道的典型方法是在启动时“使用”auth提供程序,但这会在app启动时设置API键。我需要能够更改每个请求与每个oauth一起使用的键。

代码语言:javascript
复制
        app.UseCookieAuthentication(new CookieAuthenticationOptions
        {
            AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
            Provider = cookieAuthProvider,
            CookieName = "VarsityAuth",
        });

        app.UseExternalSignInCookie(DefaultAuthenticationTypes.ExternalCookie);

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

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

EN

回答 1

Stack Overflow用户

发布于 2014-10-23 18:05:49

编辑-我现在可以确认这个解决方案正在为我工作。

我正在为我自己的项目研究这个问题,它需要根据主机名或请求的第一个文件夹段(取决于配置)支持多个租户。

我还没有对其进行测试,但我认为在启动时这样的代码可能会奏效:

例如,我想为每个租户使用一个不同的auth cokie名称,并且我认为在启动时使用类似这样的代码是可行的:

代码语言:javascript
复制
// 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"
    });

});

代码语言:javascript
复制
private bool IsDomain1(IOwinContext context)
{
    return (context.Request.Host.Value == "domain1");
}
票数 9
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/25393234

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档