public async Task <ActionResult <JwtSecurityToken> > PostToken(Client client) { try { if (await _applicationDbContext.Clients .Where(_ => _.Email == client.Email) .Where(_ => _.Password == client.Password) .FirstAsync() is Client _client) { var jwt = new JwtSecurityService(_configuration); var token = jwt.GenerateJwtToken(_client); var jwtSecurityToken = new JwtSecurityToken { Schema = JwtBearerDefaults.AuthenticationScheme, Token = token }; return(jwtSecurityToken); } throw new NullReferenceException(); } catch (Exception exception) { Debug.WriteLine(exception); } return(NotFound()); }
public static void RegisterServices(IServiceCollection services, IConfiguration configuration) { services.AddScoped <IUnityOfWork, UnitOfWork>(); services.AddSingleton <IMappingCache, MappingCache>(); services.AddTransient <ITokenGenerator, JwtTokenGenerator>(); services.AddTransient <IAuthenticationService, LdapAuthenticationService>(); JwtSecurityService.RegisterTokenAuthentication(services, configuration); }
protected string GetUserTokenFromHeader(string token, IConfiguration configuration) { try { var encryptionEnabled = Convert.ToBoolean(configuration["UserTokenEncryptionEnabled"]); if (encryptionEnabled) { var decryptedToken = JwtSecurityService.Decrypt(token); if (!string.IsNullOrWhiteSpace(decryptedToken)) { var userToken = JwtSecurityService.Decode(decryptedToken); return(userToken); } } return(token); } catch (Exception exception) { return(null); } }
public object GenerateToken(string param) { return(JwtSecurityService.Encrypt(JwtSecurityService.BuildJwtToken(param, 10))); }