AzureADB2BからB2Cへの移行作業


以前、こちらで構築した環境ですが、
AzureADB2Cへ対応するよう構築しなおしてみます。

プログラムの変更点

MS公式サンプルそのままですが、

もとのプログラム.cs
 public class Startup
    {
        private static readonly string clientId = ConfigurationManager.AppSettings["ida:ClientId"];
        private static readonly string aadInstance = EnsureTrailingSlash(ConfigurationManager.AppSettings["ida:AADInstance"]);
        private static readonly string tenantId = ConfigurationManager.AppSettings["ida:TenantId"];
        private static readonly string postLogoutRedirectUri = ConfigurationManager.AppSettings["ida:PostLogoutRedirectUri"];
        private static readonly string authority = aadInstance + tenantId;

        public void Configuration(IAppBuilder app)
        {
            app.SetDefaultSignInAsAuthenticationType(CookieAuthenticationDefaults.AuthenticationType);
            app.UseCookieAuthentication(new CookieAuthenticationOptions(){});
            app.UseOpenIdConnectAuthentication(
                new OpenIdConnectAuthenticationOptions
                {
                    ClientId = clientId,
                    Authority = authority,
                    AuthenticationMode = AuthenticationMode.Active,
                    PostLogoutRedirectUri = postLogoutRedirectUri
                });
        }

書き換えたプログラム.cs

        // App config settings
        private static readonly string ClientId = ConfigurationManager.AppSettings["ida:ClientId"];
        private static readonly string AadInstance = ConfigurationManager.AppSettings["ida:AadInstance"];
        private static readonly string Tenant = ConfigurationManager.AppSettings["ida:Tenant"];
        private static readonly string RedirectUri = ConfigurationManager.AppSettings["ida:RedirectUri"];
        // B2C policy identifiers
        public static readonly string SignUpSignInPolicyId = ConfigurationManager.AppSettings["ida:SignUpSignInPolicyId"];

        public void Configuration(IAppBuilder app)
        {
            app.SetDefaultSignInAsAuthenticationType(CookieAuthenticationDefaults.AuthenticationType);
            app.UseCookieAuthentication(new CookieAuthenticationOptions(){});
            app.UseOpenIdConnectAuthentication(
                new OpenIdConnectAuthenticationOptions
                {
                    MetadataAddress = String.Format(AadInstance, Tenant, SignUpSignInPolicyId),
                    ClientId = ClientId,
                    RedirectUri = RedirectUri,
                    PostLogoutRedirectUri = RedirectUri,
                });
        }

もとのweb.config

    <add key="ida:AADInstance" value="https://login.microsoftonline.com/" />
    <add key="ida:ClientId" value="yyy-yyy-yyy-yyy-yyyyyyyyy" />
    <add key="ida:TenantId" value="xxx-xxx-xxx-xxx-xxxxxxxxx" />
    <add key="ida:PostLogoutRedirectUri" value="https://localhost:12345/" />

書き換えたweb.config

    <add key="ida:Tenant" value="AADB2CName.onmicrosoft.com" />
    <add key="ida:TenantId" value="xxx-xxx-xxx-xxx-xxxxxxxxxx" />
    <add key="ida:AadInstance" value="https://AADB2CName.b2clogin.com/tfp/{0}/{1}/v2.0/.well-known/openid-configuration" />
    <add key="ida:SignUpSignInPolicyId" value="b2c_1_loginonly" />
    <add key="ida:ClientId" value="aaaa-aaaa-aaaa-aaaa-aaaaaaaaaa" />
    <add key="ida:PostLogoutRedirectUri" value="https://localhost:1234/" />

以上です、
これはデバッグ用のconfigです。
AppService単体の環境またはAGW+AppServiceの環境では
PostLogoutRedirectUriはAppServiceのドメインの時のものを入れておきます。

※ここでハマったのはPostLogoutRedirectUriに以前のものと同様にAGWのドメインのURLを入れていて通常認証に成功するとログイン画面からPostLogoutRedirectUriにリダイレクトしPostLogoutRedirectUriからすぐに302リダイレクトでログイン画面呼び出し時にセットするcallbackURLに行くのですがログイン画面から302リダイレクトしなくてずっと先に進みませんでした。

AppService

AppServiceは特に気にせず作ります

AGW

ここではStandardV2で構築してみましたがWAFV2でも一緒です。

バックエンドプール

トップページ用に空っぽのものとパスベースルーティングで使用するAppServiceをせていします。

ここで特殊なのはAGWとAppServiceのサブスクリプションが違うのでAppServiceのドメインを直接指定しています。

HTTPSetting

まずはトップページ用

ここは適当です。

AppServiceのところ

名前は見えていませんがわかりやすい名前を設定しましょう

ここで大事なのは、バックエンドにパスをオーバーライドするの設定の部分です。
何かしらのパスが設定されているとAzureADB2C側で認証できなくなってしまうので/を設定してAppServiceへはルーティングパスや任意のパスを渡さないようにします。
以前はルーティングパスをオーバーライドしていたので、AppService側のURLRewriteでルーティングパスへアクセスしたものを該当アプリケーションにrewriteするという設定をしていて認証がうまくいかなかったので注意が必要です。

フロントエンドIPの設定

リスナー

ルール

は前回から変更はないと思います。

リライトの設定

上記ルーティングパスの設定と以前はAppService側のWebconfigに設定していた、
PostLogoutRedirectUriに設定する値がAGWのURLからAppServiceのURLをセットするよう変更したので、

Pattern to matchが、

condition側は、

(.)redirect_uri=https%3A%2F%2FXXXXXXXX.azurewebsites.net(.)$

Action側は、

{http_resp_Location_1}redirect_uri=https%3A%2F%2FXXXXX.japaneast.cloudapp.azure.com%2ルーティングパス{http_resp_Location_2}
に変更しました。

終わり

設定としては以上です。
これで以前までと同様に認証できるようになると思います。