将身份验证添加到静态Azure网站

Gio*_*aki 8 authentication azure azure-web-sites

我们有一个Azure网站托管静态网站(只是一些HTML,CSS,Javascript),然后通过AJAX调用与我们的Azure移动服务进行通信.

我们想为这个站点添加一些非常简单的身份验证(只需一个静态用户名/密码就足够了).

请推荐最简单的方法.

或者可能有更好的选择来提供静态内容,而不是Azure网站?

Fer*_*eia 15

通过利用ASP.NET的身份验证和授权,可以实现使用静态用户名和密码的非常简单的身份验证形式,如本文所述,与IIS集成:使用IIS 7.0集成将ASP.NET身份验证和授权规则应用于静态内容管道功能.

这个样本的GitHub项目为例.相关的代码片段是您应该放在根目录(这将是公共的)的Web.config文件:

<?xml version="1.0"?>
<configuration>
    <system.web>
        <compilation debug="false" />
        <authentication mode="Forms">
            <forms>
                <credentials passwordFormat="Clear">
                    <user name="Alice" password="secret" />
                </credentials>
            </forms>
        </authentication>
        <!-- Unless specified in a sub-folder's Web.config file, 
             any user can access any resource in the site -->
        <authorization>
            <allow users="*" />
        </authorization>
    </system.web>
    <system.webServer>
        <modules>
            <remove name="FormsAuthenticationModule" />
            <add name="FormsAuthenticationModule" type="System.Web.Security.FormsAuthenticationModule" />            
            <remove name="UrlAuthorization" />
            <add name="UrlAuthorization" type="System.Web.Security.UrlAuthorizationModule" />
        </modules>
    </system.webServer>
</configuration>
Run Code Online (Sandbox Code Playgroud)

这个Web.config文件可以放在子目录中(受限制):

<?xml version="1.0"?>
<configuration>
    <system.web>
        <!-- Anonymous users are denied access to this folder (and its subfolders) -->
        <authorization>
            <deny users="?" />
        </authorization>
    </system.web>
</configuration>
Run Code Online (Sandbox Code Playgroud)

您还需要一个包含HTML表单和服务器端身份验证逻辑的Login.aspx文件.有关示例,请参阅示例项目中的Login.aspx.

这样,您就可以在根级别托管公共文件,在子目录托管私有文件.如果您想保护甚至根级别,只需相应地调整授权规则.

有关配置选项的文档,请参阅以下MSDN页面: