簡體   English   中英

ASP.NET Web應用程序內的HTML內容授權

[英]HTML content authorization inside asp.net web application

我有一個同事用純HTML制作了一個網站,並帶有少量CSS和JavaScript。 我有一項特殊的任務來防止使用簡單的登錄表單來顯示此頁面。 我的同事將繼續使用他的html進行工作,但不允許他編寫任何asp.net或在遠程服務器上發布內容。

我是一個asp.net開發人員,我的第一個想法是將他的所有內容包含在Web應用程序項目的名為“內容”的文件夾中。 然后,我制作了簡單的登錄表單(login.aspx),並在web.config中放入了身份驗證(將login.aspx作為loginUrl)和授權標簽。 之后,我將整個項目發布到遠程服務器,並與該用戶共享“ Content”文件夾。 他將有權訪問所有html頁面,並且只需將他更新的或新創建的html文件復制到該文件夾​​即可繼續進行處理。

當我在Visual Studio Web開發服務器上本地運行授權和身份驗證時,整個工作就很好了。 當我嘗試訪問存儲在“內容”文件夾中的某些html內容時,我將重定向到login.apsx,並且一切正常。

當我將此完整的asp.net Web應用程序發布到遠程服務器時,我遇到了問題。 當我嘗試訪問相同的html內容時,我沒有重定向到login.aspx,並且無需身份驗證即可訪問“內容”中的所有html頁面。

這是我的web.config的身份驗證和授權部分:

  <authentication mode="Forms">
      <forms loginUrl="login.aspx"/>
    </authentication>

    <authorization>
      <deny users="?"/>
    </authorization>

這是我的login.aspx.cs的內容:

if (//USER WEB SERVICE CHECK)
{

        if (Request.QueryString["ReturnUrl"] == null)
        {
            FormsAuthentication.SetAuthCookie(UserName.Text, true);
            Response.Redirect("~/Content/index.html");
        }
        else
        {
            FormsAuthentication.RedirectFromLoginPage(UserName.Text, true);
        }
    }
    else
    {
        FailureText.Text = "Wrong username or password...";
    }
}

您有更好的主意如何使用asp.net嗎? 為什么我發布此表單身份驗證不起作用? 當整個內容發布時,是否有可能阻止訪問純html內容(作為Web應用程序的一部分)?

我在asp.net項目中經常使用相同的原理,並且在同一台遠程服務器上也可以正常工作。 我什至嘗試將其放置在另一台服務器上,但效果相同。

我的Web應用程序項目的結構如下:

ApplicationFolder
 |
  - login.aspx
 |
 - web.config
 |
 - CONTENT
        |
         - index.html
         - ...

另外,我嘗試將一些aspx內容放入CONTENT文件夾中,並且身份驗證重定向也可以正常工作。 甚至可以通過用戶表單身份驗證來保護Web應用程序中的html內容嗎?

遠程服務器使用IIS 6,ASP.NET運行時不處理html文件,因此,表單身份驗證無法正常工作。 我已經將html文件擴展名重命名為aspx,並且一切正常。 我現在對此解決方案感到滿意,但是如果有人有更好的解決方案,請在此處寫下...

我已經閱讀到IIS 6(http://forums.asp.net/t/1184547.aspx)上的網站配置有解決方法,但是我的服務器上不允許這樣做。

我認為您最好檢查服務器上該應用程序的IIS設置,與本地應用程序應該有所不同,您需要對其進行更改。

打開IIS,在站點樹視圖中單擊有問題的項目。 選擇“身份驗證”菜單,然后檢查是否啟用了表單身份驗證。 如果這不起作用,請嘗試禁用Windows身份驗證。

如果有人和我有同樣的問題,我終於解決了這個問題。

無論如何,我已經按照http://forums.asp.net/t/1184547.aspx中所述更改了IIS 6設置,並使用下面的代碼編寫了自定義請求處理程序

public class DocHandler : IHttpHandler
{

    public DocHandler() { }
    public void ProcessRequest(HttpContext context)
    {
        string path = context.Request.PhysicalPath;
        string name = path.Split('\\')[path.Split('\\').Length - 1];
        if (!string.IsNullOrEmpty(path) && path.ToLower().EndsWith(".pdf"))
        {
            context.Response.ClearHeaders();
            context.Response.ClearContent();
            context.Response.Clear();
            context.Response.Charset = null;
            context.Response.ContentType = "application/pdf";
            context.Response.AddHeader("Content-Type", "application/pdf");
            context.Response.AppendHeader("Content-Disposition", string.Format("inline;filename={0}", name));
            context.Response.WriteFile(path);
        }
        else if (!string.IsNullOrEmpty(path) && path.ToLower().EndsWith(".doc"))
        {
            context.Response.ClearHeaders();
            context.Response.ClearContent();
            context.Response.Clear();
            context.Response.Charset = null;
            context.Response.ContentType = "application/msword";
            context.Response.AddHeader("Content-Type", "application/msword");
            context.Response.AppendHeader("Content-Disposition", string.Format("inline;filename={0}", name));
            context.Response.WriteFile(path); 
        }
        else if (!string.IsNullOrEmpty(path) && path.ToLower().EndsWith(".xls"))
        {
            context.Response.ClearHeaders();
            context.Response.ClearContent();
            context.Response.Clear();
            context.Response.Charset = null;
            context.Response.ContentType = "application/vnd.ms-excel";
            context.Response.AddHeader("Content-Type", "application/vnd.ms-excel");
            context.Response.AppendHeader("Content-Disposition", string.Format("inline;filename={0}", name));
            context.Response.WriteFile(path);
        }
        else if (!string.IsNullOrEmpty(path) && path.ToLower().EndsWith(".ppt"))
        {
            context.Response.ClearHeaders();
            context.Response.ClearContent();
            context.Response.Clear();
            context.Response.Charset = null;
            context.Response.ContentType = "application/vnd.ms-powerpoint";
            context.Response.AddHeader("Content-Type", "application/vnd.ms-powerpoint");
            context.Response.AppendHeader("Content-Disposition", string.Format("inline;filename={0}", name));
            context.Response.WriteFile(path);                
        }
        else if (!string.IsNullOrEmpty(path) && path.ToLower().EndsWith(".html"))
        {        

            context.Response.ClearHeaders();
            context.Response.ClearContent();
            context.Response.Clear();
            context.Response.Charset = null;
            context.Response.ContentType = "text/html";
            context.Response.AddHeader("Content-Type", "text/html");
            context.Response.AppendHeader("Content-Disposition", string.Format("inline;filename={0}", name));
            context.Response.WriteFile(path);
        }
        else
        {
            throw new System.IO.FileNotFoundException("The page requested is invalid", path);
        }
    }
    public bool IsReusable { get { return false; } }
}

最后將以下部分添加到web.config

<add verb="GET" path="*.pdf" type="PartnerPortal.DocHandler" validate="false" />
<add verb="GET" path="*.doc" type="PartnerPortal.DocHandler" validate="false" />
<add verb="GET" path="*.xls" type="PartnerPortal.DocHandler" validate="false" />
<add verb="GET" path="*.ppt" type="PartnerPortal.DocHandler" validate="false" />
<add verb="*" path="*.html" type="PartnerPortal.DocHandler" validate="false" />

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM