示例#1
0
        // This method gets called by the runtime. Use this method to add services to the container. DI
        public void ConfigureServices(IServiceCollection services)
        {
            //setting up the database connection
            string databasePath = Path.Combine("..", "Northwind.db");


            services.AddDbContext <Northwind>(options => options.UseSqlite($"Data Source={databasePath}"));
            var jwtSection = Configuration.GetSection("JWTSettings");
            // services.AddAuthentication("BasicAuthentication").AddScheme<AuthenticationSchemeOptions,BasicAuthenticationHandler>("BasicAuthentication",null);
            var appSettings = jwtSection.Get <JWTSettings>();
            var key         = Encoding.ASCII.GetBytes(appSettings.SecretKey);

            services.AddAuthentication(x =>
            {
                x.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
                x.DefaultChallengeScheme    = JwtBearerDefaults.AuthenticationScheme;
            })
            .AddJwtBearer(x =>
            {
                x.RequireHttpsMetadata      = true;
                x.SaveToken                 = true;
                x.TokenValidationParameters = new TokenValidationParameters
                {
                    ValidateIssuerSigningKey = true,
                    IssuerSigningKey         = new SymmetricSecurityKey(key),
                    ValidateIssuer           = false,
                    ValidateAudience         = false
                };
            });


            services.Configure <JWTSettings>(jwtSection);

            services.AddControllers(options =>
            {
                Console.WriteLine("Default Output Formatters:");
                foreach (IOutputFormatter formatter in options.OutputFormatters)
                {
                    OutputFormatter mediaFomatter = formatter as OutputFormatter;
                    if (mediaFomatter == null)
                    {
                        Console.WriteLine($" {formatter.GetType().Name}");
                    }
                    else // OutputFormatter class has SupportedMediaTypes
                    {
                        Console.WriteLine("{0}, Media Types:{1}", arg0: mediaFomatter.GetType().Name, arg1: string.Join(", ", mediaFomatter.SupportedMediaTypes));
                    }
                }
            })
            .AddXmlDataContractSerializerFormatters()
            .AddXmlSerializerFormatters()
            .SetCompatibilityVersion(CompatibilityVersion.Version_3_0);

            services.AddScoped <ICustomerRepository, CustomerRepository>();
            services.AddScoped <IEmployeeRepository, EmployeeRepository>();

            // Registering the swagger generator and define a
            // swagger document for Northwind Service
            services.AddSwaggerGen(options =>
            {
                options.SwaggerDoc(name: "v1", info: new OpenApiInfo {
                    Title = "Northwind Service API", Version = "v1"
                });
            });
        }