Пример #1
0
        public async Task <ActionResult> Login(LoginModel model, string returnUrl = "/")
        {
            LoginVM viewModel = new LoginVM();


            var authent = new AuthenticationInfo {
                UserName = model.UserName, Password = model.Password
            };
            AuthenticationOut authentResult = null;

            try
            {
                authentResult = await accountService.AuthenticationAsync(authent);
            }
            catch (Exception ex)
            {
                string s = ex.Message; //TODO:Лог
            }


            switch (authentResult.Result.Type)
            {
            case ResultRequestType.Success:



                IEnumerable <string> roles = await accountService.AuthorizationAsync(model.UserName);

                if (roles.Count() > 0)
                {
                    var userInfo    = userManagerService.Get(authentResult.UserId);
                    var userProfile = await userProfileService.GetAsync(userInfo.ProfileId);

                    SignIn(authentResult.UserId, $"{model.UserName} {userProfile.FirstName.ToUpper()[0]}.{userProfile.Patronymic.ToUpper()[0]}. ", roles);
                    viewModel.URI         = string.IsNullOrWhiteSpace(returnUrl) ? "/" : returnUrl;
                    viewModel.Result.Type = ResultRequestType.Success;
                }
                else
                {
                    //TODO: лог
                    viewModel.Result.Type    = ResultRequestType.Error;
                    viewModel.Result.Message = $"Пользователь с guid'ом - {authentResult.UserId} не включен не в одну из ролей";
                }



                break;

            case ResultRequestType.Error:
            case ResultRequestType.Warning:
            case ResultRequestType.Info:
                viewModel = mapper.Map <AuthenticationOut, LoginVM>(authentResult);
                break;
            }

            return(RedirectToLocal(returnUrl));
        }
Пример #2
0
        public async Task <AuthenticationOut> AuthenticationAsync(AuthenticationInfo info)
        {
            AuthenticationOut result = new AuthenticationOut();

            try
            {
                using (IDBWorker worker = kernel.Get <IDBWorker>())
                {
                    ApplicationUser user = await worker.UserManager.FindByNameAsync(info.UserName);

                    if (user != null)
                    {
                        bool isCheckSuccess = await worker.UserManager.CheckPasswordAsync(user, info.Password);

                        if (isCheckSuccess)
                        {
                            result.UserId      = user.Id;
                            result.Result.Type = ResultRequestType.Success;
                        }
                        else
                        {
                            result.Result.Type    = ResultRequestType.Error;
                            result.Result.Message = "Не верный логин или пароль";
                        }
                    }
                    else
                    {
                        result.Result.Type    = ResultRequestType.Error;
                        result.Result.Message = "Выберите, пожалуйста, пользователя.";
                    }
                }
            }
            catch (Exception ex)
            {
            }

            return(result);
        }