// 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.AddMvc();
            services.AddCors();
            services.AddSingleton <string>(Configuration.GetConnectionString("LiteDB"));
            services.AddSingleton(typeof(CrudService <>), typeof(CrudService <>));
            services.AddTransient <UsuarioService>();
            services.AddTransient <EventoService>();


            var configuracaoAcesso = new ConfiguracaoAcesso();

            services.AddSingleton(configuracaoAcesso);

            var configuracaoToken = new ConfiguracaoToken();


            new ConfigureFromConfigurationOptions <ConfiguracaoToken>
                (Configuration.GetSection("ConfiguracaoToken")).Configure(configuracaoToken);

            services.AddSingleton(configuracaoToken);

            services.AddAuthentication(opcoes =>
            {
                opcoes.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
                opcoes.DefaultChallengeScheme    = JwtBearerDefaults.AuthenticationScheme;
            }).AddJwtBearer(bearer =>
            {
                var parametrosValidacao = bearer.TokenValidationParameters;
                parametrosValidacao.IssuerSigningKey = configuracaoAcesso.Key;
                parametrosValidacao.ValidAudience    = configuracaoToken.Audience;
                parametrosValidacao.ValidIssuer      = configuracaoToken.Issuer;

                parametrosValidacao.ValidateIssuerSigningKey = true;
                parametrosValidacao.ValidateLifetime         = true;

                parametrosValidacao.ClockSkew = TimeSpan.Zero;
            });


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

            Database.Configuration.Configure(Configuration.GetConnectionString("LiteDB"));
        }
Exemplo n.º 2
0
 public UsuarioController(UsuarioService servico, ConfiguracaoToken configToken, ConfiguracaoAcesso configAcesso) : base(servico)
 {
     ConnectionString = servico.ConnectionString;
     Token            = configToken;
     Acesso           = configAcesso;
 }