public void Signin() { var user = new SigninBindingModel { Username = "******", Password = "******" }; var result = _accountService.SigninAsync(user).GetAwaiter().GetResult(); Console.WriteLine(result.Code); Console.WriteLine(result.Data); Assert.IsTrue(result.Code == 200); Assert.IsTrue(result.Data != null); }
public async Task <IActionResult> SigninAsync([FromBody] SigninBindingModel collection) { Log.Debug($"A User is trying to signing in with this data: {JsonConvert.SerializeObject(collection)}"); if (string.IsNullOrEmpty(collection?.Username) || string.IsNullOrEmpty(collection?.Password)) { return(BadRequest(_localizer[DataTransferer.DefectiveUsernameOrPassword().Message])); } var deviceHeader = GetDeviceInfosFromHeader(); if (deviceHeader == null) { return(BadRequest(_localizer[DataTransferer.UnofficialRequest().Message])); } collection.DeviceId = deviceHeader.DeviceId; collection.DeviceName = deviceHeader.DeviceName; collection.DeviceType = deviceHeader.DeviceType; try { var result = await _accountService.SigninAsync(collection).ConfigureAwait(true); switch (result.Code) { case 200: return(Ok(result.Data)); case 400: return(BadRequest(result.Message)); case 500: default: return(Problem(result.Message)); } } catch (Exception ex) { Log.Error(ex, ex.Source); return(Problem(_localizer[DataTransferer.SomethingWentWrong().Message])); } }
public IActionResult Signin(SigninBindingModel model, HttpSession session, HttpResponse response) { using (PizzaMoreContext context = new PizzaMoreContext()) { User currentUser = context.Users.FirstOrDefault(u => u.Password == model.SignInPassword && u.Email == model.SignInEmail); if (currentUser != null) { Session sessionEntity = new Session() { SessionId = session.Id, IsActive = true, User = currentUser }; context.Sessions.Add(sessionEntity); context.SaveChanges(); this.Redirect(response, "/home/index"); } } return(this.View()); }
public async Task <IServiceResult> SigninAsync(SigninBindingModel signinModel) { var now = DateTime.UtcNow; using (var transaction = new TransactionScope(TransactionScopeAsyncFlowOption.Enabled)) { try { var query = new AccountProfileGetFirstSchema { LinkedId = signinModel.Username //StatusId = Status.Active }; if (signinModel.Username.IsPhoneNumber()) { query.TypeId = AccountProfileType.Phone.ToInt(); } else if (new EmailAddressAttribute().IsValid(signinModel.Username)) { query.TypeId = AccountProfileType.Email.ToInt(); } else { return(DataTransferer.InvalidEmailOrCellPhone()); } var accountProfile = await _accountProfileService.FirstAsync(query).ConfigureAwait(true); if (accountProfile == null) { if (query.TypeId == AccountProfileType.Phone.ToInt()) { return(DataTransferer.PhoneNotFound()); } else { return(DataTransferer.EmailNotFound()); } } var account = await FirstAsync(new AccountGetFirstSchema { Id = accountProfile.AccountId, StatusId = Status.Active.ToInt() }).ConfigureAwait(true); if (account == null) { return(DataTransferer.UserIsNotActive()); } var deviceId = 0; // check password if (_cryptograph.IsEqual(signinModel.Password, account.Password)) { var accountDeviceQuery = new AccountDeviceGetFirstSchema { AccountId = account.Id, DeviceId = signinModel.DeviceId }; var accountDevice = await _accountDeviceService.FirstAsync(accountDeviceQuery); // todo: check the accountDevice if (accountDevice != null) { deviceId = accountDevice.Id.Value; // set new token await _accountDeviceService.UpdateAsync(new AccountDeviceUpdateSchema { Id = accountDevice.Id.Value, }); } else { // create new device for account deviceId = await _accountDeviceService.AddAsync(new AccountDeviceAddSchema { AccountId = account.Id, DeviceId = signinModel.DeviceId, DeviceName = signinModel.DeviceName, DeviceType = signinModel.DeviceType, CreatedAt = now, StatusId = Status.Active.ToInt() }); } } else { return(DataTransferer.WrongPassword()); } // clean forgot password tokens await _accountProfileService.CleanForgotPasswordTokensAsync(account.Id.Value); //if(accountProfilesQuery.StatusCode != 200) { // Log.Error($"Can't update 'ForgotPasswordTokens' to NULL for AccountId={account.Id}"); // return DataTransferer.SomethingWentWrong(); //} // set last signed in at var changedAccount = UpdateAsync(new AccountUpdateSchema { LastSignedinAt = now }); transaction.Complete(); var token = _jwtHandler.Bearer(new Account(account.Id.Value, deviceId, signinModel.Username, now).ToClaimsIdentity()); return(DataTransferer.Ok(token)); } catch (Exception ex) { Log.Error(ex, ex.Source); return(DataTransferer.InternalServerError()); } } }