コード例 #1
0
ファイル: Startup.cs プロジェクト: r0npy/pelsoft.jwt.demo
        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            #region load appSettings

            services.AddMemoryCache();
            //services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();

            services.AddScoped <IStore, Store>();
            services.AddScoped <IRefreshTokenProvider, RefreshTokenProvider>();

            var mokloging = new pelsoft.auth.helpers.MockLogin();
            Configuration.Bind("mokloging", mokloging);

            var connectionStrings = new Fwk.Database.ConnectionStrings();
            Configuration.Bind("ConnectionStrings", connectionStrings);

            var apiConfig = new apiConfig();
            Configuration.Bind("apiConfig", apiConfig);

            apiAppSettings.connectionStrings = connectionStrings;
            apiAppSettings.apiConfig         = apiConfig;

            var providers = new List <Fwk.Security.Identity.jwtSecurityProvider>();
            Configuration.Bind("fwk_securityProviders", providers);

            var secConfig = new Fwk.Security.Identity.secConfig();
            secConfig.fwk_securityProviders = providers;
            secConfig.cnnStrings            = connectionStrings;


            fwkSec.SecurityManager.set_secConfig(secConfig);
            #endregion

            services.AddScoped <MockAuthenticator>();
            services.AddScoped <AmazonAuthenticator>();

            // configure DI for application services
            services.AddControllers();

            #region CORS
            //CORS  policy :  global cors policy allow all
            services.AddCors(options =>
            {
                options.AddPolicy("CorsPolicy", builder =>
                                  builder.WithOrigins("http://192.168.0.74:4200/")
                                  .AllowAnyOrigin()
                                  .AllowAnyMethod()
                                  .AllowAnyHeader());
            });

            #endregion
            IdentityModelEventSource.ShowPII = true;

            #region servicios de swagger

            services.AddSwaggerGen(c =>
            {
                c.SwaggerDoc(name: "v1", new OpenApiInfo
                {
                    Title   = "Konecta api auth ",
                    Version = "v1"
                });

                var xmlFile = $"{Assembly.GetExecutingAssembly().GetName().Name}.xml";
                var xmlPath = Path.Combine(AppContext.BaseDirectory, xmlFile);
                c.IncludeXmlComments(xmlPath);
            });

            #endregion

            //Permite conservar los nombres de los atributos
            //Core 3 uses System.Text.Json, which by default does not preserve the case.
            //Setting the PropertyNamingPolicy to null will fix the problem.
            services.AddControllers()
            .AddJsonOptions(opts => opts.JsonSerializerOptions.PropertyNamingPolicy = null);
        }
コード例 #2
0
ファイル: Startup.cs プロジェクト: r0npy/pelsoft.jwt.demo
        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            #region load appSettings

            var connectionStrings = new Fwk.Database.ConnectionStrings();
            Configuration.Bind("ConnectionStrings", connectionStrings);

            var apiConfig = new apiConfig();
            Configuration.Bind("apiConfig", apiConfig);      //  <--- This


            apiAppSettings.connectionStrings = connectionStrings;
            apiAppSettings.apiConfig         = apiConfig;


            //var providers = new List<Fwk.Security.Identity.jwtSecurityProvider>();
            //Configuration.Bind("fwk_securityProviders", providers);

            var secConfig = new Fwk.Security.Identity.secConfig();
            //secConfig.fwk_securityProviders = providers;
            secConfig.cnnStrings = connectionStrings;


            fwkSec.SecurityManager.set_secConfig(secConfig);

            #endregion

            // configure DI for application services
            services.AddScoped <ITestService, TestService>();

            services.AddControllers();
            #region configure jwt authentication


            //  var appSettings = appSettingsSection.Get<serverSettings>();
            //var key = Encoding.UTF8.GetBytes(apiAppSettings.apiConfig.api_secretKey);
            var secretKey = Encoding.ASCII.GetBytes(apiAppSettings.apiConfig.api_secretKey);

            var securityKey = new SymmetricSecurityKey(secretKey);

            services.AddAuthentication(x =>
            {
                x.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
                x.DefaultScheme             = JwtBearerDefaults.AuthenticationScheme;
                x.DefaultChallengeScheme    = JwtBearerDefaults.AuthenticationScheme;
            }).AddJwtBearer(jwtBearerOptions =>
            {
                jwtBearerOptions.Events = new JwtBearerEvents()
                {
                    //captura el evento de autenticaci�n
                    OnTokenValidated = context =>
                    {
                        return(Task.CompletedTask);
                    }
                };

                jwtBearerOptions.RequireHttpsMetadata = false;
                jwtBearerOptions.SaveToken            = true;

                //https://tools.ietf.org.html/rfc7519
                jwtBearerOptions.TokenValidationParameters = new TokenValidationParameters
                {
                    SaveSigninToken          = true,
                    ValidateActor            = true,
                    ValidateIssuer           = false, //emisor
                    ValidateAudience         = true,  //destinatarios del token
                    ValidateLifetime         = true,  //
                    ValidateIssuerSigningKey = true,
                    IssuerSigningKey         = securityKey,
                    RequireExpirationTime    = false,
                    ValidIssuer   = apiAppSettings.apiConfig.api_issuerToken,
                    ValidAudience = apiAppSettings.apiConfig.api_audienceToken,
                };
            });

            #endregion


            #region CORS
            //CORS  policy :  global cors policy allow all
            services.AddCors(options =>
            {
                options.AddPolicy("CorsPolicy", builder =>
                                  builder.WithOrigins("http://192.168.0.74:4200/")
                                  .AllowAnyOrigin()
                                  .AllowAnyMethod()
                                  .AllowAnyHeader());
            });

            #endregion
            IdentityModelEventSource.ShowPII = true;
            #region servicios  de swagger
            services.AddSwaggerGen(c =>
            {
                c.SwaggerDoc(name: "v1", new OpenApiInfo
                {
                    Title   = "microsoft Whatsapp Service API ",
                    Version = "v1"
                });
            });

            #endregion

            //Permite conservar los nombres de los atributos
            //Core 3 uses System.Text.Json, which by default does not preserve the case.
            //Setting the PropertyNamingPolicy to null will fix the problem.
            services.AddControllers()
            .AddJsonOptions(opts => opts.JsonSerializerOptions.PropertyNamingPolicy = null);

            //services.AddControllers(options => options.SuppressImplicitRequiredAttributeForNonNullableReferenceTypes = true);
        }