Пример #1
0
 private static void App_Start()
 {
     ProfileRegistration.RegisterMapping();
     _context = new DesignTimeDbContextFactory().CreateDbContext(null);
     //_studentService = new StudentService(_context);
 }
Пример #2
0
        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddDbContext <FrameworkContext>(options =>
                                                     options.UseSqlServer("Server=localhost;Database=NetCoreFramework;Trusted_Connection=True;MultipleActiveResultSets=true").EnableSensitiveDataLogging(true));

            services.AddScoped <IStudentService, StudentService>();

            services.AddMvc(/*options=> options.Filters.Add(typeof(ValidateModelStateAttribute))*/).SetCompatibilityVersion(CompatibilityVersion.Version_2_2);


            services.AddCors();

            services.AddAuthentication(options =>
            {
                options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
                options.DefaultChallengeScheme    = JwtBearerDefaults.AuthenticationScheme;
            }
                                       ).AddJwtBearer(options =>
            {
                options.Events = new JwtBearerEvents()
                {
                    OnChallenge = context =>
                    {
                        // Skip the default logic.
                        context.HandleResponse();

                        var payload = new JObject
                        {
                            ["error"]      = "Needed valid token to authorize!",
                            ["statusCode"] = 401
                        };
                        context.Response.StatusCode = 401;
                        return(context.Response.Body.WriteAsync(Encoding.Default.GetBytes(payload.ToString())).AsTask());
                    }
                };

                options.TokenValidationParameters = new Microsoft.IdentityModel.Tokens.TokenValidationParameters()
                {
                    ValidateIssuerSigningKey = true,
                    IssuerSigningKey         = new SymmetricSecurityKey(Encoding.ASCII.GetBytes("Jwt Key Jwt Key Jwt Key Jwt Key Jwt Key")),
                    ValidateIssuer           = false,
                    ValidateAudience         = false,
                    ValidateLifetime         = true,
                    ClockSkew = TimeSpan.Zero
                };
            });


            // Register the Swagger generator, defining 1 or more Swagger documents
            services.AddSwaggerGen(c =>
            {
                c.SwaggerDoc("v1doc", new Info {
                    Title = "NetCoreFramework.Presentation.WebAPI", Version = "1.0"
                });

                // Swagger 2.+ support
                var security = new Dictionary <string, IEnumerable <string> >
                {
                    { "Bearer", new string[] { } },
                };

                c.AddSecurityDefinition("Bearer", new ApiKeyScheme
                {
                    Description = "JWT Authorization header using the Bearer scheme. Example: \"Authorization: Bearer {token}\"",
                    Name        = "Authorization",
                    In          = "header",
                    Type        = "apiKey"
                });
                c.AddSecurityRequirement(security);
            });


            services.AddRabbitMq(Configuration);

            ProfileRegistration.RegisterMapping();
        }