Secure binary files with ASP.NET and open PDF with Browser

Posted: 07-09-2003, 01:29 PM
Open Binary files with ASP.Net
Posted: 07-08-2003 05:47 PM
I am trying to protect PDF files with a form that asks the user to
enter name and password.
Basically I created two pages: login.aspx and openpdf.aspx.

Here is the problem.
After authentication, when the script redirects to openpdf.aspx, I
have to hit the refresh button in order for the PDF to open with my
local acrobat reader. PS: I have acrobat installed correctly. Then
after it opens it, I click back and try to access a pdf manually
typing http://localhost/pdf/test.pdf and it gives me this error with a
messy content.

The server block is not well formed.

Line 3570:r@
Line 3571:1T<ȸѹm
a-߉MKvǀz(j-|BL9~3p.Q3
Line 3572:x*Gn߶dN;|ܛ}6'Ѷ$3'qR< %&4S|qJ~DzR>"ދx/9f
|ʸ'yj= kâٱ ]0'!,ڣp~
ӳq0rN<Qٗc;ޥ`|<\"^\e >PF<?WOcq|"xiorM_a{
"u| = &RK忚6W$dv}*1?X߶i{ #_\Y3Nmc}
Line 3573:6>|d)]";__/9Գ
Line 3574:3%OlOI'
<!=19Gc"&}/>3d,mB=E;bW8{;


Here's my web.config

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.web>

<httpHandlers>
<add verb="*" path="*.pdf" type="System.Web.UI.PageHandlerFactory" />
</httpHandlers>


<authentication mode="Forms">
<forms name="aspxauth" loginUrl="login.aspx" path="/" />
</authentication>

<authorization>
<deny users="?"/>
</authorization>


</system.web>
</configuration>


---------------------This is the validation in login.aspx
--------------------------

private bool ValidateUser(string uid, string passwd)
{
SqlConnection conn;
SqlCommand cmd;
SqlDataReader dr;
conn = new SqlConnection("your connection");
cmd = new SqlCommand("Select * from Sn_RegisteredUsers where
FirstName='" + uid + "'",conn);

conn.Open();
dr = cmd.ExecuteReader();
while (dr.Read())
{

if (string.Compare(dr["Pwd"].ToString(),passwd,false)==0)
{
conn.Close();
return true;
}
}
conn.Close();
return false;
}



if (ValidateUser(txtUserName.Value,txtUserPass.Value) )
{
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;
Response.Cookies.Add(ck);

string strRedirect;
//strRedirect = Request["ReturnUrl"];
if (strRedirect==null)
strRedirect = "openpdf.aspx";
Response.Redirect(strRedirect, true);
}
else
Response.Redirect("login.aspx", true);
}

---------------------------- |
-----------------------------------------

-----------------------This is the openpdf.aspx
code------------------------
<%@ Page Language="C#" %>
<script runat="server">
private void Page_Load(object sender, System.EventArgs e)
{
//Set the appropriate ContentType.
Response.ContentType = "Application/pdf";
//Get the physical path to the file.
string FilePath = MapPath("120-06.pdf");
//Write the file directly to the HTTP content output stream.
Response.WriteFile(FilePath);
Response.End();
}
</script>
------------------------------- |
---------------------------------------------------


I appreciate any help.

Rod
Reply With Quote

Responses to "Secure binary files with ASP.NET and open PDF with Browser"

 
LinkBack Thread Tools Search this Thread Display Modes
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
PDF file doesn't open in web browser for Version 5 but does open for version 6 Kellye_Sliger@adobeforums.com Adobe Acrobat Windows 0 04-01-2004 03:05 PM
Server control button click: open pop up browser window Jim Mitchell ASP.NET Data Grid Control 0 08-23-2003 01:58 AM
download binary files from database Dux PHP Development 7 07-06-2003 12:54 PM
When publishing a Director File to the Internet -- How do you make the browser open in full screen mode??? RFishc607 Macromedia Director Basics 1 07-01-2003 06:53 PM
on click of a link button should first save the data and then open a new browser Mathew George ASP.NET Data Grid Control 1 07-01-2003 06:06 AM