/// <summary> /// Get new token /// </summary> /// <param name="username">login</param> /// <returns>token</returns> private async Task <string> GetToken(string username) { int userId = await _appDbContext.FindByLogin(username); var identity = await _authorizationManager.GetIdentity(username, userId); if (identity == null) { return(null); } _log.LogInfo("Set token options."); var now = DateTime.Now; var jwt = new JwtSecurityToken( issuer: AuthorizationOptions.Issuer, audience: AuthorizationOptions.Audience, notBefore: now, claims: identity.Claims, expires: now.Add(TimeSpan.FromMinutes(AuthorizationOptions.Lifetime)), signingCredentials: new SigningCredentials(AuthorizationOptions.GetSymmetricSecurityKey(), SecurityAlgorithms.HmacSha256)); var encodedJwt = new JwtSecurityTokenHandler().WriteToken(jwt); _log.LogInfo("Set session options."); Sessions start = new Sessions() { Token = encodedJwt, UserId = userId, ExpiredDate = now.Add(TimeSpan.FromMinutes(AuthorizationOptions.Lifetime)) }; _log.LogInfo("Check for previous session."); if (await _appDbContext.IsExistPreviousSession(userId)) { _log.LogInfo("The session was founded. I`ll delete it."); await _appDbContext.DeleteSessions(userId); _log.LogInfo("Success delete."); } _log.LogInfo("Add session"); await _appDbContext.AddSession(start); _log.LogInfo("Session was add."); _log.LogInfo("Return session's token"); return(encodedJwt); }