Oauth 2.0 每个请求更改OWIN身份验证中间件(多租户、每个租户的oauth API密钥)
我有一个多租户应用程序。每个租户都可以使用OAUTH-2通过Facebook、Twitter、Google等对其用户进行身份验证。每个租户都有自己的API密钥用于上述服务 设置OWIN管道的典型方法是在启动时“使用”身份验证提供程序,但这会在应用程序启动时设置API键。我需要能够为每个请求更改每个oauth API使用的密钥Oauth 2.0 每个请求更改OWIN身份验证中间件(多租户、每个租户的oauth API密钥),oauth-2.0,asp.net-mvc-5,owin,owin-middleware,Oauth 2.0,Asp.net Mvc 5,Owin,Owin Middleware,我有一个多租户应用程序。每个租户都可以使用OAUTH-2通过Facebook、Twitter、Google等对其用户进行身份验证。每个租户都有自己的API密钥用于上述服务 设置OWIN管道的典型方法是在启动时“使用”身份验证提供程序,但这会在应用程序启动时设置API键。我需要能够为每个请求更改每个oauth API使用的密钥 app.UseCookieAuthentication(new CookieAuthenticationOptions {
app.UseCookieAuthentication(new CookieAuthenticationOptions
{
AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
Provider = cookieAuthProvider,
CookieName = "VarsityAuth",
});
app.UseExternalSignInCookie(DefaultAuthenticationTypes.ExternalCookie);
app.UseMicrosoftAccountAuthentication(
clientId: "lkjhlkjkl",
clientSecret: "kjhjkk");
我需要能够根据租户更改每个请求的这些设置。我如何才能做到这一点?编辑-我现在可以确认此解决方案对我有效 我正在为我自己的项目调查这个问题,该项目需要根据主机名或请求的第一个文件夹段(取决于配置)支持多租户 我还没有对此进行测试,但我认为在startup中类似这样的代码可能会起到作用: 例如,我想为每个租户使用不同的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"
});
到目前为止,我遇到了一个障碍。AuthenticationOptions(例如FacebookAuthenticationOptions)的典型实现似乎标记为内部,Facebook auth中间件使用FacebookAuthenticationOptions(而不是接口)。因此,无法覆盖FacebookAuthenticationOptions的AppId和AppSecret属性,因此我不得不推出自己几乎相同版本的Facebook Auth中间件:((悲伤的熊猫)您好,我刚刚问了一个类似的问题,根据租户的身份,我需要在哪里使用多个OpenIdConnect配置。如果您能分享一些关于如何处理此问题的代码片段,那将非常棒!供您参考,我的问题是:嗨,kingdangoless,请分享您的解决方案。社区将永远感激您。
private bool IsDomain1(IOwinContext context)
{
return (context.Request.Host.Value == "domain1");
}