// This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            var users = new Dictionary <string, string>
            {
                { "Chris", "password" }
            };
            var svc = new DummyUserService(users);

            services.AddSingleton <IUserService>(svc);

            services.AddAuthentication(o =>
            {
                o.DefaultScheme             = CookieAuthenticationDefaults.AuthenticationScheme;
                o.DefaultAuthenticateScheme = CookieAuthenticationDefaults.AuthenticationScheme;
                o.DefaultChallengeScheme    = CookieAuthenticationDefaults.AuthenticationScheme;
            })
            .AddCookie(options =>
            {
                options.Cookie.Name = ".CoreAuth";
                options.LoginPath   = new Microsoft.AspNetCore.Http.PathString("/auth/signin");
            });

            services.AddAuthorization(options =>
            {
                options.AddPolicy("Cookies", policy =>
                {
                    policy.AuthenticationSchemes.Add("Cookies");
                    policy.RequireAuthenticatedUser();
                });
            });

            services.AddMvc(options => {
                options.Filters.Add(new RequireHttpsAttribute());
            });
        }
Exemplo n.º 2
0
        public void Initialize()
        {
            _context = new JoggingDbContext(new DbContextOptionsBuilder <JoggingDbContext>().UseInMemoryDatabase().Options);
            _context.Database.EnsureDeleted();//make sure we don't share state between tests

            _context.Users.Add(_regularUser1 = new User()
            {
                Id = 123, Email = "[email protected]", FirstName = "Joe", LastName = "User", Role = UserRole.User
            });
            _context.Users.Add(_regularUser2 = new User()
            {
                Id = 234, Email = "[email protected]", FirstName = "Moe", LastName = "User", Role = UserRole.User
            });
            _context.Users.Add(_adminUser = new User()
            {
                Id = 345, Email = "*****@*****.**", FirstName = "Le", LastName = "Admin", Role = UserRole.Admin
            });
            _context.SaveChanges();

            _userService = new DummyUserService(_regularUser1);
            _controller  = new EntriesController(_context, _userService);
        }