Click here to Skip to main content
15,887,862 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi,

In Asp .net application i need to write a code for denying folder and images access when directly users trying to access, but same images need to display in webpage.

I used Below code in web.config, its blocking access to images , but not able to display in web page also.


XML
<system.webServer>

    <modules>
      <remove name="FormsAuthenticationModule" />
      <add name="FormsAuthenticationModule" type="System.Web.Security.FormsAuthenticationModule" />

      <remove name="UrlAuthorization" />
      <add name="UrlAuthorization" type="System.Web.Security.UrlAuthorizationModule" />
    </modules>                

XML
</system.webServer>


XML
<location path="Photos/photos2016">
    <system.web>
      <authorization>               
        <deny users="*"/> 
      </authorization>

    </system.web>
  </location>


What I have tried:

XML
<system.webServer>

    <modules>
      <remove name="FormsAuthenticationModule" />
      <add name="FormsAuthenticationModule" type="System.Web.Security.FormsAuthenticationModule" />

      <remove name="UrlAuthorization" />
      <add name="UrlAuthorization" type="System.Web.Security.UrlAuthorizationModule" />
    </modules>   </system.webServer> 


XML
<location path="awards/awards2015">
  <system.web>
    <authorization>
      <deny users="*"/>
    </authorization>

  </system.web>
</location>
Posted
Updated 19-Dec-16 21:58pm
v2

1 solution

Make a page which will get the ressource, like if using Webforms could be GetGraphics.aspx?ressourceid=mypicture.png which then requires an active session or a cookie.

If you use mvc you can do the same thing with routing ... which ultimately can also be set in place in web.forms like for instance

C#
public class UriActionFilter : System.Web.Mvc.ActionFilterAttribute
{
    public override void OnActionExecuting(System.Web.Mvc.ActionExecutingContext filterContext)
    {
        if (System.Threading.Thread.CurrentPrincipal.Identity.IsAuthenticated)
        {
            // Sample: somehow identify the user, in case of a custom identity, replace the below line to get the user identifier
            var user = System.Threading.Thread.CurrentPrincipal.Identity.Name;

            // get the parameter that will let you know what is the image or path that is being requested now
            var ctxParams = filterContext.ActionParameters;

            // if the user does not have the permission to view, return 403
            filterContext.RequestContext.HttpContext.Response.StatusCode = 403;
            return;
        }
        base.OnActionExecuting(filterContext);
    }
}

Which i picked up as a reply here: c# - ASP .NET MVC - files access - Stack Overflow[^]
 
Share this answer
 

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