在Powershell中获取ADFS令牌

15
我们拥有一个ADFS 2.0环境,用于将我们的Active Directory域与Office 365联合。最近,我们遇到了一个问题,集群停止响应,这导致所有用户的电子邮件/日历访问中断。由于我们目前没有针对ADFS的监控,因此我正在尝试编写一个PowerShell脚本,定期尝试对我们的ADFS集群进行身份验证并获取有效令牌,类似于testexchangeconnectivity.com上的SSO测试工具。
看起来令牌实际上是由/adfs/services/trust/2005/usernamemixed发放的,但每当我尝试运行invoke-webrequest或new-Webservice代理以使用本地AD凭据对此URI进行调用时,都会收到400 Bad Request错误提示。
我需要做什么才能正确地从此端点请求令牌?
3个回答

3

2
我从事的产品使用WS-Federation和WS-Trust进行联合身份验证。我相信您的情况是我们工作流程的一部分。
多年来,我已经开发了针对基于SOAP的API的PowerShell自动化,并在某个时候将该知识整合到可在gallery上获得的WcfPS模块中。
该模块的代码是开源的,虽然它是脚本,但它严重依赖于.NET框架类和程序集中的System.ServiceModelSystem.IdentityModel程序集。我提到这一点是因为那些程序集内大部分的API在.NET标准2中不可用,所以该模块不幸地无法在非Windows操作系统上运行。您也可以在我的帖子WCFPS-PowerShell模块与SOAP端点一起工作中了解更多信息。
这是一个示例,您可以根据您的服务提供商要求和依赖方配置发出对称和承载令牌。该代码需要对联合安全流程、设置和术语有基本的理解。
# 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

0

基本上,您可以使用WSTrustChannelFactory创建一个通道,并将其传递给RequestSecurityToken。

Leandro有一个漂亮简洁的示例

如果您没有使用.NET 4.5,则需要安装Windows身份验证基础(WIF)。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接