Ejemplo n.º 1
0
        public async Task <IActionResult> Index(UserProfileViewModel model)
        {
            var userState = _userStateWrapper.GetUserState();

            if (userState == null || userState.Id == 0)
            {
                return(RedirectToAction("Logon", "Member"));
            }

            if (ModelState.IsValid)
            {
                if (model.Id != userState.Id)
                {
                    ModelState.AddModelError(Constants.ModelStateCustomErrorKey, NotMachingUserId);
                }
                else
                {
                    var user = await _memberService.GetUserByIdAsync(userState.Id);

                    user = model.ChangeUser(user);
                    // TODO: Check result
                    var result = await _memberService.UpdateUserAsync(user);

                    await _signinManager.SignOffAsync();

                    await _signinManager.SignInAsync(UserState.FromUser(user));

                    model.FromUser(user);
                }
            }
            return(View(model));
        }
Ejemplo n.º 2
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));
        }