2015-07-21 76 views
1

在我们的Sitecore解决方案中下载媒体项目时,我们一直面临一个问题。我们想要认证下载媒体的用户。如果用户无权访问该文件,我们需要重定向该用户/取消下载。Sitecore - 根据请求获取有关媒体项目的信息

决定重定向的东西是该媒体项目(带ID的字段)的元数据。

我们做了一个模块:

<add type="Lib.CustomMediaRequestSessionModule, Lib" name="CustomMediaRequestSessionModule" /> 

代码:

public class CustomMediaRequestSessionModule : IHttpModule 
{ 
    public void Init(HttpApplication application) 
    { 
     application.BeginRequest += Application_BeginRequest; 
    } 

    private void Application_BeginRequest(object source, EventArgs e) 
    { 
     var application = (HttpApplication)source; 
     var currentContext = HttpContext.Current; 

     if (currentContext.Request.Url.ToString().ToLower().Contains("/~/media/")) 
     { 
      //Here we want to authenticate the user 
     } 
    } 

    public void Dispose() 
    { 
    } 
} 

我们从请求得到的唯一信息是文件路径。 “/~/media/path/to/file.doc”。

是否有另一种方式从媒体项目获取更多信息?在Sitecore中执行同样的操作是否有更好的方法?

回答

4

您可以使用MediaManager.ParseMediaRequest方法来获取媒体项目:

MediaRequest request = MediaManager.ParseMediaRequest(HttpContext.Current.Request); 

if (request == null) 
{ 
    return false; 
} 

Media media = MediaManager.GetMedia(request.MediaUri); 

有一个很好的博客文章,解释如何在Sitecore的限制媒体项目在这里Restricting access to Sitecore Media Items