asp.net Web应用程序内的HTML内容授权

时间:2012-02-21 16:58:17

标签: asp.net html forms-authentication

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

我是一名asp.net开发人员,我的第一个想法是将他的所有内容都包含在名为“Content”的web aplication项目文件夹中。然后我创建了简单的登录表单(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做到这一点?为什么这种表单身份验证在我发布时不起作用?作为Web应用程序的一部分,当整个内容发布时,是否可以阻止访问纯HTML内容?

我在我的asp.net项目中经常使用相同的原理,它在同一个远程服务器上运行良好。我甚至试图把它放在另一台服务器上,但我也有同样的效果。

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

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

此外,我试图在CONTENT文件夹中放入一些aspx内容,并且身份验证重定向工作正常。甚至可以使用表单身份验证来保护Web应用程序中的html内容吗?

远程服务器使用IIS 6并且ASP.NET运行时不处理html文件,并且因为表单身份验证不起作用。我已经将html文件扩展名重命名为aspx,一切正常。我现在对这个解决方案很满意,但是如果有人有更好的解决方案,请在这里写一下......

我已经读过在IIS 6上有一个关于网站配置的解决方法(http://forums.asp.net/t/1184547.aspx),但我的服务器上不允许这样做。

3 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:0)

打开IIS,在站点树视图中单击相关项目。 选择&#34;身份验证&#34;菜单并检查是否启用了Formsauthentication。如果这不起作用,请尝试禁用Windows身份验证。

答案 2 :(得分:0)

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

我无论如何都改变了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" />