Ejemplo n.º 1
0
        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            AutoMapper.Mapper.Initialize(cfg => {
                cfg.CreateMap <Patient, PatientImportViewModel>();
            });

            var builder = services.AddIdentityServer()
                          .AddTemporarySigningCredential()
                          .AddInMemoryIdentityResources(Config.GetIdentityResources())
                          .AddInMemoryApiResources(Config.GetApiResources())
                          .AddInMemoryClients(Config.GetClients());

            builder.Services.Configure <MongoDbRepositoryConfiguration>(Configuration.GetSection("MongoDbRepository"));
            builder.Services.AddTransient <IUserRepository, UserRepository>();
            builder.Services.AddTransient <UserRepository>();
            builder.Services.AddTransient <IProfileService, ProfileService>();
            builder.Services.AddTransient <IResourceOwnerPasswordValidator, UserResourceOwnerPasswordValidator>();
            builder.Services.AddTransient <IPasswordHasher <User>, PasswordHasher <User> >();
            builder.Services.AddTransient <UserRepository>();
            builder.Services.AddTransient <PatientRepository>();
            builder.Services.AddTransient <CalendarEventRepository>();
            builder.Services.AddTransient <TeamRepository>();
            builder.Services.AddTransient <UserUtilService>();
            builder.Services.AddTransient <ChatHandler>();

            services.AddCors(options =>
            {
                // this defines a CORS policy called "default"
                options.AddPolicy("default", policy => policy.AllowAnyHeader().AllowAnyMethod().AllowAnyOrigin().AllowCredentials());
            });

            // Add framework services.
            services.AddMvc(options =>
            {
                options.Filters.Add(new CorsAuthorizationFilterFactory("default"));
            });

            services.AddWebSocketManager();
        }
        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            //database connection
            services.AddDbContext <ApplicationDbContext>(options =>
                                                         options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));

            //aspnet identity
            services.AddIdentity <ApplicationUser, IdentityRole>()
            .AddEntityFrameworkStores <ApplicationDbContext>()
            //.AddUserStore<UserStore<ApplicationUser>()
            //.AddRoleStore<IRoleStore<>()
            .AddUserManager <ApplicationUserManager>()
            //.AddRoleManager<RoleManager<>()
            .AddDefaultTokenProviders();

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

            // configure identity server with in-memory stores, keys, clients and scopes
            services.AddIdentityServer()
            .AddDeveloperSigningCredential()
            .AddInMemoryPersistedGrants()
            .AddInMemoryIdentityResources(Config.GetIdentityResources())
            .AddInMemoryApiResources(Config.GetApiResources())
            .AddInMemoryClients(Config.GetClients())
            .AddAspNetIdentity <ApplicationUser>()
            .AddProfileService <ProfileService>();
            //.AddTestUsers(Config.GetUsers());

            //JwtSecurityTokenHandler.DefaultInboundClaimTypeMap.Clear();

            services.AddAuthentication(IdentityServerAuthenticationDefaults.AuthenticationScheme)
            .AddIdentityServerAuthentication(x =>
            {
                x.Authority = Configuration.GetSection("IdentityServerSettings:Host").Value;
                //x.AllowedScopes = new List<string> { "Api" };
                x.ApiSecret            = "ServerSecret";
                x.ApiName              = "server";
                x.SupportedTokens      = SupportedTokens.Both;
                x.RequireHttpsMetadata = false;
                x.RoleClaimType        = "role";

                //change this to true for SLL
                x.RequireHttpsMetadata = false;
            });

            //httpcontext
            services.AddSingleton <IHttpContextAccessor, HttpContextAccessor>();

            services.AddScoped <ApplicationUserManager>();

            //extra auth policies
            services.AddAuthorization(options =>
            {
                options.AddPolicy("SameTokenIp", policy => policy.Requirements.Add(new SameTokenIpRequirement()));
            });

            services.AddSingleton <IAuthorizationHandler, SameTokenIpHandler>();


            //resource owner setup for identity server
            services.AddTransient <IResourceOwnerPasswordValidator, ResourceOwnerPasswordValidator>();
            services.AddTransient <IProfileService, ProfileService>();


            //add signalr
            services.AddSignalR(config => config.EnableDetailedErrors = true);
        }