我有一个login.aspx
页面,其中包含用户名和密码的自定义文本框,即没有loginview
在提供正确的用户名和密码后,我分配了sessionid
,用于访问网站上的其他页面。
现在下载file (1234)
我将用户重定向到~/download.aspx?fileid=1234
,在此页面上,我会检查会话ID并将用户发送到文件网址,即~/file/1234.pdf
。
如果有人直接输入文件网址,那么我无法阻止他
plase指导我如何做到这一点...
P.S。 :我已阅读authentication rule
文件中的web.config
但不知道如何将用户标记为经过身份验证的用户,并在登录时提供正确的用户名和密码。 (我只检查数据库中的用户名和密码并重定向到主页)
答案 0 :(得分:4)
您的身份验证策略相当薄弱。您应该将站点的区域(即此实例中的files目录)与角色绑定,并将用户分配给它们。
然而,要解决更直接的问题,只需禁用外部世界进入文件目录,当它们点击~/download.aspx?fileid=1234
时,只需为它们提供文件即可。您可以在此处找到相关说明:How to properly serve a PDF file
答案 1 :(得分:1)
看看这个 - http://support.microsoft.com/kb/301240
在该文章中查找第4点 - “对事件处理程序进行编码,以便验证用户凭据”,它解释了如何在验证用户后设置身份验证cookie
要查看的代码:
FormsAuthenticationTicket tkt;
string cookiestr;
HttpCookie ck;
tkt = new FormsAuthenticationTicket(1, txtUserName.Value, DateTime.Now,
DateTime.Now.AddMinutes(30), chkPersistCookie.Checked, "your custom data");
cookiestr = FormsAuthentication.Encrypt(tkt);
ck = new HttpCookie(FormsAuthentication.FormsCookieName, cookiestr);
if (chkPersistCookie.Checked)
ck.Expires=tkt.Expiration;
ck.Path = FormsAuthentication.FormsCookiePath;
Response.Cookies.Add(ck);
你能做的是:
1. Enable form authentication in web.config
2. deny anonymous access to downloads folder
3. When user authenticates, set authentication cookie and redirect user to download folder
4. download folder now can only be accessed by logged in user and id
答案 2 :(得分:1)
以下是我在项目中使用的代码
void ServeFile(string fname, bool forceDownload)
{
if(UserHasPermission(fname))
{
DownloadFile(fname,forceDownload);
}
else
{
ShowMessage("You have no permission");
}
}
private void DownloadFile( string fname, bool forceDownload )
{
string path = MapPath( fname );
string name = Path.GetFileName( path );
string ext = Path.GetExtension( path );
string type = "";
// set known types based on file extension
if ( ext != null )
{
switch( ext.ToLower() )
{
case ".htm":
case ".html":
type = "text/HTML";
break;
case ".txt":
type = "text/plain";
break;
case ".doc":
case ".rtf":
type = "Application/msword";
break;
case ".pdf":
type = "Application/pdf";
break;
}
}
if ( forceDownload )
{
Response.AppendHeader( "content-disposition",
"attachment; filename=" + name );
}
if ( type != "" )
Response.ContentType = type;
Response.WriteFile( path );
Response.End();
}