Exemplo n.º 1
0
        public static IServiceCollection AddEdamosApiServices(this IServiceCollection services)
        {
            services.AddAuthentication(options =>
            {
                options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
                options.DefaultChallengeScheme    = JwtBearerDefaults.AuthenticationScheme;
            })
            .AddJwtBearer(JwtBearerDefaults.AuthenticationScheme,
                          options =>
            {
                options.Authority = DebugConstants.IdentityServer.Authority;
                options.Audience  = Consts.Api.ResourceId;
                options.SaveToken = false;
                options.Events    = new JwtBearerEvents
                {
                    OnTokenValidated = async vc =>
                    {
                        ClaimsPrincipal principal =
                            await IdentityHelper.CreateEdamosPrincipal(vc.HttpContext, vc.Principal);
                        vc.HttpContext.User = principal;
                        vc.Principal.AddIdentity(principal.Identity as ClaimsIdentity);
                    }
                };
            });


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

                options.DefaultPolicyName = "default";
            });

            return(services);
        }