在Powershell中获取ADFS令牌

在Powershell中获取ADFS令牌,powershell,adfs2.0,office365,Powershell,Adfs2.0,Office365,我们有一个ADFS 2.0环境,用于将Active Directory域与Office 365联合 最近我们遇到了一个问题,集群停止响应,这反过来破坏了我们所有用户的电子邮件/日历访问。由于我们目前没有对ADFS进行任何监控,我正在尝试编写一个PowerShell脚本,该脚本将定期尝试对我们的ADFS群集进行身份验证,并获得一个有效令牌,类似于testexchangeconnectivity.com上的SSO测试 看起来令牌实际上是由 /adfs/services/trust/2005/user

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

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

看起来令牌实际上是由

/adfs/services/trust/2005/usernamemix

但每当我试图对这个URI运行invoke webrequest或new Webservice proxy并提供本地AD凭据时,我就会收到一个400错误的请求错误


为了从该端点正确请求令牌,我必须做些什么?

此脚本将帮助您完成任务 您将需要.NETFramework 4.5


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

基本上,您使用WSTrustChannelFactory,创建通道,并向其传递RequestSecurityToken

Leandro有一个很好的,简洁的


您需要安装Windows身份基础(WIF),如果您不使用.NET 4.5。< /P> < P>我使用WS-CUnand和WS-Type进行联合认证的产品。我相信你的案子是我们工作流程的一部分

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

该模块的源代码是开放的,尽管它在脚本中,但它严重依赖于
System.ServiceModel
System.IdentityModel
程序集中的.net framework类和程序集。我之所以提到这一点,是因为这些程序集中的大多数API都无法从.NET标准2中获得,因此不幸的是,该模块在非windows操作系统中无法工作。你也可以在我的帖子里读到更多

这是一个示例,您可以根据服务提供商的要求和依赖方的配置来发行对称和承载令牌。代码要求对联邦安全流、设置和术语有基本的了解

# 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