public ProductTypesController()
 {
     _factory    = new MusicShopDbContextFactory();
     _context    = _factory.Create(new DbContextFactoryOptions());
     _repository = new Repository(_context);
     _schema     = JSchema.Parse(GetJsonSchema());
 }
示例#2
0
        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            var connectionString = MusicShopDbContextFactory.GetDefaultMusicShopDbConnection();
            var dbContextFactory = new MusicShopDbContextFactory();
            var dbContext        = dbContextFactory.Create(new DbContextFactoryOptions());

            //services.AddSingleton(dbContextFactory);
            //services.AddSingleton(dbContext);
            //services.AddSingleton<IRepository, Repository>();

            services.AddCors();

            services.AddIdentity <User, IdentityRole>()
            .AddEntityFrameworkStores <MusicShopDbContext>()
            .AddDefaultTokenProviders();

            // Add in EF
            services.AddEntityFramework(connectionString);



            // Add framework services.
            services.AddMvc();

            services.Configure <AppConfiguration>(Configuration.GetSection("AppConfiguration"));
            //intercept any unauthorized requests that come inot the api and rather than redirecting to account/login
            //(since were using identity) return a 401 unauthorized header
            services.Configure <IdentityOptions>(config =>
            {
                config.Cookies.ApplicationCookie.Events =
                    new CookieAuthenticationEvents
                {
                    //default action when we are not logged in is to redirect to login, we're adjusting the behaviour of this here...
                    OnRedirectToLogin = ctx =>
                    {
                        //if (ctx.Request.Path.StartsWithSegments("/api") && ctx.Response.StatusCode == 200)
                        //{
                        ctx.Response.StatusCode = 401;
                        return(Task.FromResult <object>(null));
                        //}

                        //ctx.Response.Redirect(ctx.RedirectUri);
                        //return Task.FromResult<object>(null);
                    }
                };
            });
        }