Asp.net mvc 在MVC中,如何确定调用操作?
我有以下控制器,它与返回的图像一起阻止直接链接:Asp.net mvc 在MVC中,如何确定调用操作?,asp.net-mvc,asp.net-mvc-3,razor,Asp.net Mvc,Asp.net Mvc 3,Razor,我有以下控制器,它与返回的图像一起阻止直接链接: public class ImageController : Controller { [HttpGet] public ActionResult Details(string id, string file) { if (null == Request.UrlReferrer || Request.UrlReferrer.Authority != Request.Url.Autho
public class ImageController : Controller
{
[HttpGet]
public ActionResult Details(string id, string file)
{
if (null == Request.UrlReferrer ||
Request.UrlReferrer.Authority != Request.Url.Authority ||
Request.UrlReferrer.AbsolutePath.ToLower() != Url.Action("Photos", "Home", new { id = id }).ToLower())
{
return base.File(Server.MapPath("/Content/images/notfound.jpg"));
}
// else process and return image
}
}
这个管用,但有点味道。有什么MVC ier可以告诉我的吗?起初我对ControllerContext抱有希望,但事实并非如此
从我的观点来看,我是这样称呼这一行动的:
@foreach (var item in Model.Items)
{
<li>
<img src='@Url.Action("Details", "Image", new { id = Model.Project, file = item.ThumbnailPath })' />
</li>
}
@foreach(Model.Items中的变量项)
{
}
在您的视图中生成一个包含id+文件+日期时间的加密字符串。在img src@Url.Action中传递此加密字符串
在控制器中,解密此字符串以获取id&file&datetime。如果datetime大于15秒,则不要提供图像。如果密钥未成功解密,请不要为图像提供服务
由于加密逻辑是私有的,其他人无法伪造请求,并且由于加密密钥中包含datetime,您可以在10-15秒内使链接过期,因此其他人无法热链接到您的图像
希望这是有意义的
@foreach (var item in Model.Items) {
<li>
<img src='@Url.Action("Details", "Image", new { encryptedString= item.encryptedString})' />
</li> }
@foreach(Model.Items中的变量项){
}
公共类ImageController:控制器
{
[HttpGet]
公共操作结果详细信息(字符串加密字符串)
{
尝试
{
string[]值=DescryptString(encryptedString);
//值[0]=id
//值[1]=文件
//值[2]=日期时间
如果(日期时间差<10秒)
返回过程和返回图像;
else//链接已过期
返回base.File(Server.MapPath(“/Content/images/notfound.jpg”);
}
捕获(解码异常)
{
//伪造请求
返回base.File(Server.MapPath(“/Content/images/notfound.jpg”);
}
}
}
整个东西都有轻微的气味。这是不赞成的;许多用户不发送Referer
。我相信Opera在默认情况下根本不会发送Referer
。很高兴你同意。我的问题不仅仅是将3行替换为1行,而是要避免URLreferer及其所有问题。有趣的解决方法。我猜隐含的答案是你不能决定呼叫动作。
public class ImageController : Controller
{
[HttpGet]
public ActionResult Details(string encryptedString)
{
try
{
string[] values = DescryptString(encryptedString);
// values[0] = id
// values[1] = file
// values[2] = datetime
if(dateTime difference < 10 seconds )
return process and return image ;
else // link expired
return base.File(Server.MapPath("/Content/images/notfound.jpg"));
}
catch(DecodingException e)
{
// forged request
return base.File(Server.MapPath("/Content/images/notfound.jpg"));
}
}
}