NewTech
NewTech

Reputation: 326

Encrypting URL in ASP.Net MVC 4

I used the below guide to encrypt the URL in my mvc app. I do see that the values are encrypted but I am getting the IIS error saying the page or resource you are looking for is not found. I m thinking it has something to do with the routing. I m completely new to MVC so I tried couple of different route combinations but nothing works. To add to this, when debugging I cant get my Application_Start event to get hit,so i m not able to debug it as well. My route.config file is given below. Could somebody please help me with the issue ?

The URL that is generated after the encryption is this. My route.config file is given below.

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 }
        );

    }

please let me know where am I doing wrong.

My action is below.

 [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");
    }

Upvotes: 0

Views: 3241

Answers (2)

NewTech
NewTech

Reputation: 326

I finally found the answer. The problem was my website was setup in IIS and the URL that it was setup with was with the virtual directory name. so the url for home was http://localhost/VDName/Home/Login but the url generated for the invoice that i gave above was missing the VD name so that is why it was throwing 404 error. I changed my URL to remove the VD name and it works fine now. Thank you Chris for your time :)

Upvotes: 0

Chris Pratt
Chris Pratt

Reputation: 239220

First, you only need custom routes when the param you're capturing is part of the actual URL path. Here, q is part of the query string, so no special routing is required.

Second, you should spend some time reviewing the docs at http://www.asp.net/mvc/overview/controllers-and-routing. The action field of the anonymous object you pass to defaults, merely tells the routing framework what action to load if the action portion of the URL is not included. For example, in the standard default route:

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

If you attempt to load the URL, /Home, MVC will load the Index action of HomeController. Likewise, if you leave off the controller, as well, (i.e. just /, MVC will again load HomeController.Index, because those are the defaults. Since all of your routes here use the same URL pattern, only the first one is ever utilized. The rest are skipped entirely.

All that said, it shouldn't really matter. In the URL, you're passing both a controller and action name, so the first route should match that and load up HomeController.GetInvoice. Since the q param is in the query string, it has no effect on routing, but will be passed to a param named q on your action. Therefore assuming you have an action like:

public class HomeController : Controller
{
    ...

    public ActionResult GetInvoice(string q)
    {
        ...
    }
}

Then, your routing is just fine. There must be some place in the GetInvoice action where you're returning a 404, and that's where your code is falling through. In general, the best way to figure out what's happening is to debug and add a break point at the start of the action you expect to hit. If the breakpoint is not triggered, then it's an issue with your routing, but if you do hit the breakpoint, then you can walk through your code line by line and figure out exactly where things are going awry.

Upvotes: 1

Related Questions