[英]How to restrict static file that only authenticated user can access?
不允許用戶訪問HTML文件,只有經過身份驗證的用戶才能訪問HTML文件。 我已經在web.config文件中添加了身份驗證,但這不起作用。
<location path="test">
<system.web>
<authorization>
<allow users="?"/>
<deny users="*"/>
</authorization>
</system.web>
</location>
當我將此代碼放入Web.config文件中時,只有未經身份驗證的用戶才能訪問HTML文件。 當我通過身份驗證以登錄應用程序時,無法訪問HTML文件。
此代碼僅拒絕訪問,不允許訪問經過身份驗證的用戶的“測試”文件夾。
提前致謝 !!!
好吧,我有辦法用httphandlers做到這一點。
首先將一個類添加到您的項目中,並實現IHttpHandler類。 然后將此示例代碼添加到ProcessRequest
方法。
if (!HttpContext.Current.User.Identity.IsAuthenticated )
{
HttpContext.Current.Response.Write("//No authenticated access to static files are allowed");
return;
}
var requestedFile=File.ReadAllText(HttpContext.Current.Request.PhysicalPath);
HttpContext.Current.Response.ContentType = "text/html";
HttpContext.Current.Response.Write(requestedFile);
當然,不要忘了將此引用添加到Web配置中。
<add verb="*" path="*.html" type="YourNamespace.YourClassName, YourNameSpace" name="JS" />
希望能幫助到你。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.