简体   繁体   English

如何允许IIS 7.5上的匿名访问网站?

[英]How to allow anonymous access to web-site on IIS 7.5?

tl;dr : Why does tl;博士 :为什么

<allow users="?">

work on IIS Express, but not on IIS? 在IIS Express上工作,但在IIS上却没有?

Background 背景

I have a new asp.net web-forms project. 我有一个新的asp.net web-forms项目。 When running locally on Windows 7 IIS Express, i can block "all users" from accessing the site by adding a deny * rule to web.config: 在Windows 7 IIS Express上本地运行时,我可以通过向web.config添加deny *规则来阻止“所有用户”访问该站点:

web.config web.config中

<configuration>
   <system.web>
      <authorization>
         <deny users="*" />
      </authorization>
   </system.web>
</configuration>

this causes me to be denied access: 这导致我被拒绝访问:

在此输入图像描述

And so that makes sense. 这是有道理的。

I can deny access to anonymous users 我可以拒绝匿名用户访问

I the web.config, i can block access to anonymous users, by using the ? 我的web.config,我可以阻止访问匿名用户,使用? marker, rather than the all ( * ) marker: 标记,而不是所有* )标记:

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

And because i am not authenticated, i will again be 401 Unauthorized: 因为我没有通过身份验证,我将再次成为401 Unauthorized:

在此输入图像描述

And that makes sense. 这是有道理的。

Allow anonymous 允许匿名

I can allow anonymous access, by changing the deny in web.config to allow : 我可以允许匿名访问,通过更改web.config中的denyallow

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

And now I am brought right to the homepage: 现在我被带到了主页:

在此输入图像描述

and that makes sense. 这是有道理的。

But doesn't work on IIS 但是在IIS上不起作用

The above works on IIS Express. 以上适用于IIS Express。 But when i publish to Windows Server 2012 R2 IIS 7.5, trying to allow anonymous ( ? ) users does not work: 但是,当我发布到Windows Server 2012 R2 IIS 7.5时,尝试allow匿名( ? )用户不起作用:

在此输入图像描述

That makes no sense: 这是没有意义的:

  • works on IIS Express 适用于IIS Express
  • fails on IIS 7.5 在IIS 7.5上失败

Try allowing everyone 尝试允许每个人

Rather than: 而不是:

  • allowing just anonymous users ( ? ) 只允许匿名用户( ?
  • i can try to allow all users ( * ) 我可以尝试允许所有用户( *

i change web.config again to allow everyone ( * ): 我再次更改web.config以允许每个人( * ):

<authorization>
  <allow users="*" />
</authorization>

And locally i can still access the site: 在本地我仍然可以访问该网站:

在此输入图像描述

but once i publish to IIS 7.5 it still fails: 但是一旦我发布到IIS 7.5,它仍然会失败:

在此输入图像描述

What's going on? 这是怎么回事?

I'm not doing anything wrong. 我没有做错任何事。 So what do i need to change? 那么我需要改变什么呢?

Initially i created an empty web-site, and started adding things to it. 最初我创建了一个空的网站,并开始添加内容。 Later, i need to create real web-site (with pages that displayed information, and buttons to click), so i started over with an Empty Web Forms web-site . 后来,我需要创建真正的网站(显示信息的页面和点击按钮),所以我重新开始使用一个空的Web窗体网站

My feeling is that Owin broke everything. 我的感觉是Owin打破了一切。

Nevertheless, what is going on? 然而,发生了什么?

Solution

I found it. 我找到了。 There are some settings about a web-site that do not go with the web-site. 有一些关于网站的设置不适用于网站。 That is, there are configuration options about a web-site that you cannot configure through web.config , or any other file in the web-site's folder. 也就是说,有一些关于网站的配置选项,您无法通过web.config或网站文件夹中的任何其他文件进行配置。 In particular: 特别是:

在此输入图像描述

I don't know where IIS stores the use of anonymous authentication. 我不知道IIS存储使用匿名身份验证的位置。 But without anonymous authentication, IIS is unable to realize that an anonymous user is anonymous. 但是,如果没有匿名身份验证,IIS无法识别匿名用户是匿名用户。

Enabling anonymous authentication: 启用匿名身份验证:

在此输入图像描述

causes IIS to realize that anonymous users are anonymous. 导致IIS意识到匿名用户是匿名的。

That explains: 这解释了:

  • why it worked on IIS Expres 为什么它适用于IIS Expres
  • why it didn't work on IIS 7.5 为什么它在IIS 7.5上不起作用
  • why it still didn't work when both web-sites have the exact same set of configuration files 当两个网站都具有完全相同的配置文件集时,为什么它仍然不起作用

It doesn't explain why IIS doesn't treat anonymous users as anonymous when anonymous authentication is not enabled; 它没有解释为什么在未启用匿名身份验证时,IIS不会将匿名用户视为匿名用户; but that's another issue for another day. 但那是另一天的另一个问题。 If you've read down to here, you can copy-paste everything i just said, and get the accept. 如果你已经读到这里,你可以复制粘贴我刚刚说的所有内容,并获得接受。 Otherwise i'll have to wait two days to answer it myself. 否则我将不得不等待两天才能自己回答。 Better you get the rep. 你最好得到代表。

You learn something by finding the solution. 你通过寻找解决方案来学习一些东西 Congratulation. 祝贺。

Authorization happens after authentication. 授权发生授权。 So on IIS you saw the 401.2 error page before the authorization rule was ever processed. 因此,在IIS上,您在处理授权规则之前看到了401.2错误页面。 Only after a proper authentication method is set to enabled, then things start to work out. 只有在将正确的身份验证方法设置为启用后,才会开始解决问题。

IIS Express should give you the same 401.2 error page if you disable all its authentication methods. 如果禁用所有身份验证方法,IIS Express应该为您提供相同的401.2错误页面。 Just a note. 只是一张纸条。


A Microsoft Patterns and Practices article explains more about why you need anonymous authentication enabled in order to allow anonymous users: Microsoft模式和实践文章详细介绍了为什么您需要启用匿名身份验证才能允许匿名用户:

ASP.NET authentication is a two-step process. ASP.NET身份验证分为两步。 First, 第一,

  • Internet Information Services (IIS) authenticates the user and creates a Windows token to represent the user. Internet信息服务(IIS)对用户进行身份验证,并创建Windows令牌以表示用户。
  • If IIS is configured to use anonymous authentication, a token for the IUSR_MACHINE account is generated and used to represent the anonymous user. 如果IIS配置为使用匿名身份验证,则会生成IUSR_MACHINE帐户的令牌并用于表示匿名用户。

IIS-then passes the token to ASP.NET. 然后,IIS将令牌传递给ASP.NET。

Note Because forms authentication does not rely on IIS authentication, you should configure anonymous access for your application in IIS if you intend to use forms authentication in your ASP.NET application 注意由于表单身份验证不依赖于IIS身份验证,因此如果要在ASP.NET应用程序中使用表单身份验证,则应在IIS中为应用程序配置匿名访问

In IIS, anonymous access is enabled for all applications that use forms authentication. 在IIS中,为使用表单身份验证的所有应用程序启用匿名访问。

IIS allows the request because anonymous access is enabled in the IIS metabase. IIS允许该请求,因为IIS元数据库中启用了匿名访问。 ASP.NET confirms that the authorization element includes a tag. ASP.NET确认授权元素包含标记。

There are two ways for a user to be authenticated when issuing a request to IIS: 在向IIS发出请求时,有两种方法可以对用户进行身份验证:

  • IIS authenticates your identity itself (using Basic, Digest, or Windows authentication) IIS对您的身份进行身份验证(使用Basic,Digest或Windows身份验证)
  • IIS is configured to allow "anonymous" authentication, and the web-site will handle authentication itself IIS配置为允许“匿名”身份验证,并且Web站点将自行处理身份验证

The confusing part here is that there is a difference between: 这里令人困惑的部分是:

  • anonymous as far as IIS is concerned 就IIS而言是匿名的
  • anonymous as far is ASP.net Forms authentication is concerned 匿名到目前为止是ASP.net Forms身份验证

From IIS's point of view any request that will be authenticated using Forms (or Owin, or any other custom authentication module) is still an anonymous request: 从IIS的角度来看,任何使用Forms (或Owin或任何其他自定义身份验证模块)进行身份验证的请求仍然是匿名请求:

| IIS Authentication | Application Authentication |
|--------------------|----------------------------|
| Basic              |                            |
| Digest             |                            |
| Windows            |                            |
| Anonymous          | Forms                      |
| Anonymous          | Owin                       |
| Anonymous          | BasicAuthModule            |

When i was attempting to allow anonymous users access: 当我试图允许匿名用户访问时:

<allow users="?" />

That is a Forms authentication directive. 这是一个Forms身份验证指令。 But in order to even reach forms authentication, you must allow anonymous authentication at the IIS level. 但是,为了甚至达到表单身份验证,您必须允许IIS级别的匿名身份验证。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM