// This method gets called by the runtime. Use this method to add services to the container. public void ConfigureServices(IServiceCollection services) { //PARA DEFINIR A AUTENTICAÇÃO var tokenConfigurations = new TokenConfiguration(); new ConfigureFromConfigurationOptions <TokenConfiguration>( Configuration.GetSection("TokenConfiguration") ) .Configure(tokenConfigurations); services.AddSingleton(tokenConfigurations); services.AddAuthentication(Options => { Options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme; Options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme; }) .AddJwtBearer(Options => { Options.TokenValidationParameters = new TokenValidationParameters { ValidateIssuer = true, ValidateAudience = true, ValidateLifetime = true, ValidateIssuerSigningKey = true, ValidIssuer = tokenConfigurations.Issuer, ValidAudience = tokenConfigurations.Audience, IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(tokenConfigurations.Secret)) }; }); services.AddAuthorization(Auth => { Auth.AddPolicy("Bearer", new AuthorizationPolicyBuilder() .AddAuthenticationSchemes(JwtBearerDefaults.AuthenticationScheme) .RequireAuthenticatedUser().Build()); }); //------------------------------------------- services.AddDbContext <MySqlContext>(options => options.UseMySql(Configuration.GetConnectionString("MySqlConnectionString"))); //PARA RECEBER FORMATO EM XML services.AddMvc(option => { option.RespectBrowserAcceptHeader = true; option.FormatterMappings.SetMediaTypeMappingForFormat("xml", MediaTypeHeaderValue.Parse("application/xml")); option.FormatterMappings.SetMediaTypeMappingForFormat("json", MediaTypeHeaderValue.Parse("application/json")); }) .AddXmlSerializerFormatters(); //------------------------- //CONFIGURAR O CORS services.AddCors(options => options.AddDefaultPolicy(buider => { buider.AllowAnyOrigin() .AllowAnyMethod() .AllowAnyHeader(); })); services.AddControllers(); //INJEÇAO DE DEPENDENCIA //CLASS BOOK services.AddScoped(typeof(IRepository <>), typeof(GenericRepository <>)); services.AddScoped <IBookBusiness, BookBusinessImplementation>(); //INJETAR TOKEN PARAAUTENTICAÇÃO services.AddScoped <ILoginBusiness, LoginBusinessImplementation>(); services.AddTransient <ITokenService, TokenService>(); services.AddScoped <IUserRepository, UserRepository>(); //INJETAR FILE UPLOAD services.TryAddTransient <IHttpContextAccessor, HttpContextAccessor>(); services.AddScoped <IFileBusiness, FileBusinessImplementation>(); //CLASS PERSON services.AddScoped <IPersonBusiness, IPersonBusinessImplementation>(); services.AddScoped <IPersonRepository, PersonRepository>(); //PARA VERSIONAMENTO DA API services.AddApiVersioning(); //DEFINIR O ARRANQUE COM O SWAGGER services.AddSwaggerGen(c => { c.SwaggerDoc("v1", new OpenApiInfo { Title = "Rest Api", Version = "v1", Description = "Api developed in course", Contact = new OpenApiContact { Name = "João Machado", Url = new System.Uri("https://github.com/joaomachado46"), } }); }); }