123.onmicrosoft.com
2 つのテナントをセットアップしましたabc.onmicrosoft.com
。
OpenIdConnectAuthenticationMiddleware
OpenID Connect 認証フローを実行するために使用する Web アプリケーションを開発しました。
すぐに使えるミドルウェアでは、OWIN ミドルウェアの構成中にクライアント ID を渡す必要があります。これが私たちの課題です。この Web アプリケーションは、 と の 2 つのバインディングを使用して 1 回デプロイされ123.mydomain.com
ますabc.mydomain.com
。
実行時にクライアント ID を設定します。私たちの現在のアプローチは、サブクラスを開発し、実行時にクライアント ID を収集する関数を挿入することでした。次のようなものです。
public class LendingMiddleware : OpenIdConnectAuthenticationMiddleware
{
private Action<IOwinContext, OpenIdConnectAuthenticationOptions> _optionBuilder = null;
public LendingMiddleware(OwinMiddleware next, IAppBuilder app,
OpenIdConnectAuthenticationOptions options,
Action<IOwinContext, OpenIdConnectAuthenticationOptions> optionBuilder = null) : base(next, app, options)
{
_optionBuilder = optionBuilder;
}
public LendingMiddleware(OwinMiddleware next, IAppBuilder app, OpenIdConnectAuthenticationOptions options) : base(next, app, options)
{
}
public override async Task Invoke(IOwinContext context)
{
_optionBuilder?.Invoke(context, Options);
await base.Invoke(context);
}
}
クラスの起動時に、次の 2 つのミドルウェアを追加します。 UseTenantVerification はホスト ヘッダーを読み取り、このテナントに関連付けられたアプリケーション構成を読み込みます。LendingAuthentication は LendingMiddleware を使用して openid フローを実行し、Lending Middleware は OpenIdConnectAuthenticationMiddleware から継承されます
public void ConfigureAuth(IAppBuilder app)
{
//fixed address for multitenant apps in the public cloud
string authority = "https://login.microsoftonline.com/common/";
app.SetDefaultSignInAsAuthenticationType(CookieAuthenticationDefaults.AuthenticationType);
app.UseCookieAuthentication(new CookieAuthenticationOptions { });
app.UseTenantVerification();
app.UseLendingAuthentication(CreateOptions(authority), (ctx, options) =>
{
TenantConfig tc = ctx.Environment["TenantInfo"] as TenantConfig;
options.ClientId = tc.ClientId;
options.TokenValidationParameters.ValidAudience = tc.ClientId;
//ctx.ProtocolMessage.Password = tenantConfig.Secret;
});
}
TenantConfig
次のようになります。
public TenantConfig GetTenantConfig(string httpHost)
{
IDictionary<string, TenantConfig> Tenants = new Dictionary<string, TenantConfig>();
Tenants.Add(new KeyValuePair<string, TenantConfig>("123.local", new TenantConfig
{
ClientId = "90677fe0-412c-4625-8d45-f37c14ffb456",
Secret = "[secret!]",
HostName = "123.local",
ApiResources = new Dictionary<string, ApiResourceConfig>
{
{
"TodoWebApi", new ApiResourceConfig
{
ResourceIdentifier = "https://123.onmicrosoft.com/TodoWebApiMT",
ResourceAddress = "http://TodoWebApiMT.local"
}
}
}
}));
Tenants.Add(new KeyValuePair<string, TenantConfig>("abc.local", new TenantConfig
{
ClientId = "000309f7-ac34-4fb6-a833-ef7c664e0958",
Secret = "[secret!]",
HostName = "abc.local",
ApiResources = new Dictionary<string, ApiResourceConfig>
{
{
"TodoWebApi", new ApiResourceConfig
{
ResourceIdentifier = "https://abc.onmicrosoft.com/TodoWebApiMT",
ResourceAddress = "http://TodoWebApiMT.local"
}
}
}
}));
var k = Tenants.Keys.First(item => item.Equals(httpHost));
return Tenants[k];
}
このテナントがアクセスするクライアント ID、アプリ キー、および API リソースを保持します。今のところ API アプリは 1 つしかありませんが、最終的にはいくつかの API アプリをテナント ディレクトリに追加してアクセスを許可する予定です。
これを行うより良い方法はありますか?