コード例 #1
0
        public void Login(string userId, string pwd, string messageServiceType)
        {
            IMessageService msgService = null;

            switch (messageServiceType)
            {
            case "EmailService":
                msgService = new EmailService();
                break;

            case "ShortMessageService":
                msgService = new ShortMessageService();
                break;

            default:
                throw new ArgumentException("Unknown message service type!");
            }

            var authService = new AuthenticationService(msgService);  // 於建立物件時注入相依性

            if (authService.TwoFactorLogin(userId, pwd))
            {
                // 請使用者收信,然後回來輸入信中提示的驗證碼。
                string userInputToken = "123456";
                if (authService.VerifyToken(userInputToken))
                {
                    // 登入成功。
                }
            }
            // 登入失敗。
        }
コード例 #2
0
ファイル: MainApp.cs プロジェクト: huanlin/di-book-support
        public void Login(string userId, string pwd, string messageServiceType) 
        {
            IMessageService msgService = null;
            switch (messageServiceType)
            {
                case "EmailService":
                    msgService = new EmailService();
                    break;
                case "ShortMessageService":
                    msgService = new ShortMessageService();
                    break;
                default:
                    throw new ArgumentException("Unknown message service type!");
            }

            var authService = new AuthenticationService(msgService);  // 於建立物件時注入相依性
            if (authService.TwoFactorLogin(userId, pwd))  
            {
                // 請使用者收信,然後回來輸入信中提示的驗證碼。
                string userInputToken = "123456";
                if (authService.VerifyToken(userInputToken))
                {
                    // 登入成功。
                }
            }
            // 登入失敗。
        }