How do I get the SecurityToken from the ClaimsPrincipal?
How to get SecurityToken
from ClaimsPrincipal
?
I need this because I want to pass it from MVC application to WCF service in AuthenticationManager / Authenticate .
In the Authenticate method, BootstrapContext is null. And even after authenticating, it sometimes gets null, making it not a reliable choice for me.
This is my Authentication Manager class:
public override ClaimsPrincipal Authenticate (string resourceName, ClaimsPrincipal incomingPrincipal) {string passportID = incomingPrincipal.Identity.GetPassportID (). ToString ();
try
{
// I need the token here
SecurityToken token = GetToken(incomingPrincipal);
return base.Authenticate(resourceName, incomingPrincipal);
}
catch (Exception ex)
{
throw new SecurityException("User is not authenticated.", ex);
}
}
source to share
I ended up using this code:
BootstrapContext context = ClaimsPrincipal.Current.Identities.First().BootstrapContext as BootstrapContext;
SecurityToken token = context.SecurityToken;
if (context.SecurityToken != null)
{
token = context.SecurityToken;
}
else if (String.IsNullOrWhiteSpace(context.Token) == false)
{
var handlers = FederatedAuthentication.FederationConfiguration.IdentityConfiguration.SecurityTokenHandlers;
token = handlers.ReadToken(new XmlTextReader(new StringReader(context.Token)));
}
var actAsToken = GetActAsToken(token);
You can learn more about this in this . It looks like it context.SecurityToken
will be cleaned up for some time so it cannot be too dependent on it.
source to share