【问题标题】:HTML content authorization inside asp.net web applicationasp.net web应用程序中的HTML内容授权
【发布时间】:2012-02-21 16:58:16
【问题描述】:

我有一个同事用简单的 html 和一点点 css 和 javascript 制作了一个网站。 我有一个特殊的任务是用简单的登录表单来阻止这个页面。我的同事将继续处理他的 html,但不允许他编写任何 asp.net 或在远程服务器上发布内容。

我是一名 asp.net 开发人员,我的第一个想法是将他的所有内容包含在名为“Content”的 Web 应用程序项目的文件夹中。然后我制作了简单的登录表单(login.aspx),并在 web.config 中放置了身份验证(使用 login.aspx 作为 loginUrl)和授权标签。之后,我将整个项目发布到远程服务器,我将与该用户共享“内容”文件夹。他将有权访问所有 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 项目中经常使用相同的原理,它在同一个远程服务器上工作得很好。我什至试图把它放在另一台服务器上,但我得到了同样的效果。

我的网络应用项目的结构如下:

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) 上有一个网站配置的解决方法,但我的服务器上不允许这样做。

【问题讨论】:

    标签: asp.net html forms-authentication


    【解决方案1】:

    我认为你最好检查一下服务器上该应用程序的 IIS 设置,你的本地应该有一些不同,你需要更改它。

    【讨论】:

      【解决方案2】:

      打开 IIS,在站点树形视图中单击有问题的项目。 选择“身份验证”菜单并检查是否启用或禁用 Formsauthentication。如果这不起作用,请尝试禁用 Windows 身份验证。

      【讨论】:

        【解决方案3】:

        如果有人和我有同样的问题,我终于解决了这个问题......

        我已经按照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" />
        

        【讨论】:

          猜你喜欢
          • 2015-05-10
          • 2019-08-18
          • 1970-01-01
          • 2013-12-07
          • 1970-01-01
          • 2013-09-15
          • 1970-01-01
          • 2013-10-07
          • 1970-01-01
          相关资源
          最近更新 更多