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());
        }
示例#2
0
 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);
            }
        }
示例#4
0
 public object GenerateToken(string param)
 {
     return(JwtSecurityService.Encrypt(JwtSecurityService.BuildJwtToken(param, 10)));
 }