如何使用 ASP.NET 在纯 HTML 页面上进行表单身份验证?

发布于 2024-09-16 05:09:45 字数 674 浏览 8 评论 0原文

我在 IIS7 中使用表单身份验证来对开发站点进行密码保护,但是当站点仅包含静态 HTML 文件 + login.aspx + web.config 时,身份验证似乎会被绕过。

当我将文件重命名为 .aspx 时,系统会提示我输入登录表单 我没有做任何花哨的事情。我有一个非常简单的登录脚本,之后它应该重定向到index.html。

有什么建议吗?总而言之,整个网站(目前)使用 HTML,并且需要密码保护。

<authentication mode="Forms">
  <forms name="appNameAuth" path="/" loginUrl="~/login.aspx" defaultUrl="index.html" protection="All" timeout="525600">
    <credentials passwordFormat="Clear">
      <user name="[user]" password="[password]" />
    </credentials>
  </forms>
</authentication>
<authorization>
  <deny users="?" />
</authorization>

I am using forms authentication in IIS7 to password-protect a dev site, but the authentication seems to get by-passed when the site contains only static HTML files + login.aspx + web.config.

When I renamed the files to .aspx, I am prompted with the login form
I am not doing anything fancy. I have a very simple login script and it should just redirect to index.html afterward.

Any suggestions? To summarize, the entire site is using HTML (for now) and needs to be password protected.

<authentication mode="Forms">
  <forms name="appNameAuth" path="/" loginUrl="~/login.aspx" defaultUrl="index.html" protection="All" timeout="525600">
    <credentials passwordFormat="Clear">
      <user name="[user]" password="[password]" />
    </credentials>
  </forms>
</authentication>
<authorization>
  <deny users="?" />
</authorization>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

×纯※雪 2024-09-23 05:09:45

在 IIS7 中,如果您想在表单身份验证下保护 *.html 或 *.htm 文件(或其他非 .net 扩展名),请将以下行添加到您的 web.config 中:

<compilation>
    <buildProviders>
        <add extension=".html" type="System.Web.Compilation.PageBuildProvider" />
        <add extension=".htm" type="System.Web.Compilation.PageBuildProvider" />
    </buildProviders>
</compilation>

并且

<system.webServer>
     <handlers>
         <add name="HTML" path="*.html" verb="GET, HEAD, POST, DEBUG"   type="System.Web.UI.PageHandlerFactory" resourceType="Unspecified" requireAccess="Script" />
         <add name="HTM" path="*.htm" verb="GET, HEAD, POST, DEBUG" type="System.Web.UI.PageHandlerFactory" resourceType="Unspecified" requireAccess="Script" />
     </handlers>
</system.webServer>

In IIS7 if you want to protect *.html or *.htm files (or other non .net extensions) under forms authentication then add the following lines to your web.config:

<compilation>
    <buildProviders>
        <add extension=".html" type="System.Web.Compilation.PageBuildProvider" />
        <add extension=".htm" type="System.Web.Compilation.PageBuildProvider" />
    </buildProviders>
</compilation>

AND

<system.webServer>
     <handlers>
         <add name="HTML" path="*.html" verb="GET, HEAD, POST, DEBUG"   type="System.Web.UI.PageHandlerFactory" resourceType="Unspecified" requireAccess="Script" />
         <add name="HTM" path="*.htm" verb="GET, HEAD, POST, DEBUG" type="System.Web.UI.PageHandlerFactory" resourceType="Unspecified" requireAccess="Script" />
     </handlers>
</system.webServer>
蒲公英的约定 2024-09-23 05:09:45

要通过表单身份验证锁定 HTML 文件,您需要让 ASP.NET 为它们提供服务。您可以在 IIS 中通过将所需的扩展名(例如 .html、.htm 等)与 aspnet_isapi.dll 相关联来执行此操作。

一旦 ASP.NET 为这些文件提供服务,您就可以像任何 aspx 页面一样指定它们的权限。

有关详细信息,请参阅 MSDN

默认情况下,IIS 处理静态
内容本身 - 例如 HTML 页面和
CSS 和图像文件 - 并且只有双手
关闭对 ASP.NET 运行时的请求
当页面扩展名为
请求 .aspx、.asmx 或 .ashx。

但是,IIS 7 允许集成
IIS 和 ASP.NET 管道。与几个
您可以设置的配置设置
IIS 7 调用
适用于所有人的 FormsAuthenticationModule
请求。此外,借助 IIS 7,您
可以定义URL授权规则
任何类型的文件。了解更多
信息,请参阅 IIS6 之间的更改
和 IIS7 安全性,您的 Web 平台
安全性和理解 IIS7 URL
授权。

长话短说,在之前的版本中
IIS 7,只能使用表单
身份验证以保护资源
由 ASP.NET 运行时处理。
同样,URL授权规则是
仅适用于由
ASP.NET 运行时。但有了 IIS 7 就可以了
可以集成
FormsAuthenticationModule 和
UrlAuthorizationModule 进入 IIS 的 HTTP
管道,从而扩展了这个
满足所有请求的功能。

To make the HTML files locked down by your forms authetication, you need have them served by ASP.NET. You can do this in IIS by associating the extension(s) you need (eg. .html, .htm, etc) with the aspnet_isapi.dll.

Onces ASP.NET is servicing those files you can specify the permissions for them just like any aspx page.

For more information refer to MSDN:

By default, IIS processes static
content itself - like HTML pages and
CSS and image files - and only hands
off requests to the ASP.NET runtime
when a page with an extension of
.aspx, .asmx, or .ashx is requested.

IIS 7, however, allows for integrated
IIS and ASP.NET pipelines. With a few
configuration settings you can setup
IIS 7 to invoke the
FormsAuthenticationModule for all
requests. Furthermore, with IIS 7 you
can define URL authorization rules for
files of any type. For more
information, see Changes Between IIS6
and IIS7 Security, Your Web Platform
Security, and Understanding IIS7 URL
Authorization.

Long story short, in versions prior to
IIS 7, you can only use forms
authentication to protect resources
handled by the ASP.NET runtime.
Likewise, URL authorization rules are
only applied to resources handled by
the ASP.NET runtime. But with IIS 7 it
is possible to integrate the
FormsAuthenticationModule and
UrlAuthorizationModule into IIS's HTTP
pipeline, thereby extending this
functionality to all requests.

猫瑾少女 2024-09-23 05:09:45

虽然这是一个老问题,但我发现 pomarc 的答案中的链接非常有用。以下是适用于 IIS7 的摘要。

web.config 中,添加或修改 下的

<handlers>
  <add name="HTML" path="*.html" verb="GET,HEAD,POST,DEBUG" modules="IsapiModule" scriptProcessor="%windir%\Microsoft.NET\Framework\v2.0.50727\aspnet_isapi.dll" resourceType="Unspecified" requireAccess="Script" />
</handlers>

替换 verb 符合您所需的价值; scriptProcessor 值与您的环境的正确路径。

然后,添加或修改下的

<compilation debug="false" strict="false" explicit="true">
  <buildProviders>
    <!--Add below so .html file will be handled by ASP.NET (for use of Forms Authentication)-->
    <add extension=".html" type="System.Web.Compilation.PageBuildProvider" />
  </buildProviders>
</compilation>
<httpHandlers>
  <!--Add below so .html file will be handled by ASP.NET (for use of Forms Authentication)-->
  <add verb="GET, HEAD, POST, DEBUG" path="*.html" type="System.Web.UI.PageHandlerFactory" />
</httpHandlers>

替换verb 值与您需要的值。

您还可以包含更多用逗号“,”分隔的扩展名

Although this is an old question, I find the link in pomarc's answer really useful. Below is the summary which is suit for IIS7.

In your web.config, add or modify <handlers> under <system.webServer>:

<handlers>
  <add name="HTML" path="*.html" verb="GET,HEAD,POST,DEBUG" modules="IsapiModule" scriptProcessor="%windir%\Microsoft.NET\Framework\v2.0.50727\aspnet_isapi.dll" resourceType="Unspecified" requireAccess="Script" />
</handlers>

Replace verb value with your required one; scriptProcessor value with correct path of your environment.

Then, add or modify <compilation> and <httpHandlers> under <system.web>:

<compilation debug="false" strict="false" explicit="true">
  <buildProviders>
    <!--Add below so .html file will be handled by ASP.NET (for use of Forms Authentication)-->
    <add extension=".html" type="System.Web.Compilation.PageBuildProvider" />
  </buildProviders>
</compilation>
<httpHandlers>
  <!--Add below so .html file will be handled by ASP.NET (for use of Forms Authentication)-->
  <add verb="GET, HEAD, POST, DEBUG" path="*.html" type="System.Web.UI.PageHandlerFactory" />
</httpHandlers>

Replace verb value with your required one.

You may also include more extension separated by comma ','

锦爱 2024-09-23 05:09:45

几天前,我通过关注 fr33m3 @ 11-21-2007, 3:19 PM 在这个线程上的帖子解决了同样的问题:
http://forums.asp.net/t/1184547.aspx
遵循 2. 至 5. 的所有步骤即可完成!

希望这可以帮助你,就像它帮助我一样。

I've solved the same problem a few days ago, by following the post by fr33m3 @ 11-21-2007, 3:19 PM on this thread:
http://forums.asp.net/t/1184547.aspx
follow all the steps from 2. to 5. and you're done!

hope this can help you like it helped me.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文