예제 #1
0
 // Private method to generate JSON Web Token
 string GenerateToken(params Claim[] claims)
 => new JwtSecurityTokenHandler().CreateEncodedJwt(
     issuer: TokenConfig.Issuer,
     audience: TokenConfig.Audience,
     subject: claims == null ? null : new ClaimsIdentity(claims),         // If claim is null, set subject to null
     notBefore: DateTime.UtcNow.AddSeconds(2),
     expires: DateTime.UtcNow.AddDays(1),
     issuedAt: DateTime.UtcNow,
     signingCredentials: new SigningCredentials(
         key: new SymmetricSecurityKey(key: TokenConfig.GetKey()),
         algorithm: SecurityAlgorithms.HmacSha512
         )
     );
예제 #2
0
        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            // Repositories
            services.AddScoped <IBidListRepository, BidListRepository>();
            services.AddScoped <ICurvePointRepository, CurvePointRepository>();
            services.AddScoped <IRatingRepository, RatingRepository>();
            services.AddScoped <ITradeRepository, TradeRepository>();
            services.AddScoped <IUserRepository, UserRepository>();
            services.AddScoped <IRuleRepository, RuleRepository>();
            services.AddScoped <IAccessTokenRepository, AccessTokenRepository>();

            // Services
            services.AddScoped <IBidService, BidService>();
            services.AddScoped <ICurveService, CurveService>();
            services.AddScoped <IRatingService, RatingService>();
            services.AddScoped <ITradeService, TradeService>();
            services.AddScoped <IUserService, UserService>();
            services.AddScoped <IRuleService, RuleService>();

            // Tools
            services.AddScoped(typeof(IAppLogger <>), typeof(AppLogger <>));
            services.AddAutoMapper(typeof(MappingProfile));
            //services.AddMediatR(typeof(Startup).Assembly);


            // Contexts
            services.AddDbContext <LocalDbContext>(opts =>
                                                   opts.UseSqlServer(Configuration.GetConnectionString("Referential")));

            services.AddMvc();
            services.AddControllers();
            services.AddAuthorization();

            services.AddAuthentication(opts =>
            {
                opts.DefaultScheme             = JwtBearerDefaults.AuthenticationScheme;
                opts.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
                opts.DefaultChallengeScheme    = JwtBearerDefaults.AuthenticationScheme;
            }).AddJwtBearer(cfg =>
            {
                cfg.RequireHttpsMetadata      = false;
                cfg.SaveToken                 = true;
                cfg.TokenValidationParameters = new TokenValidationParameters()
                {
                    ValidIssuer      = TokenConfig.ValidIssuer,
                    ValidAudience    = TokenConfig.ValidAudience,
                    IssuerSigningKey = TokenConfig.GetKey(),
                    ClockSkew        = TokenConfig.SkewTime,

                    // security switches
                    RequireExpirationTime    = true,
                    ValidateIssuer           = true,
                    ValidateIssuerSigningKey = true,
                    ValidateAudience         = true
                };
            });

            services.AddSwaggerGen(c =>
            {
                c.SwaggerDoc("v1", new OpenApiInfo
                {
                    Title          = "Api",
                    Version        = "v1",
                    Description    = "Web API Service for Poseidon - OCP7",
                    TermsOfService = new Uri("https://example.com/terms"),
                    Contact        = new OpenApiContact
                    {
                        Name  = "System Admin",
                        Email = "*****@*****.**"
                    },
                    License = new OpenApiLicense
                    {
                        Name = "Use under LICX",
                        Url  = new Uri("https://example.com/license"),
                    }
                });

                //Locate the XML file being generated by ASP.NET...
                var xmlFile = $"{Assembly.GetExecutingAssembly().GetName().Name}.XML";
                var xmlPath = Path.Combine(AppContext.BaseDirectory, xmlFile);

                //... and tell Swagger to use those XML comments.
                c.IncludeXmlComments(xmlPath);
                c.AddSecurityDefinition("Bearer", new OpenApiSecurityScheme
                {
                    Description = @"JWT Authorization header using the Bearer scheme. \r\n\r\n 
                      Enter 'Bearer' [space] and then your token in the text input below.
                      \r\n\r\nExample: 'Bearer 12345abcdef'",
                    Name        = "Authorization",
                    In          = ParameterLocation.Header,
                    Type        = SecuritySchemeType.ApiKey,
                    Scheme      = "Bearer"
                });
                c.AddSecurityRequirement(new OpenApiSecurityRequirement()
                {
                    {
                        new OpenApiSecurityScheme
                        {
                            Reference = new OpenApiReference
                            {
                                Type = ReferenceType.SecurityScheme,
                                Id   = "Bearer"
                            },
                            Scheme = "oauth2",
                            Name   = "Bearer",
                            In     = ParameterLocation.Header
                        },
                        new List <string>()
                    }
                });
            });
        }