runtime icon indicating copy to clipboard operation
runtime copied to clipboard

NegotiateStream AuthenticateAsClientAsync does not work for non default credential

Open hongdai opened this issue 10 years ago • 3 comments

NegotiateStream AuthenticateAsClientAsync works for default credential, but not non default credential.

I have a repro app similar to the sample in https://msdn.microsoft.com/en-us/library/system.net.security.negotiatestream(v=vs.110).aspx

The repro app is passing in desktop, but failing in .net core. I have observed that the client has got a NTLM credential instead of kerberos and failed to log on to the server.

Repro.zip

with below exception from client.

System.Net.Security.dll!System.Net.Security.NegoState.ThrowCredentialException(long error) Line 842 C# System.Net.Security.dll!System.Net.Security.NegoState.ProcessReceivedBlob(byte[] message, System.Net.LazyAsyncResult lazyResult) Line 608 C# System.Net.Security.dll!System.Net.Security.NegoState.ReadCallback(System.IAsyncResult transportResult) Line 758 C# System.Net.Security.dll!System.Net.LazyAsyncResult.Complete(System.IntPtr userToken) Line 481 C# System.Net.Security.dll!System.Net.LazyAsyncResult.ProtectedInvokeCallback(object result, System.IntPtr userToken) Line 431 C# System.Net.Security.dll!System.Net.StreamFramer.ReadFrameComplete(System.IAsyncResult transportResult) Line 292 C# System.Net.Security.dll!System.Net.StreamFramer.ReadFrameCallback(System.IAsyncResult transportResult) Line 171 C# System.Net.Security.dll!System.Threading.Tasks.TaskToApm.InvokeCallbackWhenTaskCompletes.AnonymousMethod__0() Line 137 C# System.Private.CoreLib.ni.dll!System.Runtime.CompilerServices.TaskAwaiter.OutputWaitEtwEvents.AnonymousMethod__0() Unknown System.Private.CoreLib.ni.dll!System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback callback, object state) Unknown System.Private.CoreLib.ni.dll!System.Threading.Tasks.AwaitTaskContinuation.RunCallback(System.Threading.ContextCallback callback, object state, ref System.Threading.Tasks.Task currentTask) Unknown System.Private.CoreLib.ni.dll!System.Threading.Tasks.Task.FinishContinuations() Unknown System.Private.CoreLib.ni.dll!System.Threading.Tasks.Task.TrySetResult(int result) Unknown System.Private.CoreLib.ni.dll!System.Threading.Tasks.TaskFactory.FromAsyncCoreLogic(System.IAsyncResult iar, System.Func<System.IAsyncResult, int> endFunction, System.Action<System.IAsyncResult> endAction, System.Threading.Tasks.Task promise, bool requiresSynchronization) Unknown System.Private.CoreLib.ni.dll!System.Threading.Tasks.TaskFactory.FromAsyncImpl.AnonymousMethod__1(System.IAsyncResult iar) Unknown System.Net.Sockets.dll!System.Net.LazyAsyncResult.Complete(System.IntPtr userToken) Line 481 C# System.Net.Sockets.dll!System.Net.ContextAwareResult.CompleteCallback() Line 461 C# System.Net.Sockets.dll!System.Net.ContextAwareResult.Complete.AnonymousMethod__15_0(object s) Line 450 C# System.Private.CoreLib.ni.dll!System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback callback, object state) Unknown System.Net.Sockets.dll!System.Net.ContextAwareResult.Complete(System.IntPtr userToken) Line 451 C# System.Net.Sockets.dll!System.Net.LazyAsyncResult.ProtectedInvokeCallback(object result, System.IntPtr userToken) Line 431 C# System.Net.Sockets.dll!System.Net.Sockets.BaseOverlappedAsyncResult.CompletionPortCallback(uint errorCode, uint numBytes, System.Threading.NativeOverlapped* nativeOverlapped) Line 181 C# System.Threading.Overlapped.dll!System.Threading.ThreadPoolBoundHandleOverlapped.CompletionCallback(uint errorCode, uint numBytes, System.Threading.NativeOverlapped* nativeOverlapped) Line 48 C#

hongdai avatar Jun 05 '16 03:06 hongdai

@hongdai I've tried to repro this with no extra settings and I'm not getting the following on .Net 4.5.2 Desktop:

  • Using DefaultNetworkCredentials the server reports REDMOND\*** was authenticated using NTLM.
  • Using my credentials the server reports that the target SPN is incorrect:
Client connected.
Ending authentication.
System.Security.Authentication.AuthenticationException: Authentication failed on the remote side (the stream might still be available for additional authentication attempts). ---> System.ComponentModel.Win32Exception: The target principal name is incorrect
   --- End of inner exception stack trace ---
   at System.Net.Security.NegoState.EndProcessAuthentication(IAsyncResult result)
   at System.Net.Security.NegotiateStream.EndAuthenticateAsServer(IAsyncResult asyncResult)
   at Server.AsynchronousAuthenticatingTcpListener.EndAuthenticateCallback(IAsyncResult ar) in s:\prj\9160Auth\ConsoleApplication1\Server\Program.cs:line 83
Authentication failed - closing connection.

I've tried with both default SPN I've found already set for my machine using: setspn -l <hostname>

CIPop avatar Jun 07 '16 00:06 CIPop

You need to run the service under machine context so that the target SPN is correct. For example, you can use LocalSystem. PsExec.exe probably the easiest way to do it: PsExec.exe -i -s.

Then you should see: Using DefaultNetworkCredentials the server reports REDMOND*** was authenticated using kerberos. Using my credentials the server failed to logon.

hongdai avatar Jun 07 '16 00:06 hongdai

Thanks @hongdai. I am now able to repro. I am able to authenticate using Kerberos from a Desktop client to either a Desktop or a CoreFX server. As you pointed out, the failure is from a CoreFX client to any implementation of the server.

CIPop avatar Jun 08 '16 00:06 CIPop