Click here to Skip to main content
15,918,808 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello.

I have a webiste which built in asp.net (C#).
Now i have a problem, when user click a link to download our file (such as doc,rar file type), my website could check that : Are they logined?
IF Yes then Download file
else
my website will redirect to login page?

How can i do it?
Please!

Thank for your reading.
-----------------------------------------------------------------------
SOHOA, công nghệ nhận dạng, nhận dạng văn bản, số hóa tài liệu, số hóa văn bản, số hóa dữ liệu, số hóa thông tin, giải pháp số hóa, phần mềm nhận dạng

vtv thể thao, bóng đá, worldcup

Posted
Updated 10-Jun-10 21:06pm
v4

Solution 1 (Quick Way)

Handel the AuthenticateRequest event in the Global.asax file.

protected void Application_AuthenticateRequest(object sender, EventArgs e)
    {
        string url = HttpContext.Current.Request.Url.AbsolutePath;
        string extension = System.IO.Path.GetExtension(url);
        switch (extension.ToLower())
        {
            case ".doc":
            case ".rar":
            case ".zip": //And so on
                if (HttpContext.Current.User == null || !HttpContext.Current.User.Identity.IsAuthenticated)
                    Response.Redirect("Login.aspx");
                  break;
        }
    }



Solution 2 (Better Way)

Store all of your downloadable document in a folder and apply the authentication rules on that.

<configuration>
   <location path="Download">
      <system.web>
         <authorization>
            <allow users="?"/>
         </authorization>
      </system.web>
   </location>
</configuration>



Solution 3 (Best Way)

Write a Http Handler and pass a document identifier to it, it will be the responsibility of the handler to render the file after checking the user credential.
 
Share this answer
 
v2
Comments
thepbac 11-Jun-10 2:49am    
Thank Prakash Kalakoti, your solution are greate. I have solved this problem.
Thank u
You may store a user login information in the database or in a session.

In the download page, check if the user is logged in or not. If logged in the link should point to the file otherwise the link will point to the Login page with download file details in the query string. If the user logs in through the redirected Login page, the query string can be checked and the user can directly be taken to download page from there.

Hope that helps!
 
Share this answer
 
Comments
thepbac 11-Jun-10 2:50am    
Thank Ankur.
Its very usefull for me.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900