public async Task <ResponseModel <LoginModel> > Login(string userName, string password, string uri, bool isPersistent = false, bool lockoutOnFailure = false)
        {
            var result = await _signInManager.PasswordSignInAsync(userName, password, isPersistent, lockoutOnFailure);

            var responseModel = new ResponseModel <LoginModel>(result.Succeeded);

            if (!result.Succeeded)
            {
                responseModel.AddError(AppErrors.PasswordOrUserIncorrect);
                return(responseModel);
            }

            var user = await _accountRepository.Get(userName);

            var loginModel = new LoginModel(user.Email);

            responseModel.Data = loginModel;

            if (!await _accountRepository.IsEmailConfirmedAsync(userName))
            {
                responseModel.Success = false;
                responseModel.AddError(AppErrors.EmailNotConfirmed);

                return(responseModel);
            }

            var token = BuildToken(user.UserName, user.Email);

            responseModel.Data.AddToken(token);
            return(responseModel);
        }
        public async Task <ResponseModel <LoginModel> > ResetPassword(string email, string password, string confirmPassword,
                                                                      string token)
        {
            var user = await _accountRepository.GetByEmail(email);

            var loginModel = new LoginModel(email);
            var response   = new ResponseModel <LoginModel>(loginModel);

            if (user == null)
            {
                response.AddError(AppErrors.PasswordOrUserIncorrect);
                return(response);
            }

            var result = await _accountRepository.ResetPassword(user, token, password);

            if (!result.Succeeded)
            {
                foreach (var error in result.Errors)
                {
                    response.AddError(new ErrorResponseModel(error.Code, error.Description));
                }

                return(response);
            }

            await _emailClientSender.SendConfirmationResetPassword(user.Email, new SendResetPasswordModel(user.UserName));

            response.Success = true;
            return(response);
        }
        public async Task <ResponseModel <LoginModel> > GetPasswordResetToken(string email)
        {
            var user = await _accountRepository.GetByEmail(email);

            var loginModel = new LoginModel(email);
            var response   = new ResponseModel <LoginModel>(loginModel);

            if (user == null)
            {
                response.AddError(AppErrors.PasswordOrUserIncorrect);
                return(response);
            }

            var token = await _accountRepository.GeneratePasswordResetTokenAsync(user);

            var api       = "/ConfirmationRegisterMail";
            var webAppUrl = "https://cienciaargentina.com";
            var url       = $"www.MATIASCODEAESTO={user.Email}&token={token}";

            url = url.Replace("+", "%2B");

            await _emailClientSender.SendGetPasswordResetToken(email, new SendGetPasswordResetTokenModel(user.UserName, url));

            return(response);
        }
Esempio n. 4
0
        public async Task <ResponseModel <AccountModelApi <int> > > GetUserByEmailAsync(String email)
        {
            var accByEmail = await this._redisCachingService.GetItemAsync <AccountModelBussines <int> >(
                RedisCachingKeysUtil.GET_ACCOUNT_BY_EMAIL_KEY + email
                );

            if (accByEmail == null)
            {
                accByEmail = await this._accountRepository.GetByEmailAsync(email);

                await this._redisCachingService.PutItemAsync <AccountModelBussines <int> >(
                    RedisCachingKeysUtil.GET_ACCOUNT_BY_EMAIL_KEY + email,
                    accByEmail,
                    new DistributedCacheEntryOptions()
                {
                    AbsoluteExpirationRelativeToNow = TimeSpan.FromDays(3),
                    SlidingExpiration = TimeSpan.FromDays(1)
                }
                    );
            }

            ResponseModel <AccountModelApi <int> > resp = new ResponseModel <AccountModelApi <int> >(
                AutoMapperConfig.Mapper.Map <AccountModelApi <int> >(accByEmail)
                );

            if (accByEmail == null)
            {
                resp.AddError(new Error($"Item with email {email} not found."));
            }

            return(resp);
        }
        public async Task <ResponseModel <AuthTokenResponseModel> > GetTokenResponseAsync(LoginModelApi loginApiModel)
        {
            var accountApiModel = await accountService.GetByEmailAsync(loginApiModel.Email);

            var respModel = new ResponseModel <AuthTokenResponseModel>();

            if (accountApiModel == null || !await accountService.VerifyUserAsync(loginApiModel))
            {
                respModel.AddError(new Error($"Email or password is incorrect..."));
                return(respModel);
            }

            var claims = GenerateClaims(accountApiModel);

            var jwt = new JwtSecurityToken(
                issuer: this.authJWToptions.Value.Issuer,
                audience: this.authJWToptions.Value.Audience,
                notBefore: DateTime.UtcNow,
                claims: claims.Claims,
                expires: DateTime.UtcNow.Add(TimeSpan.FromDays(this.authJWToptions.Value.Lifetime)),
                signingCredentials: new SigningCredentials(this.GetSymmetricSecurityKey(), SecurityAlgorithms.HmacSha256));

            var tokenRespModel = new AuthTokenResponseModel()
            {
                Token  = new JwtSecurityTokenHandler().WriteToken(jwt),
                UserId = Convert.ToInt32(claims.Name)
            };

            respModel.Item = tokenRespModel;

            return(respModel);
        }
Esempio n. 6
0
        public async Task <ResponseModel <ICollection <AccountModelApi <int> > > > GetAllAsync()
        {
            var allAccs = await this._redisCachingService.GetItemAsync <ICollection <AccountModelBussines <int> > >(
                RedisCachingKeysUtil.GET_ALL_ACCOUNTS_KEY
                );

            if (allAccs == null)
            {
                allAccs = await this._accountRepository.GetAllAsync();

                await this._redisCachingService.PutItemAsync <ICollection <AccountModelBussines <int> > >(
                    RedisCachingKeysUtil.GET_ALL_ACCOUNTS_KEY,
                    allAccs,
                    new DistributedCacheEntryOptions()
                {
                    AbsoluteExpirationRelativeToNow = TimeSpan.FromMinutes(3)
                }
                    );
            }

            ResponseModel <ICollection <AccountModelApi <int> > > resp = new ResponseModel <ICollection <AccountModelApi <int> > >(
                AutoMapperConfig.Mapper.Map <ICollection <AccountModelApi <int> > >(allAccs)
                );

            if (allAccs == null)
            {
                resp.AddError(new Error($"Db error."));
            }

            return(resp);
        }
        public async Task <ResponseModel <List <ImportFile> > > SearchTemplates(List <string> headerNames)
        {
            var retResp = new ResponseModel <List <ImportFile> >();

            try {
                var resp = await httpClient.PostAsJsonAsync("api/Templates/Search", headerNames);

                if (resp.IsSuccessStatusCode)
                {
                    return(await resp.Content.ReadFromJsonAsync <ResponseModel <List <ImportFile> > >());
                }
                retResp.AddError("The request was not successful.");
            } catch (Exception) {
                retResp.AddError("There was an error that occured while trying to save .");
            }
            return(retResp);
        }
Esempio n. 8
0
        private ResponseModel SetOfError(IdentityResult res)
        {
            var response = new ResponseModel();

            foreach (IdentityError err in res.Errors)
            {
                response.AddError(err.Code, err.Description);
            }
            return(response);
        }
Esempio n. 9
0
        public async Task <ActionResult> GetUserByID(string id)
        {
            var user = await _userManager.FindByIdAsync(id);

            if (user != null)
            {
                return(Ok(user));
            }
            var response = new ResponseModel();

            response.AddError(ErrorConstant.NoContentCode, ErrorConstant.NoContentMess);
            return(Ok(response));
        }
        public async Task <ResponseModel <LoginModel> > SendConfirmationRegisterMail(string email, string uri)
        {
            var user = await _accountRepository.GetByEmail(email);

            var loginModel = new LoginModel(email);
            var response   = new ResponseModel <LoginModel>(loginModel);

            if (user != null)
            {
                return(await SendEmailConfirmationAsync(user, uri));
            }

            response.AddError(AppErrors.PasswordOrUserIncorrect);
            return(response);
        }
        public async Task <ResponseModel <LoginModel> > ForgotUsername(string email)
        {
            var user = await _accountRepository.GetByEmail(email);

            var responseModel = new ResponseModel <LoginModel>(user != null);

            if (!responseModel.Success)
            {
                responseModel.AddError(AppErrors.PasswordOrUserIncorrect);
                return(responseModel);
            }

            await _emailClientSender.SendForgotUser(user.Email, new SendForgotUserModel(user.UserName));

            return(responseModel);
        }
        public ResponseModel <UserModelApi> SignUp(UserModelApi model)
        {
            var response = new ResponseModel <UserModelApi>();

            var res = _service.Create(model);

            if (res != null)
            {
                response.Item = res;
            }
            else
            {
                response.AddError("User wtih this e-mail and login already exists");
            }

            return(response);
        }
        public ResponseModel <UserModelApi> SignIn(UserModelApi model)
        {
            var response = new ResponseModel <UserModelApi>();

            var res = _service.VerifyUser(model.Email, model.Password);

            if (res != null)
            {
                response.Item = res;
                FormsAuthentication.SetAuthCookie(model.Email, true);
            }
            else
            {
                response.AddError("User wtih this e-mail and login abscent");
            }

            return(response);
        }
Esempio n. 14
0
        public async Task <ResponseModel <SharedFolderModelApi <int> > > ShareFolder(SharedFolderModelApi <int> sharingModel)
        {
            var acc = await this._accountRepository.GetByEmailAsync(sharingModel.AccountEmail);

            var resp = new ResponseModel <SharedFolderModelApi <int> >();

            if (acc == null)
            {
                resp.AddError(new Error($"This user not exists"));
                return(resp);
            }

            await this._redisCachingService.RemoveItemAsync(RedisCachingKeysUtil.GET_SHARED_FOLDERS_BY_USER_ID + acc.Id);

            var folder = await this._storedFolderRepository.GetByIdAsync(sharingModel.StoredFolderId);

            await NotifyAboutSharing(folder.Title, acc.Id);

            resp.Item = AutoMapperConfig.Mapper.Map <SharedFolderModelApi <int> >(await CreateSharing(sharingModel, acc));

            return(resp);
        }
        public async Task <ResponseModel <LoginModel> > SendEmailConfirmationAsync(ApplicationUser user, string uri)
        {
            var responseModel = new ResponseModel <LoginModel>(new LoginModel(user.Email));

            if (await _accountRepository.IsEmailConfirmedAsync(user))
            {
                responseModel.AddError(AppErrors.EmailIsConfirmed);
                return(responseModel);
            }

            var tokenConfirmation = await _accountRepository.GenerateEmailConfirmationTokenAsync(user);

            var api       = "/ConfirmationRegisterMail";
            var webAppUrl = "https://cienciaargentina.com";
            var url       = $"{webAppUrl}{api}/ConfirmationRegisterMail?email={user.Email}&token={tokenConfirmation}";

            url = url.Replace("+", "%2B");
            var sendConfirmationModel = new SendConfirmationAccountModel(user.UserName, "", url);
            await _emailClientSender.SendConfirmationAccountEmail(user.Email, sendConfirmationModel);

            responseModel.Success = true;
            return(responseModel);
        }
Esempio n. 16
0
        public async Task <ResponseModel <AccountModelApi <int> > > RegisterUserAsync(AccountModelApi <int> model)
        {
            var registeredUser = await this._accountRepository.CreateAsync(
                AutoMapperConfig.Mapper.Map <AccountModelBussines <int> >(model)
                );

            ResponseModel <AccountModelApi <int> > resp = new ResponseModel <AccountModelApi <int> >(
                AutoMapperConfig.Mapper.Map <AccountModelApi <int> >(registeredUser)
                );

            if (registeredUser == null)
            {
                resp.AddError(new Error($"User with {model.Email} alredy exists."));
                return(resp);
            }

            await this._redisCachingService.RemoveItemAsync(RedisCachingKeysUtil.GET_ACCOUNT_BY_EMAIL_KEY);

            await this._accountSubscriptionService.SubscribeUserByIdDefaultAsync(registeredUser.Id);

            //ToDo : make crypto

            return(resp);
        }
        public async Task <ResponseModel <LoginModel> > Add(ApplicationUser user, string password, string uri)
        {
            var result = await _accountRepository.Add(user, password);

            var loginModel = new LoginModel(user.Email);
            var response   = new ResponseModel <LoginModel>(loginModel, result.Succeeded);

            if (!result.Succeeded)
            {
                foreach (var error in result.Errors)
                {
                    response.AddError(new ErrorResponseModel(error.Code, error.Description));
                }

                return(response);
            }

            var token = BuildToken(user.UserName, user.Email);

            response.Data.AddToken(token);
            await SendEmailConfirmationAsync(user, uri);

            return(response);
        }