Example #1
0
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddSignalR()
            .AddNewtonsoftJsonProtocol(opt =>
            {
                opt.PayloadSerializerSettings.TypeNameHandling = TypeNameHandling.Objects;
                opt.PayloadSerializerSettings.ContractResolver = new DefaultContractResolver();
            });
            services.AddSingleton <IUserIdProvider, NameUserIdProvider>();
            services.AddResponseCompression(opts =>
            {
                opts.MimeTypes = ResponseCompressionDefaults.MimeTypes.Concat(
                    new[] { "application/octet-stream" });
            });
            services.Configure <AppSettings>(Configuration.GetSection("AppSettings"));
            services.AddDbContext <OnlineChatDatabaseContext>(options =>
            {
                options.UseSqlServer(Configuration["AppSettings:ConnectionString"]);
            });

            services.AddAuthentication(options =>
            {
                options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
                options.DefaultChallengeScheme    = JwtBearerDefaults.AuthenticationScheme;
            })
            .AddJwtBearer(options =>
            {
                var key = Configuration.GetSection("AppSettings").GetValue <string>("EncryptionKey");

                options.TokenValidationParameters = new TokenValidationParameters
                {
                    ValidateIssuerSigningKey = true,
                    IssuerSigningKey         = new SymmetricSecurityKey(Encoding.ASCII.GetBytes(key)),
                    ValidateIssuer           = false,
                    ValidateAudience         = false
                };

                options.Events = new JwtBearerEvents
                {
                    OnMessageReceived = context =>
                    {
                        var accessToken = context.Request.Query["access_token"];

                        // If the request is for our hub...
                        var path = context.HttpContext.Request.Path;
                        if (!string.IsNullOrEmpty(accessToken) &&
                            (path.StartsWithSegments("/instantmessaging")))
                        {
                            // Read the token out of the query string
                            context.Token = accessToken;
                        }
                        return(Task.CompletedTask);
                    }
                };
            });

            services.AddTransient <IUserService, UserService>()
            .AddTransient <IChatService, ChatService>()
            .AddScoped <ISubscriptionManager, SubscriptionManager>()
            .AddScoped <IUserRepo, UserRepo>()
            .AddScoped <IChatRepo, ChatRepo>()
            .AddScoped <IMessageReadStatusRepo, MessageReadStatusRepo>()
            .AddScoped <IMessageRepo, MessageRepo>()
            .AddSingleton(AutomapperConfig.Build());

            services.AddControllers();
        }