2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Azure Active Directory B2Cでマルチテナントサンプルを動かしてみた(2)

Posted at

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=** の部分もエラーになるような気がします・・)

Startup.Auth.cs
        // 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 に以下のような実装がされていることに依ります。

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.configaad:ClientIdとかaad:ClientSecretとかを設定してないからではないかな?と邪推してます。が、疲れたのでごめんなさい。

以上。

2
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?