【问题标题】:How to protect and authenticate an entire web folder with Azure and IIS如何使用 Azure 和 IIS 保护和验证整个 Web 文件夹
【发布时间】:2020-05-26 13:58:40
【问题描述】:

我正在尝试找出在 Microsoft Azure 中是否有比我当前的解决方案更好的身份验证方法。

我确实有一个在 Windows Server 2019 和 IIS 中设置的 PHP 应用程序。我正在使用 SimpleSAMLphp 库,它工作正常 - 它可以在 Azure 中进行身份验证,没有任何问题。

但是...它在 file-by-file 基础上进行身份验证,这意味着我需要在我希望它进行身份验证的每个 PHP 文件中包含 Azure 身份验证。像这样的东西工作正常:

https://whatever.local/secure/myfile.php:

# ----------
# myfile.php
# ----------

# Include the SimpleSAMLphp library
require($_SERVER["DOCUMENT_ROOT"]."/../simpleSAMLphp/lib/_autoload.php");

# Make sure I am authenticated in Azure
$azure = new \SimpleSAML\Auth\Simple("my-identity");
$azure->requireAuth();

# Get Azure data
$azureUserAttr = $azure->getAttributes();

# Get the data in to useable variables
$azureId = $azureUserAttr["userid"][0] ?? "";
$azureEmail = $azureUserAttr["email"][0] ?? "";

# Print the variables
echo "ID=$azureId<br />";
echo "Email=$azureEmail<br />";

是否可以安装某种 Windows IIS 运行时来保护整个文件夹,所以每次我调用某些东西时,例如/secure/ 然后通过 Azure 进行身份验证?

例如 - 如何确保我必须在 Azure 中进行身份验证才能获取这张想象中的图片 https://whatever.local/secure/picture.jpg ?我可以保护整个/secure/ 文件夹吗?

理想情况下它可以做到这一点,然后我应该将 Azure 属性/声明作为 PHP 中的服务器变量提供?

【问题讨论】:

  • 由于 PHP 应用程序可以在 IIS 集成管道下执行。 HTTP 模块可用于根据带有 /secure/ 的 url 验证身份验证令牌。您可以使用 httpmodule 来检查特定的 cookie。如果没有这样的cookie,您可以将其重定向到身份验证页面。

标签: php windows azure iis


【解决方案1】:

很长一段时间过去了,但前几天我偶然发现了我的问题的确切解决方案 - Shibboleth Service Provider。这将完全符合我的要求 - 在文件夹级别保护我的 IIS Web 服务器并在需要时通过 Azure 进行身份验证 - 并且它将提供 Web 服务器端变量中的数据。

Shibboleth SP 是已安装的本机 Windows 应用程序,它提供 Windows 服务并作为模块集成到 IIS 中。请确保在安装中启用Configure IIS Module。它甚至很容易设置。当然这对每个人来说都是不同的,但就我而言,我只需要更改两个文件中的一些参数:

shibboleth2.xml

<InProcess>
    <ISAPI normalizeRequest="true" safeHeaderNames="true">
        <Site id="YOUR.IIS.SITE.ID" name="YOUR.WEB.FQDN"/>
    </ISAPI>
</InProcess>
<!-- You get the site ID from IIS Manager -->

<!-- Below I make sure the /secure folder is proper secured with Azure -->
<!-- More folders could be added if required -->
<RequestMapper type="Native">
    <RequestMap>
    <Host name="YOUR.WEB.FQDN">
        <Path name="secure" authType="shibboleth" requireSession="true"/>
    </Host>
    </RequestMap>
</RequestMapper>

<ApplicationDefaults entityID="YOUR.AZURE.ENTITYID"

<SSO entityID="https://sts.windows.net/YOUR.IDENTITY.PROVIDER/">
    SAML2
</SSO>

<!-- Your XML federation data - here I have it as a local file -->
<MetadataProvider type="XML" validate="true" path="metadata.xml"/>

attribute-map.xml

<Attribute name="YOUR.AZURE.ATTRIBUTE.NAME" id="YOUR.SERVER:VARIABLE.NAME"/>

您还必须在 Azure 中配置 回复 URL,默认为 https://YOUR.WEB.FQDN/Shibboleth.sso/SAML2/POST

配置后(以及每次配置更改后),您需要重新启动 IIS 和 Shibboleth 服务,Shibboleth Daemon (Default)

在 Shibboleth 配置之后,您可以检查 https://YOUR.WEB.FQDN/Shibboleth.sso/Status 并查看它是否提供 XML 输出(必须)。但是,您应该将shibboleth2.xml 中的 IP 地址更改为您执行 Web 请求的本地计算机:

<Handler type="Status" Location="/Status" acl="YOUR.COMPUTER.IP ::1"/>

注销 URL 将是 https://YOUR.WEB.FQDN/Shibboleth.sso/Logout

在注销时,您还可以通过添加 return 参数重定向到另一个 URL,而不是在 Azure 中配置的 URL - 例如。 https://YOUR.WEB.FQDN/Shibboleth.sso/Logout?return=/MyReturnDir/

一旦完成,您就会突然将所有 Azure 变量作为 Web 服务器端变量。在下面的示例中,我在 Azure 中定义了一些属性,我需要将其映射到 attribute-map.xml

<Attribute name="ADCompanyName" id="Company"/>
<Attribute name="ADUserFirstName" id="FirstName"/>
<Attribute name="ADUserLastName" id="LastName"/>

然后我可以从 PHP 获取这个服务器端变量,$_SERVER["Company"],其中包含登录的 Azure 用户的内容。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-22
    • 1970-01-01
    • 2011-02-23
    • 2020-09-21
    • 2019-11-06
    • 1970-01-01
    相关资源
    最近更新 更多