如何处理未经授权的URL重定向到错误页面

时间:2015-07-15 07:08:32

标签: asp.net asp.net-mvc url unauthorized

在Asp.net mvc 4 / mvc 5中如何处理未经授权的url重定向到错误页面。 请任何人给出建议

2 个答案:

答案 0 :(得分:0)

私有GetDataModel ModelEmpty(GetDataModel模型)         {             ShowData data = new ShowData();             data.SRNO = model.Id;             if(model.Name!= null)             data.Name = model.Name;             if(model.Res!= null)                 data.Res = model.Res;             if(model.Tel!= null)                 data.Tel = model.Tel;             if(model.Ch!= null)                 data.Ch = model.Ch;             if(model.Res_Off!= null)                 data.Res_Off = model.Res_Off;
            if(model.Off!= null)             data.Off_Address = model.Off;             if(model.Email!= null)             data.Email = model.Email;             if(model.Mobile!= null)                 data.Mobile = model.Mobile;             if(model.EnrolmentNoofBarCouncil!= null)                 data.EnrolmentNoofBarCouncil = model.EnrolmentNoofBarCouncil;             if(model.DHCBAMembershipNo!= null)             data.DHCBAMembershipNo = model.DHCBAMembershipNo;             if(model.Fax!= null)                 data.Fax = model.Fax;

        db.ShowDatas.Add(data);
        db.SaveChanges();
        model.Id = 0;
        model.Name = "";
        model.Mobile = "";
        model.Off = "";
        model.Res_Off = "";
        model.Res = "";
        model.Tel = "";
        model.Fax = "";
        model.Email = "";
        model.DHCBAMembershipNo = "";
        model.EnrolmentNoofBarCouncil = "";
        model.Ch = "";            
        return model;

    }       

    public ActionResult About()        
    {          
        GetDataModel model2 = new GetDataModel();
        //string path = HttpContext.Server.MapPath("~/HtmlFiles/A1.htm");
       // string path = HttpContext.Server.MapPath("~/HtmlFiles/B.htm");
        string directoryPath = HttpContext.Server.MapPath("~/HtmlFiles");
        string[] fileEntries = Directory.GetFiles(directoryPath);
        //get the page
        foreach (var fileName  in fileEntries)
        {
        var web = new HtmlWeb();
        var document = web.Load(fileName);
        var page = document.DocumentNode;
        var count = page.QuerySelectorAll("span").Count();
        var htmlArray = page.QuerySelectorAll("span").ToArray();
        for (int i = 0; i < count; i++)
        {
            var item = htmlArray[i];
            if (item.InnerText!="" && item.FirstChild.Name != "img" && item.FirstChild.Name != "hr")
            {


                if (item.Attributes["class"].Value != "ft1" && item.Attributes["class"].Value != "ft2" && item.Attributes["class"].Value != "ft3")
                {
                    if (item.Attributes["class"].Value == "ft0" && item.InnerText != " ")
                    {
                        int value;

                        if (int.TryParse(item.InnerText, out value))
                        {
                            var id = Convert.ToInt32(item.InnerText);                              
                                model2.Id = id;
                        }
                        else
                        {
                            var Name = item.InnerText;
                            if (Name != "Name  &amp; Address")
                                model2.Name = Name.Trim();
                        }
                    }



                    if (item.Attributes["class"].Value == "ft4" && item.InnerText != " ")
                    {
                        if (item.InnerText == "Res:")
                        {
                            int value;

                                string Res = htmlArray[i + 1].InnerHtml.Contains(":") ? htmlArray[i - 1].InnerHtml : htmlArray[i + 1].InnerHtml;
                                if (!int.TryParse(htmlArray[i + 2].InnerText, out value) && !htmlArray[i + 2].InnerHtml.Contains("skype_c2c_print_container notranslate"))
                                {
                                    if (htmlArray[i + 2].InnerText.Trim().Length > 6)
                                    {
                                        if (!int.TryParse(htmlArray[i + 2].InnerText.Trim().Substring(0, 4), out value))
                                        Res = Res + (htmlArray[i + 2].InnerHtml.Contains(":") ? "" : htmlArray[i + 2].InnerHtml);
                                    }
                                    else
                                        Res = Res + (htmlArray[i + 2].InnerHtml.Contains(":") ? "" : htmlArray[i + 2].InnerHtml);
                                }

                                if (!htmlArray[i + 3].InnerHtml.Contains(":") && !int.TryParse(htmlArray[i + 3].InnerText.Trim().Substring(0, 4), out value) && !htmlArray[i + 3].InnerHtml.Contains("skype_c2c_print_container notranslate"))
                                    Res = Res + (htmlArray[i + 3].InnerHtml.Contains(":") ? "" : htmlArray[i + 3].InnerHtml);

                                model2.Res = Res.Trim();


                        }
                        if (item.InnerText == "Ch:")
                        {
                            string CH = htmlArray[i + 1].InnerHtml.Contains(":") ? htmlArray[i - 1].InnerHtml : htmlArray[i + 1].InnerHtml;
                            CH = CH + (htmlArray[i + 2].InnerHtml.Contains(":") ? "" : htmlArray[i + 2].InnerHtml);
                            if (!(htmlArray[i + 2].InnerHtml.Contains(":")))
                                CH = CH + (htmlArray[i + 3].InnerHtml.Contains(":") ? "" : htmlArray[i + 3].InnerHtml);
                            model2.Ch = CH.Trim();
                        }

                        if (item.InnerText.Trim() == "Res/Off.:")
                        {
                            string Res_Off =string.Empty;//htmlArray[i + 1].InnerHtml.Contains(":") ? htmlArray[i - 1].InnerHtml.Contains(":")?"":htmlArray[i - 2].InnerHtml.Contains(":") ? htmlArray[i - 1].InnerHtml :htmlArray[i - 3].InnerHtml.Contains(":")?htmlArray[i - 2].InnerHtml+""+htmlArray[i - 1].InnerHtml:"" : htmlArray[i + 1].InnerHtml;
                            if (htmlArray[i - 3].Attributes["class"].Value!="ft0")
                                Res_Off = Res_Off + htmlArray[i - 3].InnerText;
                            if (htmlArray[i - 2].Attributes["class"].Value != "ft0")
                                Res_Off =Res_Off+htmlArray[i - 2].InnerText;
                            if (htmlArray[i - 1].Attributes["class"].Value != "ft0")
                                Res_Off = Res_Off + htmlArray[i - 1].InnerText;
                           // Res_Off = Res_Off + (htmlArray[i + 2].InnerHtml.Contains(":") ? "" : htmlArray[i + 2].InnerHtml);
                            //if (!(htmlArray[i + 2].InnerHtml.Contains(":")))
                               // Res_Off = Res_Off + (htmlArray[i + 3].InnerHtml.Contains(":") ? "" : htmlArray[i + 3].InnerHtml);
                            model2.Res_Off = Res_Off.Trim();
                        }
                        if (item.InnerText == "Tel:")
                        {
                            string Tel = (htmlArray[i + 1].InnerHtml.Contains(":") ? htmlArray[i - 1].InnerHtml : htmlArray[i + 1].InnerHtml).Trim();

                            if (Tel.Contains("skype_c2c_print_container notranslate"))
                            {

                                string html="<span class='skype_c2c_print_container notranslate'>";
                                var vals = Tel.Substring(html.Length+1,12);
                                model2.Tel = model2.Tel + vals + ",";
                            }

                            if (!Tel.Contains("skype_c2c_print_container notranslate") && Tel.Contains(",")&& Tel.Length>4)
                            {
                                string []telArr = Tel.Split(',');
                                int value;
                                if (telArr[0].Length > 4)
                                model2.Tel = model2.Tel + (int.TryParse(telArr[0].Trim().Substring(0, 4), out value) ? int.TryParse(telArr[0].Trim().Substring(0, 4), out value) ? Tel.Trim() : "" : "") + ",";
                            }
                            else
                                if (!Tel.Contains("skype_c2c_print_container notranslate") && Tel.Contains("/") && Tel.Length>4)
                                {
                                    string[] telArr = Tel.Split('/');
                                    int value;
                                    model2.Tel = model2.Tel + (int.TryParse(telArr[0].Trim().Substring(0, 4), out value) ? int.TryParse(telArr[0].Trim().Substring(0,4), out value) ? Tel : "" : "") + ",";
                                }
                                   //else
                                   // if (!Tel.Contains("skype_c2c_print_container notranslate") && Tel.Contains(" "))
                                   // {
                                   //     string[] telArr = Tel.Split(' ');
                                   //     int value;
                                   //     model2.Tel = model2.Tel + (int.TryParse(telArr[0].Trim().Substring(0, 4), out value) ? int.TryParse(telArr[0].Trim().Substring(0,4), out value) ? Tel : "" : "") + ",";
                                   // }
                            else
                            {
                                int value;
                                model2.Tel = model2.Tel+(int.TryParse(Tel, out value) ? Tel.Trim() : "")+",";
                            }
                               // model2.Tel =model2.Tel+ Tel+",";
                        }
                        if (item.InnerText == "Fax:")
                        {
                            model2.Fax = htmlArray[i + 1].InnerText;
                        }

                        if (item.InnerText == "Off:")
                        {
                                string Off = htmlArray[i + 1].InnerHtml.Contains(":") ? htmlArray[i - 1].InnerHtml : htmlArray[i + 1].InnerHtml;
                                if (!htmlArray[i + 2].InnerHtml.Contains("skype_c2c_print_container notranslate") && !(htmlArray[i + 1].InnerHtml.Contains(":")))
                                    Off = Off + (htmlArray[i + 2].InnerHtml.Contains(":") ? "" : htmlArray[i + 2].InnerHtml);
                                int value;
                                if (!int.TryParse(htmlArray[i + 3].InnerText, out value)&&!htmlArray[i + 3].InnerHtml.Contains("skype_c2c_print_container notranslate") && !(htmlArray[i + 2].InnerHtml.Contains(":")))
                                    Off = Off + (htmlArray[i + 3].InnerHtml.Contains(":") ? "" : htmlArray[i + 3].InnerHtml);

                                model2.Off = Off.Trim();


                        }

                        if (item.InnerText.Contains("E-mail:"))
                        {   
                            string Email = htmlArray[i].InnerText.Replace("E-mail:","").Trim();                              
                              model2.Email = Email;
                        }
                        if (item.InnerText.Contains("Mobile:"))
                        {
                            string Mobile = htmlArray[i].InnerHtml.Replace("Mobile:","").Trim();
                            model2.Mobile = Mobile.Trim();
                        }

                        if (item.InnerText.Contains("Enrolment No.of Bar Council:"))
                        {
                            string EnrolmentNoofBarCouncil = htmlArray[i].InnerText.Replace("Enrolment No.of Bar Council:","").Trim();
                            model2.EnrolmentNoofBarCouncil = EnrolmentNoofBarCouncil.Trim();
                        }
                        if (item.InnerText.Contains("D.H.C.B.A.  Membership  No.:"))
                        {
                            string DHCBAMembershipNo = htmlArray[i].InnerText.Replace("D.H.C.B.A.  Membership  No.:","").Trim();
                            model2.DHCBAMembershipNo = DHCBAMembershipNo.Trim();
                            if (model2.Tel!=null && model2.Tel.Length > 1)
                                 model2.Tel = model2.Tel.Substring(0, model2.Tel.Length - 1).Trim();
                             if (model2.Tel!=null && model2.Tel!="" && model2.Tel.ToCharArray()[0] == ',')
                             {
                                 model2.Tel = model2.Tel.Length==1?"": model2.Tel.Substring(1, model2.Tel.Length-1);
                             }

                             if (model2.Tel != null && model2.Tel != "" && model2.Tel.ToCharArray()[model2.Tel.Length-1] == ',')
                             {
                                 model2.Tel = model2.Tel.Length == 1 ? "" : model2.Tel.Substring(0, model2.Tel.Length - 1);
                             }
                                ListModel.Add(model2);
                                model2 = ModelEmpty(model2);

                        }

                    }


                }
            }

        }
        }

        return View();
    }

答案 1 :(得分:-1)

这是来自xml文件的检查URL。 Xml文件由userName生成,或者userName是int类型。

public class LogActionFilter:ActionFilterAttribute
{
   public override void OnActionExecuting(ActionExecutingContext filterContext)
   {
       bool access= Log("OnActionExecuting", filterContext.RouteData);
       if (access == true)
       {
           //filterContext.Result=new RedirectResult("~/User/not_assigned");
           filterContext.Result = new RedirectToRouteResult(
           new RouteValueDictionary 
           { 
               { "controller", "Error" }, 
               { "action", "error" } 
            });
       }
   }      

   private bool Log(string methodName, RouteData routeData)
   {
       var controllerName = routeData.Values["controller"];
       var actionName = routeData.Values["action"];
       var message = String.Format("{0} controller:{1} action:{2}", methodName, controllerName, actionName);
       int id = Convert.ToInt32(HttpContext.Current.User.Identity.Name);
       XmlDocument doc = new XmlDocument();
       string path=HttpContext.Current.Server.MapPath("~/XMLFiles/"+id+".xml");
       doc.Load(path);
       XmlNodeList nodes=doc.SelectNodes("/Modules/Module");
       foreach (XmlNode node in nodes)
       {
           XmlNodeList cnodes = node.ChildNodes;
           foreach (XmlNode cnode in cnodes)
           {
               if (cnode.HasChildNodes)
               {
                   foreach (XmlNode chnode in cnode.ChildNodes)
                   {
                       if (chnode.Attributes["controller"].InnerText.ToString().Equals(controllerName.ToString(), StringComparison.InvariantCultureIgnoreCase) && chnode.Attributes["action"].InnerText.ToString().Equals(actionName.ToString(), StringComparison.InvariantCultureIgnoreCase))
                       {
                           string access = chnode.Attributes["access"].InnerText;
                           if (access.Equals("false", StringComparison.InvariantCultureIgnoreCase))
                           {
                               return true;
                           }
                       }
                   }
               }
               else
               {
                   if (cnode.Attributes["controller"].InnerText.ToString().Equals(controllerName.ToString(), StringComparison.InvariantCultureIgnoreCase) && cnode.Attributes["action"].InnerText.ToString().Equals(actionName.ToString(), StringComparison.InvariantCultureIgnoreCase))
                   {
                       string access = cnode.Attributes["access"].InnerText;
                       if (access.Equals("false", StringComparison.InvariantCultureIgnoreCase))
                       {
                           return true;
                       }
                   }
               }
           }
       }   
       return false;
   }   

}