예제 #1
0
        public bool ValidateToken(string token)
        {
            var tokenHandler = new JwtSecurityTokenHandler();
            var tokenValidationParameters = TokenValidation.TokenParameter(_jwtSettings, JwtSecurity.GetPublicSigningCredential(_jwtSettings));

            try
            {
                tokenHandler.ValidateToken(token.Replace($"{_jwtSettings.TokenType} ", ""), tokenValidationParameters, out SecurityToken securityToken);
                return(true);
            }
            catch
            {
                return(false);
            }
        }
예제 #2
0
        public static void JwtConfiguration(this IServiceCollection services, IConfiguration configuration)
        {
            var tokenSection  = configuration.GetSection("JwtSettings");
            var tokenSettings = tokenSection.Get <JwtSettings>();

            services.Configure <JwtSettings>(tokenSection);
            services.AddScoped <IJwtTokenProvider, JwtTokenProvider>();

            services.AddAuthentication(x =>
            {
                x.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
                x.DefaultChallengeScheme    = JwtBearerDefaults.AuthenticationScheme;
            })
            .AddJwtBearer(x =>
            {
                x.Events = new JwtBearerEvents
                {
                    //It is the first event that meets and accepts all requests from the Client, whether token or not.
                    OnMessageReceived = context =>
                    {
                        return(Task.CompletedTask);
                    },
                    //If the token sent with the request is valid, it is triggered and verification procedures are performed
                    OnTokenValidated = context =>
                    {
                        return(Task.CompletedTask);
                    },
                    //The token that came with the request is invalid, worn or corrupted
                    OnAuthenticationFailed = context =>
                    {
                        return(Task.CompletedTask);
                    },
                    OnChallenge = context =>
                    {
                        return(Task.CompletedTask);
                    }
                };
                x.RequireHttpsMetadata      = false;
                x.SaveToken                 = true;
                x.TokenValidationParameters = TokenValidation.TokenParameter(tokenSettings, JwtSecurity.GetPublicSigningCredential(tokenSettings));
            });
        }