管道模型图片防盗链简单实现

webconfig 集成配置采用hander注入

MyHanders代码实现

 this.IsReusable = true;
            var httpRequest = context.Request;
            var urlReferrer = httpRequest.UrlReferrer;
            if (urlReferrer == null || string.IsNullOrEmpty(urlReferrer?.ToString()))
            {
                context.Response.ContentType = "image/jpeg";
                context.Response.WriteFile("~/image/404.jpg");
            }
            else
            {
                if (urlReferrer.Host.Contains("localhost"))
                {
                    string urlPath = httpRequest.AppRelativeCurrentExecutionFilePath;
                    string urlPathValues = context.Server.MapPath(urlPath);
                    if (System.IO.File.Exists(urlPathValues))
                    {
                        context.Response.ContentType = "image/jpeg";
                        context.Response.WriteFile(httpRequest.AppRelativeCurrentExecutionFilePath);
                    }
                    else
                    {
                        context.Response.ContentType = "image/jpeg";
                        context.Response.WriteFile("~/image/404.jpg");
                    }
                }
            }
View Code
原文地址:https://www.cnblogs.com/zxp6/p/11133070.html