1

There are static files in my website like:

http://myurl.com/Content/a.html

http://myurl.com/Content/b.html

... etc

I want to verify the user's authentication when access. The authentication should be check by database's data.

My framework:ASP.NET MVC5

Community
  • 1
  • 1
Steven Chou
  • 1,504
  • 2
  • 21
  • 43
  • Ideally it should be redirected via Controller and Action method where you can implementing Authentication & Authorization. – Souvik Ghosh Dec 30 '16 at 03:54
  • I've trying by this [link](http://stackoverflow.com/questions/41357311/net-mvc-how-to-hide-the-true-url) , but the javascript file from static html was relative path, so when I use http://myurl.com/Content/a.html as source in my Action it will get javascript file from http://myurl.com/MyController/MyAction/abc.js, it display wrong.. – Steven Chou Dec 30 '16 at 03:57

4 Answers4

2

Make sure users are authenticated by using the authorization element in your web.config. If all your static pages are in a folder named StaticPages, add a web.config file to that folder and insert the code below.

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

https://msdn.microsoft.com/en-us/library/8d82143t(v=vs.85).aspx

KRob
  • 389
  • 3
  • 19
2

Create web.config file in your static folder and insert this code.

<configuration>
  <system.web>
    <authorization>
      <deny users="?"/>
    </authorization>
  </system.web>
</configuration>
0

Instead of putting your files in the traditional method /myserver/myfile.html, consider adding an action which returns the file after authinticating the user:

[Authorize]
public ActionResult GetFile(string name)
{
   // return the file
}

You may also use OutputCache attribute to cache your static file so that it is not being requested every time (if you are sure your file contents will not be changed)

FindOut_Quran
  • 728
  • 3
  • 10
  • 27
0

I finally solved this problem by this way, a important thing is I must to replace the static file's relative path with absolute path.

Community
  • 1
  • 1
Steven Chou
  • 1,504
  • 2
  • 21
  • 43