Beispiel #1
0
        public async Task <IActionResult> Logon(LogonViewModel model)
        {
            if (ModelState.IsValid)
            {
                // Authenticate first
                var authenticated = _externalAuthenticationProvider.Authenticate(model.UserName, model.Password);

                if (authenticated)
                {
                    var user = await _memberService.GetUserByUsernameAsync(model.UserName);

                    if (user != null)
                    {
                        if (string.IsNullOrWhiteSpace(user.Name) || string.IsNullOrWhiteSpace(user.EMail))
                        {
                            // Not Registered properly
                            await _signinManager.SignInAsync(UserState.FromUser(user));

                            return(RedirectToAction("Index", "Member"));
                        }
                        // Registered properly
                        await _signinManager.SignInAsync(UserState.FromUser(user));

                        return(RedirectToAction("Index", "Home"));
                    }
                    var result = await _memberService.RegisterNewUserAsync(model.UserName);

                    if (result == 1)
                    {
                        user = _memberService.GetUserByUsernameAsync(model.UserName).GetAwaiter().GetResult();
                        await _signinManager.SignInAsync(UserState.FromUser(user));

                        return(RedirectToAction("Index", "Member"));
                    }
                    // Could't register, might try again.
                    ModelState.AddModelError(Constants.ModelStateCustomErrorKey, RegisteringNewUserFailureMessage);
                }
                else
                {
                    _logger.Warn($"Authentication failure for user {model.UserName}!");
                    ModelState.AddModelError(Constants.ModelStateCustomErrorKey, InvalidLogonMessage);
                }
            }
            model = new LogonViewModel
            {
                UserName = model.UserName
            };
            return(View(model));
        }