示例#1
0
        // This method gets called by the runtime. Use this method to add services to the container.
        // For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddDBConfiguration(Configuration);
            services.RegisterDI();
            services.AWSConfiguration(Configuration);
            services.MailSettings(Configuration);
            services.ApplicationSetting(Configuration);

            var key = TokenAuthOption.newKey;

            services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme).AddJwtBearer(JwtBearerDefaults.AuthenticationScheme, (o) =>
            {
                o.TokenValidationParameters = new TokenValidationParameters()
                {
                    IssuerSigningKey         = new SymmetricSecurityKey(key),
                    ValidAudience            = TokenAuthOption.Audience,
                    ValidIssuer              = TokenAuthOption.Issuer,
                    ValidateIssuerSigningKey = true,
                    ValidateLifetime         = true,
                    ValidateIssuer           = true,
                    ValidateAudience         = true,
                    //ClockSkew = TimeSpan.FromMinutes(0)
                };
            });

            services.AddAuthorization(auth =>
            {
                auth.AddPolicy(JwtBearerDefaults.AuthenticationScheme, new AuthorizationPolicyBuilder()
                               .AddAuthenticationSchemes(JwtBearerDefaults.AuthenticationScheme)
                               .RequireAuthenticatedUser().Build());
            });

            services.AddCors(a => a.AddPolicy("AttachMore",
                                              builder => builder
                                              .AllowAnyHeader()
                                              .AllowAnyMethod()
                                              .AllowAnyOrigin()
                                              ));

            services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);

            services.AddSwaggerGen(a =>
            {
                a.SwaggerDoc("v2", new Swashbuckle.AspNetCore.Swagger.Info {
                    Title = "AttachMore NextGen", Version = "v2"
                });
                a.OperationFilter <AuthorizationHeaderParameterOperationFilter>();
            });
        }