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

时间:2013-07-12 16:14:53

标签: authentication azure azure-web-sites

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

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

请推荐最简单的方法。

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

3 个答案:

答案 0 :(得分:15)

通过利用ASP.NET的身份验证和授权,可以实现使用静态用户名和密码的非常简单的身份验证形式,并与IIS集成,如本文所述:Apply ASP.NET Authentication and Authorization Rules to Static Content with IIS 7.0's Integrated Pipeline Feature

this sample GitHub project为例。相关的代码片段是这个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>

这个可以放在子目录中的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>

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

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

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

答案 1 :(得分:4)

这是一个以前已经回答的旧问题,但如果您正在寻找不需要代码或配置更改的最简单的解决方案,您可以使用Azure网站身份验证/授权(免责声明,我帮助构建了此功能)。以下是一些链接:

博客文章: http://azure.microsoft.com/blog/2014/11/13/azure-websites-authentication-authorization/

演示视频: http://azure.microsoft.com/en-us/documentation/videos/azure-websites-easy-authentication-and-authorization-with-chris-gillum/

编辑: 这是一篇更新的博客文章,描述除了默认的Azure AD之外如何使用社交提供商(Twitter,Facebook,Google,Microsoft帐户):https://azure.microsoft.com/en-us/blog/announcing-app-service-authentication-authorization/

答案 2 :(得分:0)

会利用Twitter或Microsoft帐户等社交提供商吗?设置可能较少(即无需管理自己的身份存储)。