2012-04-02 34 views
3

我有以下情况,我想问问什么是最佳解决方案?如何确保仅由特定用户下载的链接?

我有一个特定的文件我想要特定用户(根据一些权限)下载此文件。

所以我只为授权用户显示此文件,但如果有人(未经授权)识别文件链接(知道链接url)并下载它!

如何让此文件只能由authorized users下载。

+1

IIS,Apache?你正在使用哪个Web服务器? – Shai 2012-04-02 08:46:25

+0

您是否尝试不使用非授权情况下的下载标签? – Pankaj 2012-04-02 08:46:49

+0

IIS,我打算把文件放在我的web应用程序中的一个文件夹中 – 2012-04-02 08:47:42

回答

3

将文件放入Web服务器未提供的目录中,并为“虚拟url”实施处理程序,该虚拟url进而检查权限等。 - 可能的方法是ASHX处理程序(请参阅here代码和here MSDN参考)。

+0

你可以更多地解释你的意思吗?文件放到一个不是由Web服务器提供的目录中? – 2012-04-02 09:01:00

+1

@just_name我的意思是把文件放到IIS不知道的地方,这样任何人“猜测”链接都无法到达文件,因为IIS不知道它。然后你的处理程序在检查权限后通过访问那个地方来提供文件。 – Yahia 2012-04-02 09:04:55

+0

“通用处理程序”是否可以访问iis以外的文件 – 2012-04-02 09:08:14

1

,最好的办法是添加HttpHandlers的,检查请求的文件是否有特殊权限或没有,一个例子为我所说的是:

 using Microsoft.VisualBasic; 
using System; 
using System.Collections; 
using System.Collections.Generic; 
using System.Data; 
using System.Diagnostics; 

public class MyHTTPHandler : IHttpHandler, IRequiresSessionState 
{ 

string myFile; 
public bool IsReusable { 
    get { return true; } 
} 

public void ProcessRequest(System.Web.HttpContext context) 
{ 
    myFile = context.Request.Path; 
    if (myFile.ToLower().Contains("members private files") || myFile.ToLower().Contains("members%20private%20files")) { 
     if (System.Web.HttpContext.Current.Session["Login"] == null) { 
      context.Response.Redirect("~/NotAuthorized.aspx"); 
     } else { 
      if (myFile.ToLower().Contains("privatefiles")) { 
       StartDownload(context, myFile); 
      } else { 
       if (IsMemberAuthoraizedToDownloadFile(context)) { 
        StartDownload(context, myFile); 
       } else { 
        context.Response.Redirect("~/NotAuthorized.aspx"); 
       } 
      } 
     } 
    } else { 
     StartDownload(context, myFile); 
    } 
} 

private void StartDownload(HttpContext context, string downloadFile) 
{ 
    context.Response.Buffer = true; 
    context.Response.Clear(); 
    context.Response.AddHeader("content-disposition", "attachment; filename=" + downloadFile); 
    context.Response.ContentType = "application/pdf"; 
    context.Response.WriteFile(downloadFile); 
} 

// just my own function to check if user is valid 

private bool IsMemberAuthoraizedToDownloadFile(HttpContext context) 
{ 
    GroupMembersControl MyGroupMemberc = new GroupMembersControl(); 
    System.Collections.Generic.List<GroupMembers> MemberGroupsL = MyGroupMemberc.GetMemberGroups(System.Web.HttpContext.Current.Session["Login"]); 
    MemberGroupControl MyGroupC = new MemberGroupControl(); 
    MemberGroup MyGroup = default(MemberGroup); 
    foreach (GroupMembers groupmember in MemberGroupsL) { 
     MyGroup = MyGroupC.GetMemberGroup(groupmember.GroupID); 
     if (myFile.ToLower().Contains(MyGroup.Name.ToLower)) { 
      return true; 
     } 
    } 
    return false; 
} 
    } 
2

我的答案是: 不要使用直接链接!

创建Download.aspx并有链接,下载张贴到Download.aspx?PARAMS

的PARAMS应该是加密/散列含有文件路径+名称下载和SESSION_ID。

On Download.aspx验证session_id在浏览器上有效且处于活动状态。

这应该让你允许下载到只有正确的乡亲:

如果添加到PARAMS也Download.aspx

1

的onLoad user_ID的,也可以拒绝USER_TYPE /许可证下载以下链接提供了关于iis和asp.net授权规则的详细信息,它似乎与您的问题有关。

首先你要确保ASP.NET处理你指定文件类型的请求。你可以在IIS中进行配置(参见下面的链接)。

其次,您将需要更新你的web.config拒绝匿名用户到达您的网址,为您提供您所使用rolemanager:

<roleManager defaultProvider="SqlProvider" enabled="true" cacheRolesInCookie="false"  
    cookieName=".ASPROLES" cookieTimeout="30" cookiePath="/" cookieRequireSSL="false" 
    cookieSlidingExpiration="true" cookieProtection="All"> 
    <providers> 
    <add name="SqlProvider" type="System.Web.Security.SqlRoleProvider" 
     connectionStringName="membership" applicationName="yourApplication"/> 
    </providers> 
</roleManager> 



<location path="path/file.extension"> 
     <system.web> 
     <authorization> 
     <deny users="?"/> 
     </authorization> 
    </system.web> 
    </location> 

IIS 6 ignores Web.config authorization settings

相关问题