// This method gets called by the runtime. Use this method to add services to the container. public void ConfigureServices(IServiceCollection services) { services.Configure <AppSettings>(Configuration.GetSection("AppSettings")); services.AddCors(); services.AddControllers().AddJsonOptions(o => { o.JsonSerializerOptions.IgnoreNullValues = true; o.JsonSerializerOptions.Converters.Add(new System.Text.Json.Serialization.JsonStringEnumConverter()); }); services.AddControllers(options => { options.RespectBrowserAcceptHeader = true; // false by default }); services.AddAutoMapper(AppDomain.CurrentDomain.GetAssemblies()); services.AddRazorPages(); // configure strongly typed settings objects var appSettingsSection = Configuration.GetSection("AppSettings"); services.Configure <AppSettings>(appSettingsSection); // configure jwt authentication AppSettings appSettings = appSettingsSection.Get <AppSettings>(); var key = Encoding.ASCII.GetBytes(appSettings.Secret); services.AddAuthentication(x => { x.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme; x.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme; }) .AddJwtBearer(x => { x.Events = new JwtBearerEvents { OnTokenValidated = context => { IOptions <AppSettings> config = Options.Create <AppSettings>(appSettings); IConnection _connection = new Connection(config); UserDAO userDAO = new UserDAO(_connection); var email = ClaimHelper.GetEmailFromClaimIdentity((ClaimsIdentity)context.Principal.Identity); var user = userDAO.FindUserByEmail(email); if (user == null) { context.Fail("Nao autorizado"); } return(Task.CompletedTask); }, OnMessageReceived = context => { var accessToken = context.Request.Query["access_token"]; // If the request is for our hub... var path = context.HttpContext.Request.Path; if (!string.IsNullOrEmpty(accessToken) && (path.StartsWithSegments("/chatHub"))) { // Read the token out of the query string context.Token = accessToken; } return(Task.CompletedTask); } }; x.RequireHttpsMetadata = false; x.SaveToken = true; x.TokenValidationParameters = new TokenValidationParameters { ValidateIssuerSigningKey = true, IssuerSigningKey = new SymmetricSecurityKey(key), ValidateIssuer = false, ValidateAudience = false, ValidateLifetime = true, ClockSkew = TimeSpan.Zero, }; }); services.AddSignalR(); services.AddSwaggerGen(c => { c.EnableAnnotations(); c.SwaggerDoc("v1", new OpenApiInfo { Title = "HouseM8API", Version = "v1" }); // Configure Swagger // "Bearer" is the name for this definition. Any other name could be used c.AddSecurityDefinition("Bearer", new OpenApiSecurityScheme { Description = "Use bearer token to authorize", Type = SecuritySchemeType.Http, Scheme = "bearer", BearerFormat = "JWT" }); c.OperationFilter <AddAuthorizationHeaderOperationHeader>(); // Set the comments path for the Swagger JSON and UI. var xmlFile = $"{Assembly.GetExecutingAssembly().GetName().Name}.xml"; var xmlPath = Path.Combine(AppContext.BaseDirectory, xmlFile); c.IncludeXmlComments(xmlPath, includeControllerXmlComments: true); }); }