加密ASP.Net MVC 4中的URL

时间:2016-04-11 17:37:09

标签: asp.net-mvc encryption routing

我使用以下指南加密我的mvc应用中的网址。我确实看到值已加密但我收到IIS错误,说明找不到您要查找的页面或资源。我认为它与路由有关。我对MVC是全新的,所以我尝试了几种不同的路由组合,但没有任何作用。要添加到这个,在调试时我不能让我的Application_Start事件被命中,所以我也无法调试它。 我的route.config文件如下所示。有人可以帮我解决这个问题吗?

加密后生成的URL是这个。我的route.config文件如下所示。

http://localhost/Home/GetInvoice?q=ZDmgxmJVjTEfvmkpyl8GGWP4XHfvR%2fyCFJlJM1s0vKj4J6lYJWyEA4QHBMb7kUJq

http://www.dotnettrace.net/2013/09/encrypt-and-decrypt-url-in-mvc-4.html

 public static void RegisterRoutes(RouteCollection routes)
    {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

        routes.MapRoute(
         name: "Test",
         url: "{controller}/{action}/{id}",
         defaults: new { controller = "Home", action = "TESTVIEW", id = 
  UrlParameter.Optional }
     );

        routes.MapRoute(
        name: "Invoice",
        url: "{controller}/{action}/{q}",
        defaults: new { controller = "Home", action = "GetInvoice", id = 
  UrlParameter.Optional }
       );   

        routes.MapRoute(
           name: "Error",
           url: "{controller}/{action}/{id}",
           defaults: new { controller = "Home", action = "Error", id = 
  UrlParameter.Optional }
       );
        routes.MapRoute(
           name: "ResetPassword",
           url: "{controller}/{action}/{id}",
           defaults: new { controller = "Home", action = "ResetPassword", id 
  = UrlParameter.Optional }
       );
        routes.MapRoute(
          name: "Accounts",
          url: "{controller}/{action}/{id}",
          defaults: new { controller = "Home", action = "AccountStatus", id    
= UrlParameter.Optional }
      );

        routes.MapRoute(
           name: "Register",
           url: "{controller}/{action}/{id}",
           defaults: new { controller = "Home", action = "Register", id =   
 UrlParameter.Optional }
       );              


        routes.MapRoute(
            name: "Default",
            url: "{controller}/{action}/{id}",
            defaults: new { controller = "Home", action = "Login", id =    
UrlParameter.Optional }
        );

    }

请让我知道我在哪里做错了。

我的行动如下。

 [EncryptedActionParameter]
    public FileStreamResult GetInvoice(string accountNumber,string dueDate)
    {
        // Set up the document and the MS to write it to and create the PDF 
 writer instance
        //MemoryStream ms = new MemoryStream();
        //Document document = new Document(PageSize.A4.Rotate());
        //PdfWriter writer = PdfWriter.GetInstance(document, ms);            

        // Set up fonts used in the document
        Font font_heading_1 = FontFactory.GetFont(FontFactory.TIMES_ROMAN, 
 19, Font.BOLD);
        Font font_body = FontFactory.GetFont(FontFactory.TIMES_ROMAN, 9);

        SBLWebSite.SBLCustomerService.CustomerServiceClient client = new 
 SBLWebSite.SBLCustomerService.CustomerServiceClient();

        byte[] file = client.GetInvoice(accountNumber, dueDate).Invoice;
        MemoryStream output = new MemoryStream();
        output.Write(file, 0, file.Length);
        output.Position = 0;

        HttpContext.Response.AppendHeader("content-disposition", "inline; 
  filename=file.pdf");

        // Return the output stream
        return new FileStreamResult(output, "application/pdf");
    }

2 个答案:

答案 0 :(得分:1)

首先,当您捕获的参数是实际URL路径的一部分时,您只需要自定义路由。这里,q是查询字符串的一部分,因此不需要特殊的路由。

其次,您应该花一些时间在http://www.asp.net/mvc/overview/controllers-and-routing审核文档。传递给defaults的匿名对象的操作字段仅告诉路由框架,如果未包含URL的操作部分,则要加载哪个操作。例如,在标准默认路由中:

routes.MapRoute(
    name: "Default",
    url: "{controller}/{action}/{id}",
    defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);

如果您尝试加载网址/Home,MVC将加载Index的{​​{1}}操作。同样地,如果你也离开控制器,(即只是HomeController,MVC将再次加载/,因为这些是默认值。由于这里的所有路由都使用相同的URL模式,第一个被利用。其余的被完全跳过。

所有这一切,它并不重要。在URL中,您同时传递了控制器和操作名称,因此第一个路径应与该路径匹配并加载HomeController.Index。由于HomeController.GetInvoice param位于查询字符串中,因此它对路由没有影响,但会在您的操作中传递给名为q的参数。因此,假设您有类似的行为:

q

然后,你的路由就好了。 public class HomeController : Controller { ... public ActionResult GetInvoice(string q) { ... } } 操作中必须有一些位置,您可以在其中返回404,以及您的代码所在的位置。一般来说,弄清楚发生了什么的最好方法是在你期望击中的动作开始时调试并添加一个断点。如果断点没有被触发,那么你的路由就会出现问题,但是如果你确实遇到了断点,那么你可以逐行浏览你的代码,找出出错的地方。

答案 1 :(得分:0)

我终于找到了答案。问题是我的网站是在IIS中设置的,它设置的URL是虚拟目录名称。所以家里的网址是http://localhost/VDName/Home/Login,但我上面给出的发票生成的网址缺少VD名称,这就是为什么它会抛出404错误。我更改了我的URL以删除VD名称,它现在工作正常。 谢谢克里斯的时间:)