I am trying to programmatically authenticate to my portal through ACS. When I do that manually by launching the portal, then entering my live id credentials, everything works fine. However, using below code it is failing to return a trusted token
Can anybody help please ??
functionInvoke-ADFSSecurityTokenRequest
{
param(
[Parameter(Mandatory=$true)]$ADFSBaseUri,
[Parameter(Mandatory=$true)]$AppliesTo,
[Parameter(Mandatory=$true)]$Username,
[Parameter(Mandatory=$true)]$Password,
[Parameter(Mandatory=$true)][ValidateSet("1","2")]$SAMLVersion=1,
[Parameter(Mandatory=$true)][ValidateSet("Token","RSTR")]$OutputType="Token",
[Parameter(Mandatory=$true)][Switch]$IgnoreCertificateErrors
)
$ADFSTrustPath ="v2/wstrust/13"
$SecurityMode ="TransportWithMessageCredential"
$ADFSTrustEndpoint ='username'
$Credential =New-Object System.Net.NetworkCredential-ArgumentList$Username,$Password
Add-Type-AssemblyName'System.ServiceModel, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'
Add-Type-AssemblyName'System.IdentityModel, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'
$Binding =New-Object-TypeNameSystem.ServiceModel.WS2007HttpBinding -ArgumentList ([System.ServiceModel.SecurityMode]$SecurityMode)
$Binding.Security.Message.EstablishSecurityContext=$false
$Binding.Security.Message.ClientCredentialType="UserName"
$Binding.Security.Transport.ClientCredentialType='None'
$EP=New-Object-TypeNameSystem.ServiceModel.EndpointAddress -ArgumentList ('{0}/{1}/{2}' -f$ADFSBaseUri,$ADFSTrustPath,$ADFSTrustEndpoint)
$WSTrustChannelFactory =New-Object-TypeNameSystem.ServiceModel.Security.WSTrustChannelFactory-ArgumentList$Binding,$EP
$WSTrustChannelFactory.TrustVersion=[System.ServiceModel.Security.TrustVersion]::WSTrust13
$WSTrustChannelFactory.Credentials.Windows.ClientCredential=$Credential
$WSTrustChannelFactory.Credentials.UserName.UserName=$Credential.UserName
$WSTrustChannelFactory.Credentials.UserName.Password=$Credential.Password
$Channel =$WSTrustChannelFactory.CreateChannel()
$TokenType = @{
SAML11 ='urn:oasis:names:tc:SAML:1.0:assertion'
SAML2 ='urn:oasis:names:tc:SAML:2.0:assertion'
}
$RST=New-Object-TypeNameSystem.IdentityModel.Protocols.WSTrust.RequestSecurityToken-Property @{
RequestType =[System.IdentityModel.Protocols.WSTrust.RequestTypes]::Issue
AppliesTo =$AppliesTo
KeyType =[System.IdentityModel.Protocols.WSTrust.KeyTypes]::Bearer
TokenType =if ($SAMLVersion-eq'2') {$TokenType.SAML2}else {$TokenType.SAML11}
}
$RSTR=New-Object-TypeNameSystem.IdentityModel.Protocols.WSTrust.RequestSecurityTokenResponse
try {
$OriginalCallback =[System.Net.ServicePointManager]::ServerCertificateValidationCallback
if ($IgnoreCertificateErrors.IsPresent) {[System.Net.ServicePointManager]::ServerCertificateValidationCallback= {return$true}}
$Token=$Channel.Issue($RST,[ref]$RSTR)
}
finally {
[System.Net.ServicePointManager]::ServerCertificateValidationCallback=$OriginalCallback
}
if ($OutputType-eq'RSTR') {
Write-Output-InputObject$RSTR
}
else {
Write-Output-InputObject$Token
}
}
Invoke-ADFSSecurityTokenRequest `
-ADFSBaseUri"https://mypartnerportal.accesscontrol.windows.net" `
-AppliesTo"http://localhost:#portnb#/" `
-UserName"myusername" `
-Password"mypassword" `
-OutputType"Token" `
-SAMLVersion"2" `
-IgnoreCertificateErrors