public async Task <IActionResult> Post([FromBody] LoginModel model) { if (model is null) { return(HandleUnprocessableEntity()); } var result = await _accountService.Login(model.NickName, model.Password); if (!result.IsValid) { return(HandleUnauthorized(result.Notifications.First())); } await _remainingBalanceService.Recalculate(result.Data.Id, DateTime.Now); var claims = new Dictionary <string, string>(); claims.Add(ClaimTypes.Sid, result.Data.Id.ToString()); var token = new AccountResultModel() { Id = result.Data.Id, NickName = result.Data.NickName, Token = new JwtTokenBuilder(_config.SecretJwtKey, _config.CookieExpiresInMinutes, claims).Build().Value }; return(Ok(new ResultDataModel <AccountResultModel>() { Data = token })); }
public async Task <IActionResult> Post([FromBody] AccountModel model) { if (model is null) { return(HandleUnprocessableEntity()); } var result = await _service.Add(model.Map <AccountModel, User>()); if (result.IsValid) { var claims = new Dictionary <string, string>(); claims.Add(ClaimTypes.Sid, result.Data.Id.ToString()); var token = new AccountResultModel() { Id = result.Data.Id, NickName = result.Data.NickName, Token = new JwtTokenBuilder(_config.SecretJwtKey, _config.CookieExpiresInMinutes, claims).Build().Value }; return(HandleResult(new ResultDataModel <AccountResultModel>() { Data = token })); } return(HandleResult(result)); }
public async Task <AccountResultModel> Register(AccountModel accountModel) { var user = new User { UserName = accountModel.UserName }; var result = await _unitOfWork.UserManager.CreateAsync(user, accountModel.Password); var accountResultModel = new AccountResultModel(); if (result.Succeeded) { await _unitOfWork.SignInManager.SignInAsync(user, false); accountResultModel.Token = GenerateJwtToken(accountModel.UserName, user); return(accountResultModel); } accountResultModel.Errors = new List <string>(); foreach (var error in result.Errors) { accountResultModel.Errors.Add(error.Description); } return(accountResultModel); }
public async Task <AccountResultModel> Login(AccountModel accountModel) { var result = await _unitOfWork.SignInManager.PasswordSignInAsync(accountModel.UserName, accountModel.Password, false, false); var accountResultModel = new AccountResultModel(); if (result.Succeeded) { var appUser = _unitOfWork.UserManager.Users.SingleOrDefault(r => r.UserName == accountModel.UserName); accountResultModel.Token = GenerateJwtToken(accountModel.UserName, appUser); return(accountResultModel); } accountResultModel.Errors = new List <string>(); accountResultModel.Errors.Add("Incorrect login or password"); return(accountResultModel); }
public AccountModel Login(string entranceID, string username, string password, string deviceKey) { AccountResultModel result = new AccountResultModel(); result.Status = AccountResultStatus.UNKNOWN_ERROR; try { username = username.Trim(); entranceID = entranceID.Trim(); deviceKey = deviceKey.Trim(); PersistencyManager db = m_Persistency_Manager; if (db != null) { result.Account = GetAccount(db, entranceID, username, password, deviceKey, 1); if (result.Account != null) { result.Status = AccountResultStatus.VALID_LOGIN; } else { result.Status = AccountResultStatus.INVALID_USERNAME_PASSWORD; } } else { result.Status = AccountResultStatus.INVALID_SERVER; } } catch (Exception exc) { result.Status = AccountResultStatus.UNKNOWN_ERROR; } return(result.Account); }