public static DefaultBus CreateDefaultBus(this Configuration configuration)
        {
            var bus = new DefaultBus(configuration);

            // configuration.Builder.Register<IEventBus>(bus);
            // configuration.Builder.Register<ICommandBus>(bus);

            ObjectFactory.Configure(cfg =>
            {
                cfg.For<IEventBus>().Use(bus);
                cfg.For<ICommandBus>().Use(bus);
            });

            return bus;
        }
示例#2
0
        public void ConfigureServices(IServiceCollection services)
        {
            var bus = DefaultBus.CreateBus(Configuration.GetSection("RabbitMq"));

            services.AddSingleton <IBusControl>(bus);
            services.AddSingleton <IBus>(bus);
            services.AddSingleton <IPublishEndpoint>(bus);

            /*services.AddHangfire(x => x.UseSqlServerStorage(
             *   Configuration.GetConnectionString("DefaultConnection")));*/

            services.Configure <CookiePolicyOptions>(options =>
            {
                // This lambda determines whether user consent for non-essential cookies is needed for a given request.
                options.CheckConsentNeeded    = context => true;
                options.MinimumSameSitePolicy = SameSiteMode.None;
            });


            var connectionStr = Environment.GetEnvironmentVariable("CONSTR") ??
                                Configuration.GetConnectionString("DefaultConnection");

            var inMemory = Environment.GetEnvironmentVariable("INMEMORY");

            if (inMemory == null)
            {
                Console.WriteLine("Not in memory " + inMemory);
                services.AddDbContext <ApplicationDbContext>(options =>
                                                             options.UseSqlServer(connectionStr));
            }
            else
            {
                Console.WriteLine("InMemory " + inMemory);
                services.AddDbContext <ApplicationDbContext>(options =>
                                                             options.UseInMemoryDatabase());
            }

            services.AddIdentity <User, IdentityRole> (options =>
            {
                options.Password.RequiredUniqueChars    = 0;
                options.Password.RequireUppercase       = false;
                options.Password.RequireNonAlphanumeric = false;
                options.Password.RequireLowercase       = false;
            })
            .AddEntityFrameworkStores <ApplicationDbContext>()
            .AddDefaultTokenProviders();

            JwtSecurityTokenHandler.DefaultInboundClaimTypeMap.Clear();
            services
            .AddAuthentication(options =>
            {
                /*  options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
                 * options.DefaultScheme = JwtBearerDefaults.AuthenticationScheme;
                 * options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;*/
            })
            .AddJwtBearer(cfg =>
            {
                cfg.RequireHttpsMetadata      = false;
                cfg.SaveToken                 = true;
                cfg.TokenValidationParameters = new TokenValidationParameters
                {
                    ValidIssuer      = Configuration["JwtIssuer"],
                    ValidAudience    = Configuration["JwtIssuer"],
                    IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(Configuration["JwtKey"])),
                    ClockSkew        = TimeSpan.Zero // remove delay of token when expire
                };
            });

            services.AddAuthorization(options =>
            {
                options.AddPolicy("admin", x =>
                {
                    x.RequireRole("admin");
                });
            });

            services.AddSwaggerGen(options =>
            {
                options.SwaggerDoc("v1", new Swashbuckle.AspNetCore.Swagger.Info()
                {
                    Title = "Student API", Version = "v1"
                });
            });

            services.ConfigureApplicationCookie(options =>
            {
                options.LoginPath        = "/login";
                options.LogoutPath       = "/logoff";
                options.AccessDeniedPath = "/error";

                options.Events = new CookieAuthenticationEvents
                {
                    OnRedirectToLogin = ctx =>
                    {
                        if (ctx.Request.Path.StartsWithSegments("/api") &&
                            ctx.Response.StatusCode == (int)HttpStatusCode.OK)
                        {
                            ctx.Response.StatusCode = (int)HttpStatusCode.Unauthorized;
                        }
                        else
                        {
                            ctx.Response.Redirect(ctx.RedirectUri);
                        }
                        return(Task.FromResult(0));
                    }
                };
            });

            services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
        }