Esempio n. 1
0
 public static void RegisterSwagger(this IServiceCollection services, ConfigSettingModel configSetting)
 {
     services.AddSwaggerGen(c =>
     {
         c.SwaggerDoc(configSetting.Swagger.Doc.Version, new Info
         {
             Title       = configSetting.Swagger.Doc.Title,
             Version     = configSetting.Swagger.Doc.Version,
             Description = configSetting.Swagger.Doc.Desc
         });
         c.AddSecurityDefinition(AuthSpec, new OAuth2Scheme
         {
             Flow             = AuthMode,
             AuthorizationUrl = $"{configSetting.Auth.BaseUrl}/oauth2/authorize",
             Scopes           = new Dictionary <string, string>
             {
                 { "email", "" },
                 { "openid", "" },
                 { "http://localhost:8080/api.aws-exp.v1", "" },
                 { "profile", "" }
             }
         });
         c.OperationFilter <AuthorizationHeaderOperationFilter>();
     });
 }
Esempio n. 2
0
        private T LoadSetting <T>(T settingClass, ConfigSettingModel configSettings)
        {
            //var settings = Activator.CreateInstance<T>();

            var settingsForClass = configSettings?.ConfigKeys;

            if (settingsForClass == null)
            {
                return(settingClass);
            }
            /////if prepoulated settings is passed, we dont have to make a DB/Cache call for EVERY setting class
            //if (prepopulatedAllSettings != null && prepopulatedAllSettings.ToList().Count > 0)
            //    _allSettings = prepopulatedAllSettings;

            var settingClassName = settingClass.GetType().Name.Replace("Model", "");

            //foreach (var prop in typeof(T).GetProperties())
            foreach (var prop in settingClass.GetType().GetProperties())
            {
                // get properties we can read and write to
                if (!prop.CanRead || !prop.CanWrite)
                {
                    continue;
                }

                //this is done because keys naming convention is classname-property. i.e.DomainSettings-DomainName
                var key        = settingClassName + "." + prop.Name;
                var settingKey = settingsForClass.ToList().FirstOrDefault(x => x.Key == key);

                //if teh key does not exist for the domain, move on to the next key
                if (settingKey == null)
                {
                    continue;
                }

                var settingValue = settingKey.Value;
                if (settingValue == null)
                {
                    continue;
                }

                if (!TypeDescriptor.GetConverter(prop.PropertyType).CanConvertFrom(typeof(string)))
                {
                    continue;
                }

                if (!TypeDescriptor.GetConverter(prop.PropertyType).IsValid(settingValue))
                {
                    continue;
                }

                object value = TypeDescriptor.GetConverter(prop.PropertyType).ConvertFromInvariantString(settingValue);

                //set property
                prop.SetValue(settingClass, value, null);
            }

            return(settingClass);
        }
        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            _configSettingModel = services.RegisterConfig(Configuration);
            services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);

            services.RegisterIoc(Configuration);
            services.RegisterOAuth(_configSettingModel);

            services.RegisterSwagger(_configSettingModel);
        }
Esempio n. 4
0
 public static void RegisterOAuth(this IServiceCollection services, ConfigSettingModel configSetting)
 {
     services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
     .AddJwtBearer(options =>
     {
         options.Authority = configSetting.Auth.Authority;
         options.Audience  = configSetting.Auth.CognitoClientId;
         options.TokenValidationParameters = new Microsoft.IdentityModel.Tokens.TokenValidationParameters
         {
             ValidateAudience = false
         };
     });
 }