Retrieve ADFS token with Net7
- Dominant language
- C#
- Stars
- 1.8k
- Forks
- 576
- Avg merge
- 6d 9h
- Merged PRs (30d)
- 2
Description
Dear,
I tried to migrate my old Net48 code to Net7 but no success.
This peace of code (Net48) is retrieving a JWT the token from an ADFS server.
```
string returnValue = null;
string relyingPartyId = "blabla";
string adfsEndpoint = "blabla";
string spnIdentity = "blabla";
GenericXmlSecurityToken genericToken;
var binding = new WS2007HttpBinding(SecurityMode.Transport);
binding.Security.Transport.ClientCredentialType = HttpClientCredentialType.Windows;
binding.Security.Message.NegotiateServiceCredential = true;
binding.Security.Message.EstablishSecurityContext = true;
binding.Security.Message.ClientCredentialType = MessageCredentialType.Windows;
var stsEp = new EndpointAddress(new Uri(adfsEndpoint), EndpointIdentity.CreateSpnIdentity(spnIdentity));
using (var factory = new WSTrustChannelFactory(binding, stsEp) { TrustVersion = TrustVersion.WSTrustFeb2005 })
{
factory.Credentials.SupportInteractive = false;
var rst = new RequestSecurityToken
{
RequestType = RequestTypes.Issue,
KeyType = KeyTypes.Bearer,
AppliesTo = new EndpointReference(relyingPartyId),
TokenType = "urn:ietf:params:oauth:token-type:jwt"
};
IWSTrustChannelContract channel = factory.CreateChannel();
genericToken = channel.Issue(rst) as GenericXmlSecurityToken;
}
if (genericToken != null)
{
returnValue = genericToken.TokenXml.InnerXml;
}
return returnValue;
```
The migrated version looks as the following :
```
string relyingPartyId = "blabla";
string adfsEndpoint = "blabla";
string spnIdentity = "blabla";
GenericXmlSecurityToken genericToken;
var binding = new WS2007HttpBinding(SecurityMode.Transport);
binding.Security.Transport.ClientCredentialType = HttpClientCredentialType.Windows;
binding.Security.Message.NegotiateServiceCredential = true;
binding.Security.Message.EstablishSecurityContext = true;
binding.Security.Message.ClientCredentialType = MessageCredentialType.Windows;
var stsEp = new EndpointAddress(new Uri(adfsEndpoint), EndpointIdentity.CreateIdentity(Claim.CreateSpnClaim(spnIdentity)));
using (var factory = new WSTrustChannelFactory(binding, stsEp))
{
var wtr = new WsTrustRequest("http://schemas.microsoft.com/idfx/requesttype/issue")
{
KeyType = "http://schemas.microsoft.com/idfx/keytype/bearer",
AppliesTo = new AppliesTo(new EndpointReference(relyingPartyId)),
TokenType = "urn:ietf:params:oauth:token-type:jwt",
WsTrustVersion = WsTrustVersion.TrustFeb2005
};
IWSTrustChannelContract channel = factory.CreateChannel();
genericToken = await channel.IssueAsync(wtr) as GenericXmlSecurityToken;
}
if (genericToken != null)
{
returnValue = genericToken.Id;
}
return returnValue;
```
This crashes on the CreateChannel with :
System.ArgumentNullException: 'Value cannot be null. (Parameter 'via')'
If I switch to CreateTrustChannel I get the following on the IssueAsync :
System.NotSupportedException: 'Exception of type 'System.NotSupportedException' was thrown.'
Could you give me the winning combination to make it work ?
Thanks in advance
Contributor guide
Assessment
This issue has not been assessed yet.