【Azure Developer】PHP网站使用AAD授权登录的参考示例

简介: 【Azure Developer】PHP网站使用AAD授权登录的参考示例

问题描述

如果有个PHP网站,需要使用AAD授权登录,有没有PHP代码实例 可供参考呢?

 

参考代码

参考一篇博文(Single sign-on with Azure AD in PHP),学习使用SSO的大体思路。如果对PHP很了解,可以参考Github中的Sample代码。

 

 

 

phpSample/federation.ini

federation.trustedissuers.issuer=https://accounts.accesscontrol.windows.net/v2/wsfederation
federation.trustedissuers.thumbprint=3f5dfcdf4b3d0eab9ba49befb3cfd760da9cccf1
federation.trustedissuers.friendlyname=Awesome Computers
federation.audienceuris=spn:d184f6dd-d5d6-44c8-9cfa-e2d630dea392
federation.realm=spn:d184f6dd-d5d6-44c8-9cfa-e2d630dea392@495c4a5e-38b7-49b9-a90f-4c0050b2d7f7
federation.reply=https://localhost/phpSample/index.php

phpSample/index.php

/*-----------------------------------------------------------------------
    Copyright (c) Microsoft Corporation.  All rights reserved.
 
    Copyright 2012 Microsoft Corporation
    All rights reserved.
    Licensed under the Apache License, Version 2.0 (the "License");
    you may not use this file except in compliance with the License.
    You may obtain a copy of the License at
      http://www.apache.org/licenses/LICENSE-2.0
 THIS CODE IS PROVIDED *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, 
 EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED WARRANTIES OR 
 CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE, MERCHANTABLITY OR NON-INFRINGEMENT.
 See the Apache Version 2.0 License for specific language governing 
 permissions and limitations under the License.
--------------------------------------------------------------------------- */
<?php
require_once (dirname(__FILE__) . '/secureResource.php');
?>
<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
    <title>Index Page</title>
</head>
<body>
    <h2>Index Page</h2>
    <h3>Welcome <strong><?php print_r($loginManager->getPrincipal()->getName()); ?></strong>!</h3>
    
    <h4>Claim list:</h4>
    <ul>
<?php 
    foreach ($loginManager->getClaims() as $claim) {
        print_r('<li>' . $claim->toString() . '</li>');
    }
?>
    </ul>
</body>
</html>

 

phpSample/login.php

/*-----------------------------------------------------------------------
    Copyright (c) Microsoft Corporation.  All rights reserved.
 
    Copyright 2012 Microsoft Corporation
    All rights reserved.
    Licensed under the Apache License, Version 2.0 (the "License");
    you may not use this file except in compliance with the License.
    You may obtain a copy of the License at
      http://www.apache.org/licenses/LICENSE-2.0
 THIS CODE IS PROVIDED *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, 
 EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED WARRANTIES OR 
 CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE, MERCHANTABLITY OR NON-INFRINGEMENT.
 See the Apache Version 2.0 License for specific language governing 
 permissions and limitations under the License.
--------------------------------------------------------------------------- */
<?php
// uncomment this to display internal server errors.
//error_reporting(E_ALL);
//ini_set('display_errors', 'On');
ini_set('include_path', ini_get('include_path').';../../libraries/;');
require_once ('waad-federation/TrustedIssuersRepository.php');
?>
<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
    <title>Login Page</title>
</head>
<body>
    <h2>Login Page</h2>    
    <ul>
<?php 
    $repository = new TrustedIssuersRepository();
    $trustedIssuers = $repository->getTrustedIdentityProviderUrls();
    foreach ($trustedIssuers as $trustedIssuer) {
        $returnUrl = $_GET['returnUrl'];
        print_r('<li><a href="' . $trustedIssuer->getLoginUrl($returnUrl) . '">' . $trustedIssuer->displayName . '</a></li>');
    }
?>
    </ul>
</body>
</html>

 

phpSample/secureResource.php

/*-----------------------------------------------------------------------
    Copyright (c) Microsoft Corporation.  All rights reserved.
 
    Copyright 2012 Microsoft Corporation
    All rights reserved.
    Licensed under the Apache License, Version 2.0 (the "License");
    you may not use this file except in compliance with the License.
    You may obtain a copy of the License at
      http://www.apache.org/licenses/LICENSE-2.0
 THIS CODE IS PROVIDED *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, 
 EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED WARRANTIES OR 
 CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE, MERCHANTABLITY OR NON-INFRINGEMENT.
 See the Apache Version 2.0 License for specific language governing 
 permissions and limitations under the License.
--------------------------------------------------------------------------- */
<?php
// uncomment this to display internal server errors.
// error_reporting(E_ALL);
// ini_set('display_errors', 'On');
ini_set('include_path', ini_get('include_path').';../../libraries/;');
require_once ('waad-federation/ConfigurableFederatedLoginManager.php');
session_start();
$token = $_POST['wresult'];
$loginManager = new ConfigurableFederatedLoginManager();
if (!$loginManager->isAuthenticated()) {
    if (isset ($token)) {
        try {
            $loginManager->authenticate($token);            
        } catch (Exception $e) {
            print_r($e->getMessage());
        }
    } else {
        $returnUrl = "https://" . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'];
        header('Pragma: no-cache');
        header('Cache-Control: no-cache, must-revalidate');
        header("Location: https://" . $_SERVER['HTTP_HOST'] . dirname($_SERVER['SCRIPT_NAME']) . "/login.php?returnUrl=" . $returnUrl, true, 302);
        exit();
    }
}
?>

 

phpSample/trustedIssuers.xml

<?xml version="1.0" encoding="UTF-8"?>
<issuers>
    <issuer name="awesomecomputers.onmicrosoft.com" displayName="Awesome Computers"
        realm="spn:d184f6dd-d5d6-44c8-9cfa-e2d630dea392@495c4a5e-38b7-49b9-a90f-4c0050b2d7f7" />
    <issuer name="treyresearchinc.onmicrosoft.com" displayName="Trey Research Inc."
        realm="spn:d184f6dd-d5d6-44c8-9cfa-e2d630dea392@13292593-4861-4847-8441-6da6751cfb86" />
</issuers>

 

 

参考资料

Single sign-on with Azure AD in PHP : http://www.lewisroberts.com/2015/09/04/single-sign-on-with-azure-ad-in-php/

Azure/azure-sdk-for-php-samples : https://github.com/Azure/azure-sdk-for-php-samples

相关文章
|
20天前
|
编译器 Linux PHP
【Azure App Service】为部署在App Service上的PHP应用开启JIT编译器
【Azure App Service】为部署在App Service上的PHP应用开启JIT编译器
|
20天前
|
缓存 NoSQL 网络安全
【Azure Redis 缓存】Azure Redis服务开启了SSL(6380端口), PHP如何访问缓存呢?
【Azure Redis 缓存】Azure Redis服务开启了SSL(6380端口), PHP如何访问缓存呢?
|
12天前
|
PHP
PHP全自动采集在线高清壁纸网站源码
PHP全自动采集在线高清壁纸网站源码,PHP全自动采集在线高清壁纸网站源码,一款开源壁纸源码,无需安装。集合360壁纸,百度壁纸,必应壁纸,简单方便。每天自动采集,自动更新,非常不错,php源码 网站源码 免费源码 自动采集。
28 3
|
12天前
|
PHP 数据库
2024表白墙PHP网站源码
2024表白墙PHP网站源码
26 1
|
20天前
|
PHP Windows
【Azure App Service for Windows】 PHP应用出现500 : The page cannot be displayed because an internal server error has occurred. 错误
【Azure App Service for Windows】 PHP应用出现500 : The page cannot be displayed because an internal server error has occurred. 错误
|
20天前
|
PHP 开发工具 git
【Azure 应用服务】在 App Service for Windows 中自定义 PHP 版本的方法
【Azure 应用服务】在 App Service for Windows 中自定义 PHP 版本的方法
|
16天前
|
前端开发 关系型数据库 MySQL
最新黑名单查询录入系统PHP网站源码
最新黑名单查询录入系统PHP网站源码 前端html 后端layui 操作部分都采用API接口的方式实线 集结了layui表格的多数据操作,添加,批量删除,分页,单项删除 后台数据修改采用绑定参数的形式来进行修改可以很好的预防数据库注入,当然如果你想要测试这个防注入的你也可以尝试一下 PHP版本70+ 数据库Mysql 5.6 上传程序访问 http://你的域名/install 安装
38 0
|
16天前
|
前端开发 安全 JavaScript
PHP与现代Web开发:探索PHP在构建动态网站中的角色和优势
【8月更文挑战第29天】 在数字时代的浪潮下,PHP以其独特的灵活性、易用性以及强大的社区支持,持续成为Web开发领域的重要力量。本文将深入探讨PHP如何适应现代Web开发的需求,通过具体示例揭示PHP的实际应用,并分析其在面对新兴技术挑战时的应对策略。我们将一探究竟,PHP如何在众多编程语言中脱颖而出,成为许多开发者和企业的首选。
|
20天前
|
Linux 应用服务中间件 网络安全
【Azure 应用服务】查看App Service for Linux上部署PHP 7.4 和 8.0时,所使用的WEB服务器是什么?
【Azure 应用服务】查看App Service for Linux上部署PHP 7.4 和 8.0时,所使用的WEB服务器是什么?
|
20天前
|
应用服务中间件 Linux 网络安全
【Azure 应用服务】PHP应用部署在App Service for Linux环境中,上传文件大于1MB时,遇见了413 Request Entity Too Large 错误的解决方法
【Azure 应用服务】PHP应用部署在App Service for Linux环境中,上传文件大于1MB时,遇见了413 Request Entity Too Large 错误的解决方法