Example #1
0
        /// <summary>
        /// 获取基于JWT的Token
        /// </summary>
        /// <param name="claims">需要在登陆的时候配置</param>
        /// <returns></returns>
        public static TokenInfoViewModel BuildJwtToken(Claim[] claims)
        {
            var symmetricKeyAsBase64 = "sdfsdfsrty45634kkhllghtdgdfss345t678fs";
            var keyByteArray         = Encoding.ASCII.GetBytes(symmetricKeyAsBase64);
            var signingKey           = new SymmetricSecurityKey(keyByteArray);
            var Issuer   = AppsettingHelper.GetValue(new string[] { "Audience", "Issuer" });
            var Audience = AppsettingHelper.GetValue(new string[] { "Audience", "Audience" });

            var signingCredentials = new SigningCredentials(signingKey, SecurityAlgorithms.HmacSha256);
            var now = DateTime.Now;
            // 实例化JwtSecurityToken
            var jwt = new JwtSecurityToken(
                issuer: Issuer,
                audience: Audience,
                claims: claims,
                notBefore: now,
                expires: now.Add(new TimeSpan(60)),
                signingCredentials: signingCredentials
                );
            // 生成 Token
            var encodedJwt = new JwtSecurityTokenHandler().WriteToken(jwt);

            //打包返回前台
            var responseJson = new TokenInfoViewModel
            {
                success    = true,
                token      = encodedJwt,
                expires_in = 60,
                token_type = "Bearer"
            };

            return(responseJson);
        }
Example #2
0
        public static void AddCorsSetup(this IServiceCollection services)
        {
            if (services == null)
            {
                throw new ArgumentNullException(nameof(services));
            }

            services.AddCors(c =>
            {
                c.AddPolicy("LimitRequests", policy =>
                {
                    // 支持多个域名端口,注意端口号后不要带/斜杆:比如localhost:8000/,是错的
                    // 注意,http://127.0.0.1:1818 和 http://localhost:1818 是不一样的,尽量写两个
                    policy
                    .WithOrigins(AppsettingHelper.GetValue(new string[] { "Startup", "Cors", "IPs" }).Split(','))
                    .AllowAnyHeader()//Ensures that the policy allows any header.
                    .AllowAnyMethod();
                });

                // 允许任意跨域请求,也要配置中间件
                //c.AddPolicy("AllRequests",policy=> {
                //    policy.AllowAnyOrigin();
                //    policy.AllowAnyMethod();
                //    policy.AllowAnyHeader();
                //});
            });
        }
        public RedisCacheManager()
        {
            string redisConfiguration = AppsettingHelper.GetValue(new string[] { "AppSettings", "RedisCachingAOP", "ConnectionString" });//获取连接字符串

            if (string.IsNullOrWhiteSpace(redisConfiguration))
            {
                throw new ArgumentException("redis config is empty", nameof(redisConfiguration));
            }
            this.redisConnenctionString = redisConfiguration;
            this.redisConnection        = GetRedisConnection();
        }
Example #4
0
        public static List <MutiDBOperate> MutiInitConn()
        {
            List <MutiDBOperate> listdatabase = AppsettingHelper.GetValue <MutiDBOperate>("DBS")
                                                .Where(i => i.Enabled).ToList();

            foreach (var i in listdatabase)
            {
                // SpecialDbString(i);
            }
            List <MutiDBOperate> listdatabaseSimpleDB = new List <MutiDBOperate>();//单库

            return(listdatabase);
        }
        public static void AddSqlsugarSetup(this IServiceCollection services)
        {
            if (services == null)
            {
                throw new ArgumentNullException(nameof(services));
            }

            // 默认添加主数据库连接
            MainDb.CurrentDbConnId = AppsettingHelper.GetValue(new string[] { "MainDB" });
            // 把多个连接对象注入服务,这里必须采用Scope,因为有事务操作
            services.AddScoped <ISqlSugarClient>(o =>
            {
                // 连接字符串
                var listConfig = new List <ConnectionConfig>();
                // 从库
                var listConfig_Slave = new List <SlaveConnectionConfig>();


                BaseDBConfig.MutiConnectionString.ForEach(m =>
                {
                    listConfig.Add(new ConnectionConfig()
                    {
                        ConfigId              = m.ConnId.ObjToString().ToLower(),
                        ConnectionString      = m.Connection,
                        DbType                = (DbType)m.DbType,
                        IsAutoCloseConnection = true,
                        IsShardSameThread     = false,
                        MoreSettings          = new ConnMoreSettings()
                        {
                            IsAutoRemoveDataCache = true
                        },
                        AopEvents = new AopEvents()
                        {
                            // OnLogExecuting = (sql, p) => { Console.WriteLine(sql); },
                            OnLogExecuting = (sql, pars) =>
                            {
                                Console.WriteLine(sql);
                                Console.WriteLine(string.Join(",", pars?.Select(it => it.ParameterName + ":" + it.Value)));
                            },
                        },
                        // 从库
                        SlaveConnectionConfigs = listConfig_Slave,
                        //InitKeyType = InitKeyType.SystemTable
                    }
                                   );
                });
                return(new SqlSugarClient(listConfig));
            });
        }