【问题标题】:Get ADFS Token in Powershell在 Powershell 中获取 ADFS 令牌
【发布时间】:2012-11-11 05:11:21
【问题描述】:

我们有一个 ADFS 2.0 环境,用于将我们的 Active Directory 域与 Office 365 联合。

最近我们遇到了一个问题,即集群停止响应,这反过来又破坏了我们所有用户的电子邮件/日历访问权限。由于目前我们没有对 ADFS 进行任何监控,因此我正在尝试编写一个 PowerShell 脚本,该脚本将定期尝试对我们的 ADFS 集群进行身份验证,并获取类似于 testexchangeconnectivity.com 上的 SSO 测试的有效令牌。

看来令牌实际上是由

/adfs/services/trust/2005/usernamemixed

但每当我尝试针对此 URI 运行 invoke-webrequest 或 new-Webservice 代理并提供本地 AD 凭据时,我都会收到 400 Bad Request 错误。

我必须做什么才能正确地从该端点请求令牌?

【问题讨论】:

    标签: powershell adfs2.0 office365


    【解决方案1】:

    这个脚本应该能让你上路 http://gallery.technet.microsoft.com/scriptcenter/Invoke-ADFSSecurityTokenReq-09e9c90c 您将需要 .Net Framework 4.5

    您还可以使用 Connect-MSOL cmdlet 模拟 ADFS 登录到 Office 365 以连接到 powershell 会话 - 如果您使用 ADFS 帐户,则会发生 ADFS 登录。

    【讨论】:

      【解决方案2】:

      我正在开发一个使用 WS-Federation 和 WS-Trust 进行联合身份验证的产品。我相信您的案例是我们工作流程的一部分。

      多年来,我针对我们基于 SOAP 的 API 开发了 PowerShell 自动化,并在某个时候将这些知识整合到图库中的 WcfPS 模块中。

      模块的code 是开源的,尽管它在脚本中,但它严重依赖于来自System.ServiceModelSystem.IdentityModel 程序集的.n​​et 框架类和程序集。我提到这一点是因为这些程序集中的大多数 api 在 .NET 标准 2 中不可用,因此该模块很遗憾不能在非 Windows 操作系统中运行。您也可以在我的帖子WCFPS - PowerShell module to work with SOAP endpoints 中阅读更多相关信息。

      这是一个示例,您可以根据您的服务提供商要求和依赖方配置发布对称令牌和不记名令牌。该代码需要对联合安全流程、设置和术语有基本的了解。

      # Define the ADFS MEX uri 
      $adfsMexUri="https://adfs.example.com/adfs/services/trust/mex"
      
      #region Define authentication endpoints. One for windows and one with username/password
      $windowsMixed13AuthenticationEndpoint="https://adfs.example.com/adfs/services/trust/13/windowsmixed"
      $usernamePasswordMixed13AuthenticationEndpoint="https://adfs.example.com/adfs/services/trust/13/usernamemixed"
      #endregion
      
      #region Define service providers for which we want to issue a symmetric and a bearer token respectively
      # Symmatric is for SOAP, WS-Trust
      # Bearer is for Web, WS-Federation
      $soapServiceProviderAppliesTo="https://myserviceprovider/Soap/"
      $webServiceProviderAppliesTo="https://myserviceprovider/Web/"
      #endregion
      
      # Parse the MEX and locate the service endpoint
      $issuerImporter=New-WcfWsdlImporter -Endpoint $adfsMexUri
      
      #region Issue tokens with windows authentications
      $issuerEndpoint=$issuerImporter | New-WcfServiceEndpoint -Endpoint $windowsMixed13AuthenticationEndpoint
      $soapToken=New-SecurityToken -Endpoint $issuerEndpoint -AppliesTo $soapServiceProviderAppliesTo -Symmetric
      $webToken=New-SecurityToken -Endpoint $issuerEndpoint -AppliesTo $webServiceProviderAppliesTo -Bearer  
      #endregion
      
      #region Issue tokens with username/password credentials
      $credential=Get-Credential
      $issuerEndpoint=$issuerImporter | New-WcfServiceEndpoint -Endpoint $usernamePasswordMixed13AuthenticationEndpoint
      $soapToken=New-SecurityToken -Endpoint $issuerEndpoint -Credential $credential -AppliesTo $soapServiceProviderAppliesTo -Symmetric
      $webToken=New-SecurityToken -Endpoint $issuerEndpoint -Credential $credential -AppliesTo $webServiceProviderAppliesTo -Bearer    
      #endregion
      

      【讨论】:

        【解决方案3】:

        本质上,您使用 WSTrustChannelFactory,创建一个通道,并将 RequestSecurityToken 传递给它。

        Leandro 有个漂亮简洁的sample

        如果您不使用 .NET 4.5,则需要安装 Windows Identity Foundation (WIF)。

        【讨论】:

          猜你喜欢
          • 2013-10-01
          • 1970-01-01
          • 1970-01-01
          • 2023-03-17
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多