Exemplo n.º 1
0
        public static string GenerateJSON_MobileToken(string userid)
        {
            //
            var now    = DateTime.Now;
            var expire = now.AddMonths(1);

            //
            var Issuer = AppSettingHelper.GetStringFromFileJson("appsettings", "Jwt:Issuer_Mobile").Result;

            var securityKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(SecurityModel.Mobile_Key));
            var credentials = new SigningCredentials(securityKey, SecurityAlgorithms.HmacSha256);

            var token = new JwtSecurityToken(
                Issuer,
                Issuer,
                new Claim[]
            {
                new Claim(ClaimTypes.NameIdentifier, userid),
            },
                expires: expire,
                signingCredentials: credentials);

            var mo = new TokenModel()
            {
                Identifier = userid,
                Expires_In = (int)(expire - now).TotalMinutes,
                Token      = new JwtSecurityTokenHandler().WriteToken(token)
            };

            return(JsonConvert.SerializeObject(mo));
        }
Exemplo n.º 2
0
        public static async Task <string> GetLanguageText(string path)
        {
            var region = await AppSettingHelper.GetStringFromAppSetting("Languages");

            path = $"{path}:{region}";
            var text = await AppSettingHelper.GetStringFromFileJson("Languages/languages", path);

            text = string.IsNullOrWhiteSpace(text) ? "" : text;

            return(text);
        }
Exemplo n.º 3
0
        public static async Task <string> GetFileNameNormalize(IFormFile file, string folderpath = "")
        {
            var extension = Path.GetExtension(file.FileName) ?? "";

            var fileName = Path.GetFileName(string.Format("{0}{1}", StringUtilHelper.RemoveSpecialCharactersVn(file.FileName.Replace(extension, "")).GetNormalizeString(), extension));

            var folder = await AppSettingHelper.GetStringFromAppSetting("FileUpload:CustomerFolder");

            var path = string.Format("{0}{1}/{2}", folder, folderpath, fileName);

            return(path);
        }
Exemplo n.º 4
0
        public static async Task <HttpResponseMessage> HttpGet(string uri, string token = "")
        {
            var url = AppSettingHelper.GetStringFromAppSetting("ConnectionStrings:Host_Api").Result + uri;

            try
            {
                client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token);

                return(await client.GetAsync(url));
            }
            catch (System.Exception ex)
            {
                throw ex;
            }
        }
Exemplo n.º 5
0
        public static DataSet ExcuteCommandToDataSet(string command, string db = "DefaultConnection")
        {
            //Chuyển db
            var connect     = AppSettingHelper.GetStringFromFileJson("connectstring", "ConnectionStrings:" + db).Result;
            var connecttype = AppSettingHelper.GetStringFromFileJson("connectstring", "ConnectionStrings:DefaultType").Result;

            switch (connecttype)
            {
            case DatabaseModel.SQLSERVER:
                return(SqlHelper.GetDataSet(connect, command));

            default:

                return(null);
            }
        }
Exemplo n.º 6
0
        public static async Task <HttpResponseMessage> HttpPost <T>(string uri, T obj, string token = "")
        {
            var url = AppSettingHelper.GetStringFromAppSetting("ConnectionStrings:Host_Api").Result + uri;

            try
            {
                client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token);

                var content = JsonConvert.SerializeObject(obj);

                var data = new StringContent(content, Encoding.UTF8, "application/json");

                return(await client.PostAsync(url, data));
            }
            catch (System.Exception ex)
            {
                throw ex;
            }
        }
Exemplo n.º 7
0
        public static List <T> ExcuteCommandToList <T>(string command, string db = "DefaultConnection")
        {
            List <T> list = new List <T>();

            //Chuyển db
            var connect     = AppSettingHelper.GetStringFromFileJson("connectstring", "ConnectionStrings:" + db).Result;
            var connecttype = AppSettingHelper.GetStringFromFileJson("connectstring", "ConnectionStrings:DefaultType").Result;

            switch (connecttype)
            {
            case DatabaseModel.SQLSERVER:

                return(SqlHelper.ExcuteCommandToList <T>(connect, command));

            case DatabaseModel.MYSQL:

                return(MySqlHelper.ExcuteCommandToList <T>(connect, command));

            default:

                return(list);
            }
        }
Exemplo n.º 8
0
        public static Task <List <SY_MenuFunction> > MenuFunctionByUserId(SessionModel user, HttpContext context)
        {
            var connecttype = AppSettingHelper.GetStringFromFileJson("connectstring", "ConnectionStrings:DefaultType").Result;

            //
            var identify = string.Format(CacheConfig.Kz_User_MenuFunctionCache_Key, user.UserId, SecurityModel.Cache_Key);

            var modelCache = new List <SY_MenuFunction>();

            //var cache = context.RequestServices.GetService<IMemoryCache>();

            var existed = CacheFunction.TryGet <List <SY_MenuFunction> >(context, identify, out modelCache);

            //var k = cache.Get<List<SY_MenuFunction>>(identify);

            if (existed == false)
            {
                if (user.isAdmin)
                {
                    //Làm việc với mông => không thể dùng cmd giống như sqlserve, mysql
                    switch (connecttype)
                    {
                    case DatabaseModel.MONGO:

                        var filter = Builders <SY_MenuFunction> .Filter.Eq(n => n.Active, true);

                        var kd = MongoHelper.GetConnect <SY_MenuFunction>().FindAsync(filter);

                        modelCache = kd.Result.ToList();

                        break;

                    default:

                        var cmdMenus = "SELECT * FROM sy_menufunction WHERE Active = 1";

                        modelCache = Kztek_Library.Helpers.DatabaseHelper.ExcuteCommandToList <SY_MenuFunction>(cmdMenus);

                        break;
                    }
                }
                else
                {
                    switch (connecttype)
                    {
                    case DatabaseModel.MONGO:

                        var filterRole = Builders <SY_Map_User_Role> .Filter.Eq(n => n.UserId, user.UserId);

                        var moRoles = MongoHelper.GetConnect <SY_Map_User_Role>().FindAsync(filterRole).Result.ToList();

                        var filterMenu = Builders <SY_Map_Role_Menu> .Filter.In(n => n.RoleId, moRoles.Select(n => n.RoleId));

                        var moMenus = MongoHelper.GetConnect <SY_Map_Role_Menu>().FindAsync(filterMenu).Result.ToList();

                        var filterQueryMenu = Builders <SY_MenuFunction> .Filter.In(n => n.Id, moMenus.Select(n => n.MenuId));

                        modelCache = MongoHelper.GetConnect <SY_MenuFunction>().FindAsync(filterQueryMenu).Result.ToList();

                        break;

                    default:

                        var cmdRole = string.Format("SELECT * FROM sy_map_user_role WHERE UserId = '{0}'", user.UserId);

                        var roles = Kztek_Library.Helpers.DatabaseHelper.ExcuteCommandToList <SY_Map_User_Role>(cmdRole);

                        var str_roles = new List <string>();
                        foreach (var item in roles)
                        {
                            str_roles.Add(string.Format("'{0}'", item.RoleId));
                        }

                        //Danh sách menu của tài khoản với roleids = > danh sách menu
                        var cmdRoleMenus = string.Format("SELECT * FROM sy_map_role_menu WHERE RoleId IN ({0})", roles.Any() ? string.Join(",", str_roles) : "'0'");

                        var rolemenus = Kztek_Library.Helpers.DatabaseHelper.ExcuteCommandToList <SY_Map_Role_Menu>(cmdRoleMenus);

                        //Lấy danh sách menu quyền
                        var menuids = "";
                        var count1  = 0;
                        foreach (var item in rolemenus)
                        {
                            count1++;
                            menuids += string.Format("'{0}'{1}", item.MenuId, count1 == rolemenus.Count ? "" : ",");
                        }

                        var cmdMenus = string.Format("SELECT * FROM sy_menufunction WHERE Active = 1 AND Id IN ({0})", string.IsNullOrWhiteSpace(menuids) ? "'0'" : menuids);

                        modelCache = Kztek_Library.Helpers.DatabaseHelper.ExcuteCommandToList <SY_MenuFunction>(cmdMenus);

                        break;
                    }
                }

                //Save lại vào cache
                if (modelCache == null)
                {
                    modelCache = new List <SY_MenuFunction>();
                }

                CacheFunction.Add <List <SY_MenuFunction> >(context, identify, modelCache, CacheConfig.Kz_User_MenuFunctionCache_Time);
                //cache.Set<List<SY_MenuFunction>>(identify, modelCache, DateTime.Now.AddHours(8));
            }

            return(Task.FromResult(modelCache));
        }