【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

相关文章
|
6月前
|
前端开发 关系型数据库 MySQL
PHP外链网盘系统网站源码
> 本文将详细介绍如何从零构建一个基于PHP和MySQL的文件管理系统,分解项目代码并剖析每个模块的功能。我们将以`index.php`、`config.php`和`api.php`这三个核心文件为例,详细展示如何设计文件列表、数据库配置和文件上传接口,从而实现一个完整的文件管理系统。该文章可以作为学术研究和代码实现的参考。
353 98
|
5月前
|
关系型数据库 MySQL PHP
免登录游客卡密发放系统PHP网站源码
这是一个简单易用的卡密验证系统,主要功能包括: 卡密管理和验证,多模板支持,响应式设计,验证码保护,防刷机制,简洁的用户界面, 支持自定义模板,移动端优化,安全性保护,易于部署和维护。
324 77
|
4月前
|
PHP
2025简约的打赏系统PHP网站源码
2025简约的打赏系统PHP网站源码
118 20
|
6月前
|
Linux PHP 数据安全/隐私保护
2024授权加密系统PHP网站源码
2024授权加密系统PHP网站源码
194 58
|
7月前
|
关系型数据库 MySQL PHP
PHP与MySQL的无缝集成:构建动态网站的艺术####
本文将深入探讨PHP与MySQL如何携手合作,为开发者提供一套强大的工具集,以构建高效、动态且用户友好的网站。不同于传统的摘要概述,本文将以一个生动的案例引入,逐步揭示两者结合的魅力所在,最终展示如何通过简单几步实现数据驱动的Web应用开发。 ####
|
7月前
|
SQL 前端开发 PHP
如何使用PHP开发一个购物网站?
在数字化时代,线上购物日益重要。本文介绍如何使用PHP开发一个功能完善、用户友好的购物网站,涵盖需求分析、开发环境选择、数据库设计、前后端开发、用户认证、商品展示、购物车、订单管理、功能扩展及安全性能优化等环节,旨在提供全面的开发指南。
148 3
|
7月前
|
关系型数据库 MySQL PHP
PHP与MySQL的深度整合:构建高效动态网站####
在当今这个数据驱动的时代,掌握如何高效地从数据库中检索和操作数据是至关重要的。本文将深入探讨PHP与MySQL的深度整合方法,揭示它们如何协同工作以优化数据处理流程,提升网站性能和用户体验。我们将通过实例分析、技巧分享和最佳实践指导,帮助你构建出既高效又可靠的动态网站。无论你是初学者还是有经验的开发者,都能从中获得宝贵的见解和实用的技能。 ####
69 0
|
SQL Web App开发 关系型数据库
[Azure] 创建支持Apache,PHP以及MySQL的CentOS Web Virtual Machine Server
创建Linux虚机 1. 打开 https://manage.windowsazure.com/ 并使用您的账户登录Windows Azure Portal。 2. 在下方菜单中选择New | Compute | Virtual Machine | From Gallery 开始创建一个新的虚机。
1225 0
|
2月前
|
关系型数据库 MySQL Linux
查看Linux、Apache、MySQL、PHP版本的技巧
以上就是查看Linux、Apache、MySQL、PHP版本信息的方法。希望这些信息能帮助你更好地理解和使用你的LAMP技术栈。
140 17
|
3月前
|
关系型数据库 MySQL PHP
源码编译安装LAMP(HTTP服务,MYSQL ,PHP,以及bbs论坛)
通过以上步骤,你可以成功地在一台Linux服务器上从源码编译并安装LAMP环境,并配置一个BBS论坛(Discuz!)。这些步骤涵盖了从安装依赖、下载源代码、配置编译到安装完成的所有细节。每个命令的解释确保了过程的透明度,使即使是非专业人士也能够理解整个流程。
86 18