/// <summary>Invoked to configure a TOptions instance.</summary>
        /// <param name="options">The options instance to configure.</param>
        public void Configure(OsharpOptions options)
        {
            SetDbContextOptions(options);

            IConfigurationSection section;

            //OAuth2
            section = _configuration.GetSection("OSharp:OAuth2");
            IDictionary <string, OAuth2Options> dict = section.Get <Dictionary <string, OAuth2Options> >();

            if (dict != null)
            {
                foreach (KeyValuePair <string, OAuth2Options> item in dict)
                {
                    options.OAuth2S.Add(item.Key, item.Value);
                }
            }

            //MailSender
            section = _configuration.GetSection("OSharp:MailSender");
            MailSenderOptions sender = section.Get <MailSenderOptions>();

            if (sender != null)
            {
                if (sender.Password == null)
                {
                    sender.Password = _configuration["OSharp:MailSender:Password"];
                }
                options.MailSender = sender;
            }

            //JwtOptions
            section = _configuration.GetSection("OSharp:Jwt");
            JwtOptions jwt = section.Get <JwtOptions>();

            if (jwt != null)
            {
                if (jwt.Secret == null)
                {
                    jwt.Secret = _configuration["OSharp:Jwt:Secret"];
                }
                options.Jwt = jwt;
            }

            // RedisOptions
            section = _configuration.GetSection("OSharp:Redis");
            RedisOptions redis = section.Get <RedisOptions>();

            if (redis != null)
            {
                if (redis.Configuration.IsMissing())
                {
                    throw new OsharpException("配置文件中Redis节点的Configuration不能为空");
                }
                options.Redis = redis;
            }

            // SwaggerOptions
            section = _configuration.GetSection("OSharp:Swagger");
            SwaggerOptions swagger = section.Get <SwaggerOptions>();

            if (swagger != null)
            {
                if (swagger.Url.IsMissing())
                {
                    throw new OsharpException("配置文件中Swagger节点的Url不能为空");
                }
                options.Swagger = swagger;
            }

            // HttpEncrypt
            section = _configuration.GetSection("OSharp:HttpEncrypt");
            HttpEncryptOptions httpEncrypt = section.Get <HttpEncryptOptions>();

            if (httpEncrypt != null)
            {
                options.HttpEncrypt = httpEncrypt;
            }
        }
Exemplo n.º 2
0
        /// <summary>Invoked to configure a TOptions instance.</summary>
        /// <param name="options">The options instance to configure.</param>
        public void Configure(OsharpOptions options)
        {
            SetDbContextOptions(options);

            IConfigurationSection section;

            //OAuth2
            section = _configuration.GetSection("OSharp:OAuth2");
            IDictionary <string, OAuth2Options> dict = section.Get <Dictionary <string, OAuth2Options> >();

            if (dict != null)
            {
                foreach (KeyValuePair <string, OAuth2Options> item in dict)
                {
                    options.OAuth2S.Add(item.Key, item.Value);
                }
            }

            //MailSender
            section = _configuration.GetSection("OSharp:MailSender");
            MailSenderOptions sender = section.Get <MailSenderOptions>();

            if (sender != null)
            {
                if (sender.Password == null)
                {
                    sender.Password = _configuration["OSharp:MailSender:Password"];
                }
                options.MailSender = sender;
            }

            //JwtOptions
            section = _configuration.GetSection("OSharp:Jwt");
            JwtOptions jwt = section.Get <JwtOptions>();

            if (jwt != null)
            {
                if (jwt.Secret == null)
                {
                    jwt.Secret = _configuration["OSharp:Jwt:Secret"];
                }
                options.Jwt = jwt;
            }

            //CookieOptions
            section = _configuration.GetSection("OSharp:Cookie");
            CookieOptions cookie = section.Get <CookieOptions>();

            if (cookie != null)
            {
                options.Cookie = cookie;
            }

            //MvcOptions
            section = _configuration.GetSection("OSharp:Mvc");
            MvcOptions mvc = section.Get <MvcOptions>();

            if (mvc != null)
            {
                options.Mvc = mvc;
            }

            //CorsOptions
            section = _configuration.GetSection("OSharp:Cors");
            CorsOptions cors = section.Get <CorsOptions>();

            if (cors != null)
            {
                options.Cors = cors;
            }

            // RedisOptions
            section = _configuration.GetSection("OSharp:Redis");
            RedisOptions redis = section.Get <RedisOptions>();

            if (redis != null)
            {
                if (redis.Configuration.IsMissing())
                {
                    throw new OsharpException("配置文件中Redis节点的Configuration不能为空");
                }
                options.Redis = redis;
            }

            // SwaggerOptions
            section = _configuration.GetSection("OSharp:Swagger");
            SwaggerOptions swagger = section.Get <SwaggerOptions>();

            if (swagger != null)
            {
                if (swagger.Endpoints.IsNullOrEmpty())
                {
                    throw new OsharpException("配置文件中Swagger节点的EndPoints不能为空");
                }

                if (swagger.RoutePrefix == null)
                {
                    swagger.RoutePrefix = "swagger";
                }
                options.Swagger = swagger;
            }

            // HttpEncrypt
            section = _configuration.GetSection("OSharp:HttpEncrypt");
            HttpEncryptOptions httpEncrypt = section.Get <HttpEncryptOptions>();

            if (httpEncrypt != null)
            {
                options.HttpEncrypt = httpEncrypt;
            }
        }