I would like to handle requests differently depending upon the MIME type. For example, I have PDF's, images and other media files that I would like to prohibit access to based on their respective MIME types. Any ideas on how to do this? Thanks for the help.
我想根据MIME类型处理不同的请求。例如,我有PDF,图像和其他媒体文件,我想根据各自的MIME类型禁止访问。关于如何做到这一点的任何想法?谢谢您的帮助。
I should also note that accessing the Windows registry is not an option for my application.
我还应该注意,访问Windows注册表不是我的应用程序的选项。
5 个解决方案
#1
10
.NET's mime-type mappings are stored in the System.Web.MimeMapping
class which offers the GetMimeMapping method.
.NET的mime类型映射存储在System.Web.MimeMapping类中,该类提供GetMimeMapping方法。
Prior to .NET 4.5, this class was marked as internal
, and thus not available to your code. In that case the best you can do is steal the list, which you can get using Reflector and decompile the static constructor (cctor).
在.NET 4.5之前,此类已标记为内部,因此您的代码无法使用。在这种情况下,您可以做的最好的事情是窃取列表,您可以使用Reflector并反编译静态构造函数(cctor)。
If taking that approach, you may be better off simply creating a list of supported extensions and their mime type and storing it on a dictionary. (The list inside MimeMapping is a tad verbose)
如果采用这种方法,最好只创建一个受支持的扩展名列表及其mime类型并将其存储在字典中。 (MimeMapping中的列表有点冗长)
#2
9
I had a similar problem a few month ago and solved it with this simple wrapper-class around System.Web.MimeMapping (as mentioned by Richard Szalay):
几个月前我遇到了类似的问题,用System.Web.MimeMapping这个简单的包装器解决了它(如Richard Szalay所说):
/// <summary>
/// This class allows access to the internal MimeMapping-Class in System.Web
/// </summary>
class MimeMappingWrapper
{
static MethodInfo getMimeMappingMethod;
static MimeMappingWrapper() {
// dirty trick - Assembly.LoadWIthPartialName has been deprecated
Assembly ass = Assembly.LoadWithPartialName("System.Web");
Type t = ass.GetType("System.Web.MimeMapping");
getMimeMappingMethod = t.GetMethod("GetMimeMapping", BindingFlags.Static | BindingFlags.NonPublic);
}
/// <summary>
/// Returns a MIME type depending on the passed files extension
/// </summary>
/// <param name="fileName">File to get a MIME type for</param>
/// <returns>MIME type according to the files extension</returns>
public static string GetMimeMapping(string fileName) {
return (string)getMimeMappingMethod.Invoke(null, new[] { fileName });
}
}
#3
8
Cross-posting from Why would Reflection search suddenly not find anything?
交叉发布为什么反射搜索突然找不到任何东西?
The good news is that the MimeMapping
class and its GetMimeMapping
method seem like they might be made public in .NET 4.5.
好消息是MimeMapping类及其GetMimeMapping方法看起来好像可以在.NET 4.5中公开。
However, this means that the code given in the above answer would break, since it’s only searching for GetMimeMapping
among NonPublic
methods.
但是,这意味着上述答案中给出的代码会中断,因为它只在NonPublic方法中搜索GetMimeMapping。
To ensure compatibility with .NET 4.5 (but preserve functionality in .NET 4.0 and earlier), change…
为确保与.NET 4.5的兼容性(但保留.NET 4.0及更早版本中的功能),请更改...
getMimeMappingMethod = t.GetMethod("GetMimeMapping",
BindingFlags.Static | BindingFlags.NonPublic);
…to:
getMimeMappingMethod = t.GetMethod("GetMimeMapping",
BindingFlags.Static | BindingFlags.NonPublic | BindingFlags.Public);
#4
1
This information is in the registry, in HKEY_CLASSES_ROOT\<file_extension>\Content Type
此信息位于注册表中,HKEY_CLASSES_ROOT \
using(var key = Registry.ClassesRoot.OpenSubKey(".htm"))
{
string mimeType = key.GetValue("Content Type") as string;
}
#5
0
If I am understanding your question properly, you are serving static files and want to be able to do processing on a static file request in order to decide whether or not the user has access to that file. (based on MIME type)
如果我正确理解您的问题,您正在提供静态文件,并希望能够对静态文件请求进行处理,以确定用户是否有权访问该文件。 (基于MIME类型)
If you map all files requests through a custom IHttpHandler (see the handlers section of your web.config file), you should be able to accomplish this.
如果您通过自定义IHttpHandler映射所有文件请求(请参阅web.config文件的处理程序部分),您应该能够完成此任务。
In ProcessRequest (or BeginProcessRequest if you implement an asynchronous handler), you can call HttpContext.Current.Server.MapPath("~" + HttpContext.Current.Request.Path) (might be a better way to do that) to get the current static file being requested.
在ProcessRequest(或BeginProcessRequest,如果你实现了一个异步处理程序),你可以调用HttpContext.Current.Server.MapPath(“〜”+ HttpContext.Current.Request.Path)(可能是一个更好的方法)来获取当前请求静态文件。
You can then analyze the extension of that file to make your decision.
然后,您可以分析该文件的扩展名以做出决定。
Not sure if thats what you want, but hopefully it helps
不确定那是不是你想要的,但希望它有所帮助
#1
10
.NET's mime-type mappings are stored in the System.Web.MimeMapping
class which offers the GetMimeMapping method.
.NET的mime类型映射存储在System.Web.MimeMapping类中,该类提供GetMimeMapping方法。
Prior to .NET 4.5, this class was marked as internal
, and thus not available to your code. In that case the best you can do is steal the list, which you can get using Reflector and decompile the static constructor (cctor).
在.NET 4.5之前,此类已标记为内部,因此您的代码无法使用。在这种情况下,您可以做的最好的事情是窃取列表,您可以使用Reflector并反编译静态构造函数(cctor)。
If taking that approach, you may be better off simply creating a list of supported extensions and their mime type and storing it on a dictionary. (The list inside MimeMapping is a tad verbose)
如果采用这种方法,最好只创建一个受支持的扩展名列表及其mime类型并将其存储在字典中。 (MimeMapping中的列表有点冗长)
#2
9
I had a similar problem a few month ago and solved it with this simple wrapper-class around System.Web.MimeMapping (as mentioned by Richard Szalay):
几个月前我遇到了类似的问题,用System.Web.MimeMapping这个简单的包装器解决了它(如Richard Szalay所说):
/// <summary>
/// This class allows access to the internal MimeMapping-Class in System.Web
/// </summary>
class MimeMappingWrapper
{
static MethodInfo getMimeMappingMethod;
static MimeMappingWrapper() {
// dirty trick - Assembly.LoadWIthPartialName has been deprecated
Assembly ass = Assembly.LoadWithPartialName("System.Web");
Type t = ass.GetType("System.Web.MimeMapping");
getMimeMappingMethod = t.GetMethod("GetMimeMapping", BindingFlags.Static | BindingFlags.NonPublic);
}
/// <summary>
/// Returns a MIME type depending on the passed files extension
/// </summary>
/// <param name="fileName">File to get a MIME type for</param>
/// <returns>MIME type according to the files extension</returns>
public static string GetMimeMapping(string fileName) {
return (string)getMimeMappingMethod.Invoke(null, new[] { fileName });
}
}
#3
8
Cross-posting from Why would Reflection search suddenly not find anything?
交叉发布为什么反射搜索突然找不到任何东西?
The good news is that the MimeMapping
class and its GetMimeMapping
method seem like they might be made public in .NET 4.5.
好消息是MimeMapping类及其GetMimeMapping方法看起来好像可以在.NET 4.5中公开。
However, this means that the code given in the above answer would break, since it’s only searching for GetMimeMapping
among NonPublic
methods.
但是,这意味着上述答案中给出的代码会中断,因为它只在NonPublic方法中搜索GetMimeMapping。
To ensure compatibility with .NET 4.5 (but preserve functionality in .NET 4.0 and earlier), change…
为确保与.NET 4.5的兼容性(但保留.NET 4.0及更早版本中的功能),请更改...
getMimeMappingMethod = t.GetMethod("GetMimeMapping",
BindingFlags.Static | BindingFlags.NonPublic);
…to:
getMimeMappingMethod = t.GetMethod("GetMimeMapping",
BindingFlags.Static | BindingFlags.NonPublic | BindingFlags.Public);
#4
1
This information is in the registry, in HKEY_CLASSES_ROOT\<file_extension>\Content Type
此信息位于注册表中,HKEY_CLASSES_ROOT \
using(var key = Registry.ClassesRoot.OpenSubKey(".htm"))
{
string mimeType = key.GetValue("Content Type") as string;
}
#5
0
If I am understanding your question properly, you are serving static files and want to be able to do processing on a static file request in order to decide whether or not the user has access to that file. (based on MIME type)
如果我正确理解您的问题,您正在提供静态文件,并希望能够对静态文件请求进行处理,以确定用户是否有权访问该文件。 (基于MIME类型)
If you map all files requests through a custom IHttpHandler (see the handlers section of your web.config file), you should be able to accomplish this.
如果您通过自定义IHttpHandler映射所有文件请求(请参阅web.config文件的处理程序部分),您应该能够完成此任务。
In ProcessRequest (or BeginProcessRequest if you implement an asynchronous handler), you can call HttpContext.Current.Server.MapPath("~" + HttpContext.Current.Request.Path) (might be a better way to do that) to get the current static file being requested.
在ProcessRequest(或BeginProcessRequest,如果你实现了一个异步处理程序),你可以调用HttpContext.Current.Server.MapPath(“〜”+ HttpContext.Current.Request.Path)(可能是一个更好的方法)来获取当前请求静态文件。
You can then analyze the extension of that file to make your decision.
然后,您可以分析该文件的扩展名以做出决定。
Not sure if thats what you want, but hopefully it helps
不确定那是不是你想要的,但希望它有所帮助