Esempio n. 1
0
 public override void OnAuthorization(AuthorizationContext filterContext)
 {
     LazyInitializer.EnsureInitialized(ref mRoles, ref mIsInitialized, ref mInitializerLock, () => {
         mRoles = new List <Role>();
         FCStoreDbContext db = new FCStoreDbContext();
         mRoles = db.Roles.ToList();
         StringBuilder tmpSB = new StringBuilder();
         tmpSB.Append(',');
         HashSet <string> tmpSet = new HashSet <string>();
         foreach (Role role in mRoles)
         {
             string[] tmpStrArr = role.Description.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
             foreach (string PStr in tmpStrArr)
             {
                 if (!tmpSet.Contains(PStr))
                 {
                     tmpSB.Append(PStr + ",");
                     tmpSet.Add(PStr);
                 }
             }
         }
         mAllPermission = tmpSB.ToString();
         return(mRoles);
     });
     mControllerName = filterContext.ActionDescriptor.ControllerDescriptor.ControllerName;
     mActionName     = filterContext.ActionDescriptor.ActionName;
     base.OnAuthorization(filterContext);
 }
Esempio n. 2
0
        protected void Application_Start()
        {
            AreaRegistration.RegisterAllAreas();

            //增加全局的Filter用于记录用户的Tracker
            GlobalFilters.Filters.Add(new UserTrackerLogAttribute());

            WebApiConfig.Register(GlobalConfiguration.Configuration);
            FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
            RouteConfig.RegisterRoutes(RouteTable.Routes);
            BundleConfig.RegisterBundles(BundleTable.Bundles);

            //从文件加载省份,城市,区域到数据库
            FCStoreDbContext db = new FCStoreDbContext();

            if (db.Province.Count() == 0)
            {
                //省份不存在
                string tmpFP = ConfigurationManager.AppSettings["PCTFilePath"];
                tmpFP = Server.MapPath(tmpFP);
                FileInfo tmpFI = new FileInfo(tmpFP);
                if (tmpFI.Exists)
                {
                    FileStream tmpFS  = tmpFI.OpenRead();
                    int        FLen   = (int)tmpFI.Length;
                    byte[]     buffer = new byte[FLen];
                    //格式:<Province><PName>北京</PName><PPC>22</PPC><CityArr><City><CName>北京市</CName><CPC>44</CPC><TownArr><TName>南山区</TName><TPC>66</TCP></TownArr></City></CityArr></Province>
                    if (tmpFS.Read(buffer, 0, FLen) > 0)
                    {
                        string          tmpStr    = Encoding.Unicode.GetString(buffer);
                        string          ProRgxStr = "<Province>\\s*?<PName>\\s*?(?<PName>\\w+?)\\s*?</PName>\\s*?<PPC>\\s*?(?<PPC>\\d+?)\\s*?</PPC>\\s*?<CityArr>\\s*?(?<PContent>.+?)\\s*?</CityArr>\\s*?</Province>";
                        Regex           ProRgx    = new Regex(ProRgxStr, RegexOptions.Singleline | RegexOptions.IgnoreCase);
                        MatchCollection tmpMC     = ProRgx.Matches(tmpStr);
                        foreach (Match tmpMatch in tmpMC)
                        {
                            Province tmpPro = new Province();
                            tmpPro.Name      = tmpMatch.Groups["PName"].Value;
                            tmpPro.PostCode1 = tmpMatch.Groups["PPC"].Value;
                            if (tmpPro.CityArr == null)
                            {
                                tmpPro.CityArr = new List <City>();
                            }
                            string          cityStr    = tmpMatch.Groups["PContent"].Value;
                            string          CityRgxStr = "<City>\\s*?<CName>\\s*?(?<CName>\\w+?)\\s*?</CName>\\s*?<CPC>\\s*?(?<CPC>\\d+?)\\s*?</CPC>\\s*?<TownArr>\\s*?(?<CContent>.+?)\\s*?</TownArr>\\s*?</City>";
                            Regex           cityRgx    = new Regex(CityRgxStr, RegexOptions.Singleline | RegexOptions.IgnoreCase);
                            MatchCollection tmpMC1     = cityRgx.Matches(cityStr);
                            foreach (Match tmpMatch1 in tmpMC1)
                            {
                                City tmpCity = new City();
                                tmpCity.Name      = tmpMatch1.Groups["CName"].Value;
                                tmpCity.PostCode2 = tmpMatch1.Groups["CPC"].Value;
                                if (tmpCity.TownArr == null)
                                {
                                    tmpCity.TownArr = new List <Town>();
                                }
                                string          townStr    = tmpMatch1.Groups["CContent"].Value;
                                string          TownRgxStr = "<Town>\\s*?<TName>\\s*?(?<TName>\\w+?)\\s*?</TName>\\s*?<TPC>\\s*?(?<TPC>\\d+?)\\s*?</TPC>\\s*?</Town>";
                                Regex           townRgx    = new Regex(TownRgxStr, RegexOptions.Singleline | RegexOptions.IgnoreCase);
                                MatchCollection tmpMC2     = townRgx.Matches(townStr);
                                foreach (Match tmpMatch2 in tmpMC2)
                                {
                                    Town tmpTown = new Town();
                                    tmpTown.Name      = tmpMatch2.Groups["TName"].Value;
                                    tmpTown.PostCode3 = tmpMatch2.Groups["TPC"].Value;
                                    tmpCity.TownArr.Add(tmpTown);
                                    db.Town.Add(tmpTown);
                                }
                                tmpPro.CityArr.Add(tmpCity);
                                db.City.Add(tmpCity);
                            }
                            db.Province.Add(tmpPro);
                        }
                        if (tmpMC.Count > 0)
                        {
                            db.SaveChanges();
                        }
                    }
                    tmpFS.Close();
                }
            }

            db.Dispose();

            //注册RouteDebug
            //RouteDebug.RouteDebugger.RewriteRoutesForTesting(RouteTable.Routes);
        }