How do I handle all requests with an IHttpHandler? - c#

I'm trying to set up a stripped down web server that passes all URL requests through to a single IHttpHandler, instead of trying to match the URL to the file structure of the server.
I've got the IHttpHandler in there, along with some custom modules, and they're responding as expected when I go directly to my domain, but if I access the site via something like:
http://mysite/some/random/url
I get a 404 file not found error.
I'd assumed removing one of these modules would probably cover it:
<remove name="UrlRoutingModule-4.0" />
<remove name="UrlMappingsModule" />
<remove name="FileAuthorization" />
<remove name="UrlAuthorization" />
But IIS is still trying to match the URLs to the server file structure. I've since removed every module I'm not using and it's still returning 404's.
I have actually done this before, but I can't seem to quite remember or find online quite how I got it working.
I'm now basically out of ideas - anyone?

I added the runAllManagedModulesForAllRequests as per the suggestion from #Alexei Levenkov. While I remember definitely needing to do that, it didn't immediately solve it. After much fiddling about I found that IIS had set:
resourceType="Either"
on the handler. I tested changing it to File, and the problem was fixed for file type URLs, but of course not folder "style" ones. Changing it to:
resourceType="Unspecified"
Fixed the problem for all URLs.

Related

C# - ASP NET.CORE 3.1 - problem with update record's in table

I have a problem after publishing my API for example hosting - I can add and load records - however, I cannot modify and delete records from the array - I use the database via phpmyadmin and unfortunately there is no option to grant permissions.
In the API, I have the entire functionality of user authorization and authentication with token generation - everything works very well on IIS - however, when I release the API into the world and want to connect using a desktop program - there is no possibility to modify records and delete records. When I use the control to modify or delete records, I get the answer: InternalServerError (500).
What could I forget?
Can I give API privileges to everyone in advance for testing?
The problem lies with MySQL and phpmyadmin? How to add permissions?
Additional tips?
Thank you very much for help.
I solved the problem - someone may find a solution, please:
<modules runAllManagedModulesForAllRequests="false">
<remove name="SmoothHandler" />
<remove name="RestfulUrlMapModule" />
<remove name="PlaylistHandler" />
<remove name="BitrateModule" />
<remove name="LiveStreamingHandler" />
</modules>

Handle every request in Application_BeginRequest

As title suggested, how can I achieve that in my webform project? Currently, if a url points towards a file, the server will send the response with that file without ever entering Application_BeginRequest. Even the MapPageRoute method does not change this behavior. Is there a simple solution?
You may wish to set this in your web.config:
<system.webServer>
<modules runAllManagedModulesForAllRequests="true" />
</system.webServer>
So that even static files are processed by ASP.NET.

Using .NET (website project), how can I intercept all incoming image requests and alter their request path?

Thanks for looking.
Background
I have inherited a very old (12 years) .NET website project what uses web form architecture.
This is a very large codebase and, when running on the web server, depends on a very large local resource folder of images. I am working remotely and have been told that the image folder will not be added to the source control; however, I can access it via a network folder (if connected to VPN).
If I hard-code any of the images to use the network path they work fine, but this is obviously not a good solution since there are thousands of images.
Question
Is it possible to intercept any incoming request for an image file and, if the local image folder is not found (i.e. in development on my machine), use the network path URI instead to get the image? If so, what is the correct way to do this?
What I have Tried
I tried intercepting the requests in the Application_BeginRequest method of the global.asax (which I was surprised to find in such an old project) but this does not intercept image requests apparently. My thinking was that I could re-structure the URL there and then comment that code out in production, but this also seems like it wouldn't be a great solution.
Thanks in advance.
If it's WebForms then you can most certainly use a HTTP Handler to achieve this.
public class ImageHandler : IHttpHandler
{
public bool IsReusable
{
get
{
return false;
}
}
public void ProcessRequest(HttpContext context)
{
// Handle your request here, using
// context.Request and System.IO
// to serve the image from network
}
}
Then, you'll need to register & configure the Handler in Web.Config like so, for IIS6:
<system.web>
<httpHandlers>
<add verb="*" path="*.jpg,*.png" type="Namespace.ImageHandler, AssemblyName" validate="false" />
</httpHandlers>
</system.web>
Or IIS7:
<system.webServer>
<handlers>
<add name="JpgImage" verb="*" path="*.jpg" type="Namespace.ImageHandler, AssemblyName" resourceType="File" />
<add name="PngImage" verb="*" path="*.png" type="Namespace.ImageHandler, AssemblyName" resourceType="File" />
</handlers>
</system.webServer>

Error with dot (.) character in URL

I having a issue when i click to edit a user with this url in a ASP.NET MVC 3 project:
http://domain.com:8089/User/EditUser/username.surname?IDUser=e11a621p-df11-4687-9903-8bfc33c922cf
If i get another user without the '.' character, it works fine.
The error:
HTTP Error 404.0 - Not Found
The resource you are looking for has been removed, had its name changed, or is temporarily unavailable.
I tried some tips that i find here, like:
<system.webServer>
<modules runAllManagedModulesForAllRequests="true"/>
and:
<system.web>
<httpRuntime relaxedUrlToFileSystemMapping="true" />
and this attribute on the edituser action:
[ValidateInput(false)]
But nothing seems to work. This site is hosted on a IIS server, when it was on Windows Azure WebSite, it was working as expected.
Thanks.
If you know for a fact that the edit page is the only page where you use the firstname.lastname url part, you can use the method described in this SO answer:
Prevent static file handler from intercepting filename-like URL
Specifically, in your case, adding the following web.config section should route the request to MVC:
<system.webServer>
...
<handlers>
...
<add
name="userEditPage"
path="User/EditUser/*"
verb="GET"
type="System.Web.Handlers.TransferRequestHandler"
preCondition="integratedMode,runtimeVersionv4.0" />
This will not be sufficient if you use the firstname.lastname in urls outside of the User/EditUser/... path, and is not a general solution. That would be much more complicated because you would need to tell IIS something like the following:
1) if the file exists, serve it (so that your .js files still serve properly)
2) Before any of the other handlers execute for the file extension, run the MVC handler and see if there is a route matching the url. Because what if you have a user of last name html?
3) If the MVC handler does not match any routes for the url, let the other handlers. Because what if you also had an .aspx page in your project?
Lastly, for the general case, you may want to consider the edge case of someone malicious creating a user with first name ../../web and lastname config? Just a thought, but it seems like the best you can hope for is restricting the use of the . in the url to specific paths.
After some headache, i publish it to Azure WebSites again and it works normally, with same web.config file that i was using in local enviroment. So the solution must be on the IIS, then after no more tries, i change the Application Pool to Default App Pool and guess what, it worked.

ASMX Webservice - The test form is only available for requests from the local machine

I have an ASMX webservice with two functions. One sends a JSON object to the server and stores it in a file, while the other function retrives the JSON object from the server. The webservice works perfectly in local, but when I try it in a remote server, I get the well known "The test form is only available for requests from the local machine" error.
As suggested in different forums, I have added the protocols to my web.config file:
</system.web>
<webServices>
<protocols>
<add name="HttpSoap12"/>
<add name="HttpSoap"/>
<add name="HttpGet"/>
<add name="HttpPost"/>
</protocols>
</webServices>
</system.web>
Adding the POST protocol makes the second function (the one that retrives the object) available from remote, but the first one is still only available from the local machine.
I haven't been hable to find the solution to this problem, as every solution I found was just to add the protocols in the web.config file, which only work for one of the two functions.
There is one question which seems to have a similar problem, caused by using DateTime type as input parameter. In my case, maybe it has something to do with using a "Object" type as imput for the function? What alternatives do I have if I can't use the Object type?
Any suggestion will be helpful.
Thank you and best regards,
I had this same issue. VS 2012 did not publish the web.config to my published source after I added ws protocols. It worked when I manually copied the file.
Another reason this can happen is that your parameters are more complex objects, including things like GUIDs

Categories