I'm trying to get a Kerberos ticket using the following code:
public static byte[] GetToken(string username, string password, string domain)
{
using (var domainContext = new PrincipalContext(ContextType.Domain, domain))
{
string spn = UserPrincipal.FindByIdentity(domainContext, IdentityType.SamAccountName, username).UserPrincipalName;
var provider = new KerberosSecurityTokenProvider(spn, TokenImpersonationLevel.Impersonation, new NetworkCredential(username, password, domain));
var token = provider.GetToken(TimeSpan.FromMinutes(1)) as KerberosRequestorSecurityToken;
return token.GetRequest();
}
}
My problem is the provider.GetToken() method throws a SecurityTokenValidationException with the message "The NetworkCredentials provided were unable to create a Kerberos credential, see inner exception for details". The inner exception reads "Authenticating to a service running under a user account which requires Kerberos multilegs, is not supported". Any thoughts?
I got my code from this thread and it seems it worked in this guy's case: How to get Service Token from Kerberos using SSPI