繁体   English   中英

如何允许已登录的用户能够直接从ASP.NET MVC应用程序下载文件

[英]How can I allow logged in users be able to direct download file from an ASP.NET MVC application

我有一个ASP.NET MVC应用程序,我希望登录的用户能够在应用程序路径内下载一些文件,并避免未登录的用户下载文件。

我希望此文件夹位于项目文件夹的根目录中

这取决于您要如何实现此方案:

第一种情况:您可以将下载链接放在这些代码块中,以防止显示给未经授权的用户。

查看页面:

 @if (Utility.CheckActionPermission("ActionName", "ControllerName", "AreaName"))
            {
                 // your download link should be here               
            }

控制器:

public static bool CheckActionPermission(string actionName, string controllerName, string areaName)
    {
        var accessUrl = string.Concat(areaName, "/", controllerName, "/", actionName);
        return ((CustomPrincipal)HttpContext.Current.User).Access.Any(a => a.Url == accessUrl);
    }

第二种情况:将所有链接自由显示给每个用户,但是当单击下载链接时,您需要验证用户的权限:

视图:

@Html.ActionLink("File Name", "DownloadFile", "ControllerName", new { fileName= @Model.FileName }, null)

调节器

    [Authorize]
    public static bool DownloadFile(string fileName)
    {
        var filePath = Path.Combine(PathConstants.DownloadFolder, fileName);

        //some code to download the file 
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM