public ActionResult GenerateTokenFor(GenerateTokenViewModel model) { CallService(() => { TempData["ElectionId"] = model.SelectedElectionId; TempData["WahlkreisId"] = model.SelectedWahlkreisId; TempData["Amount"] = model.Amount; if (!model.Password.Equals("ichdarfdasschon")) { throw new Exception("Sie dürfen mit diesem Passwort keine Token generieren."); } var election = Service.GetElection(model.SelectedElectionId); var wahlkreis = Service.GetWahlkreis(model.SelectedWahlkreisId); var infoToken = string.Format("Bundestagswahl: {0}\nWahlkreis: {1}\n\nErstellte Token:", election.Date.ToShortDateString(), wahlkreis.Name); for (var i = 0; i < model.Amount; i++) { infoToken += string.Format("\n {0}", TokenValidation.GenerateTokenString(model.SelectedElectionId, model.SelectedWahlkreisId)); } GetMessageBag().Info.Add(infoToken); }); return(RedirectToAction("GenerateToken")); }
public async Task <IActionResult> GenerateToken([FromBody] GenerateTokenViewModel model) { var user = _userService.GetUserByPhone_UserName(model.UserName); if (user == null) { return(new OkObjectResult(new { Errors = new[] { "IncorrectLogin" } })); } return(await GenerateTokenWithRoles(user, model, RoleConstant.AllRoleList)); }
public IActionResult GenerateToken() { var userId = _userManager.GetUserId(User); var userTokenInfo = _tokensService.GetTokenInfoForUser(userId); var model = new GenerateTokenViewModel { AlreadyGenerated = userTokenInfo.TokenGenerated }; return(View(model)); }
public async Task <ActionResult> AssignTokenPermissions([FromBody] GenerateTokenViewModel generateTokenViewModel) { var token = _tokenService.GetToken(await Tokens, generateTokenViewModel.TokenKey); token = _tokenService.ClearTokenPermissions(token); token.TokenPermissions = (await AssignTokenPermissions(generateTokenViewModel.Permissions)).ToList(); var savedToken = await _tokenService.SaveToken(token); await ClearTokenCache(); return(Ok(savedToken)); }
private async Task <IActionResult> GenerateTokenWithRoles(User user, GenerateTokenViewModel model, List <string> requiredRoles) { if (user == null) { return(Ok(new { Errors = new[] { "IncorrectLogin" } })); } //// Verify the password provided var result = await _signInManager.CheckPasswordSignInAsync(user, model.Password, true); if (result.IsLockedOut) { return(Ok(user.LockoutEnd == DateTimeOffset.MaxValue ? new { Errors = new[] { "UserDeactivated" } } : new { Errors = new[] { "UserLockedOut" } })); } if (result.IsNotAllowed) { return(Ok(new { Errors = new[] { "EmailNotConfirmed" } })); } if (!result.Succeeded) { await _userManager.AccessFailedAsync(user); return(Ok(new { Errors = new[] { "IncorrectLogin" } })); } var activePlanId = string.Empty; // Get roles of current user var roles = await _userManager.GetRolesAsync(user); if (!requiredRoles.Any(requiredRole => roles.Contains(requiredRole))) { return(Ok(new { Errors = new[] { "UnauthorizedRole" } })); } // Generate claims and JWT token var langs = Utils.GetAllLanguages(); var claims = ClaimHelper.GetClaims(user, roles, langs); _userService.UpdateLastLogin(user.Id); // Generate token var token = TokenGenerator.Generate(claims, roles, _config, user.SecurityStamp); return(Ok(token)); }
public async Task <ActionResult> GenerateToken([FromBody] GenerateTokenViewModel generateTokenViewModel) { var expiryDate = DetermineExpiryDate(); var tokenKey = _tokenKeyGenerator.GenerateKey(HashAlgorithm.Sha512); var generatedToken = _tokenService.GenerateToken(tokenKey, expiryDate); if (generateTokenViewModel.Permissions.Any()) { generatedToken.TokenPermissions = (await AssignTokenPermissions(generateTokenViewModel.Permissions)).ToArray(); } var savedToken = await _tokenService.SaveToken(generatedToken); await ClearTokenCache(); return(Ok(savedToken)); }
public GenerateTokenViewModel GetGenerateTokenModel() { var model = new GenerateTokenViewModel(); using (var context = new ElectionDBEntities()) { var elections = context.Elections.Where(e => e.Id >= 3); var electionVms = ViewModelMap.ViewModelMap.GetElectionViewModels(elections).ToList(); var wahlkreise = context.Wahlkreis; var wahlkreiseVms = ViewModelMap.ViewModelMap.GetWahlkreisViewModels(wahlkreise).ToList(); model.Election = electionVms.OrderBy(r => r.Date); model.SelectedElectionId = elections.Any() ? elections.OrderByDescending(r => r.Date).First().Id : 0; model.Wahlkreise = wahlkreiseVms.OrderBy(w => w.Name); model.Amount = 1; } return(model); }
public async Task <IActionResult> GenerateToken(GenerateTokenViewModel postModel) { var userId = _userManager.GetUserId(User); var userTokenInfo = _tokensService.GetTokenInfoForUser(userId); if (userTokenInfo.TokenGenerated) { return(BadRequest("Token dla bieżącego użytkownika był już generowany wcześniej")); } var generatedToken = _tokenGenerator.GenerateToken(_tokenLength); _tokensService.AddTokenForUser(generatedToken); await _usersService.SetTokenGeneratedForUser(userId); var model = new GenerateTokenViewModel { Token = generatedToken }; return(View(model)); }