예제 #1
0
        public AuthorizationDataSerializer(JwtConfiguration jwtConfiguration)

        {
            this.jwtConfiguration = jwtConfiguration ??
                                    throw new ArgumentNullException(nameof(jwtConfiguration));
            tokenHandler = new JwtSecurityTokenHandler();
        }
        public static IServiceCollection AddOtcAspNetCoreJwtAuthorizationContext(this IServiceCollection services, Otc.AuthorizationContext.AspNetCore.Jwt.JwtConfiguration jwtConfiguration)
        {
            if (services == null)
            {
                throw new ArgumentNullException(nameof(services));
            }

            if (jwtConfiguration == null)
            {
                throw new ArgumentNullException(nameof(jwtConfiguration));
            }

            services.AddSingleton(jwtConfiguration);
            services.AddSingleton(typeof(IAuthorizationDataSerializer <>), typeof(AuthorizationDataSerializer <>));
            services.AddScoped(typeof(IAuthorizationContext <>), typeof(AuthorizationContext <>));

            services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
            .AddJwtBearer(options =>
            {
                var apiConfiguration = jwtConfiguration;

                options.SaveToken            = true;
                options.RequireHttpsMetadata = false;

                options.TokenValidationParameters = new TokenValidationParameters()
                {
                    ValidIssuer      = apiConfiguration.Issuer,
                    ValidAudience    = apiConfiguration.Audience,
                    IssuerSigningKey = new SymmetricSecurityKey(
                        Encoding.UTF8.GetBytes(apiConfiguration.SecretKey))
                };
            });

            return(services);
        }