TL;DR
Azure Active Directory B2Cでマルチテナントサンプルを動かしてみた(1) 続き。
まだまだ設定が残っているので少しずつ書き換える。
Reset Password
サインインをしようとして、おもむろに Forgot your password? をクリックすると、404エラーになります。demoサイトではTOPに遷移するだけなので、とりま付け足した機能なのかもしれません。
'/' アプリケーションでサーバー エラーが発生しました。
リソースが見つかりませんでした。
説明: HTTP 404. 探しているリソース (または、その依存関係の 1 つ) が削除されたか、名前が変更されたか、また一時的に使用できません。以下の URL のスペルが正しいことを確認してください。
要求された URL: /Account/ResetPassword
バージョン情報: Microsoft .NET Framework バージョン:4.0.30319; ASP.NET バージョン:4.7.2633.0
Startup.Auth.cs では認証失敗時に AuthenticationFailed メソッドへ遷移するよう設定されていますが、リダイレクトURLが /Account/ResetPassword になっているのが原因です。(きっと**/Home/Error?message=** の部分もエラーになるような気がします・・)
// Used for avoiding yellow-screen-of-death
private Task AuthenticationFailed(AuthenticationFailedNotification<OpenIdConnectMessage, OpenIdConnectAuthenticationOptions> notification)
{
notification.HandleResponse();
// Handle the error code that Azure AD B2C throws when trying to reset a password from the login page
// because password reset is not supported by a "sign-up or sign-in policy"
if (notification.ProtocolMessage.ErrorDescription != null && notification.ProtocolMessage.ErrorDescription.Contains("AADB2C90118"))
{
// If the user clicked the reset password link, redirect to the reset password route
notification.Response.Redirect("/Account/ResetPassword");
}
else if (notification.Exception.Message == "access_denied")
{
notification.Response.Redirect("/");
}
else
{
notification.Response.Redirect("/Home/Error?message=" + notification.Exception.Message);
}
return Task.FromResult(0);
}
で、なぜこれがダメなのかというと、RouteConfig.cs に以下のような実装がされていることに依ります。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
namespace B2CMultiTenant
{
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Default",
url: "{ttype}/{controller}/{action}/{id}",
defaults: new { ttype = "b2c", controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
}
}
b2c/Account/ResetPassword じゃないとダメ、と・・。
notification.Response.Redirect("b2c/Account/ResetPassword");
お。上手くResetPassword() も通過して上手くいったようですね!
雑感
URLのハードコーティングは好きじゃないので、なんとかしたいところですね。
で、リダイレクトされるとmicrosoftonline.comになっちゃって、アカウントの回復どころじゃない・・ってなるんですが、これについてはまた書きます。
たぶん、Web.config でaad:ClientId
とかaad:ClientSecret
とかを設定してないからではないかな?と邪推してます。が、疲れたのでごめんなさい。
以上。