private static async Task RespondToOneMsg(TcpListener listener) { var socket = await listener.AcceptSocketAsync(); var client = await listener.AcceptTcpClientAsync(); Console.WriteLine("Connected"); using (var stream = new NegotiateStream(client.GetStream())) { await stream.AuthenticateAsServerAsync(CredentialCache.DefaultNetworkCredentials, ProtectionLevel.EncryptAndSign, TokenImpersonationLevel.Identification); Console.WriteLine($"remote {stream.RemoteIdentity.AuthenticationType}"); Console.WriteLine($"remote name = {stream.RemoteIdentity.Name}"); var recvBuffer = new byte[1024]; var byteCount = stream.Read(recvBuffer, 0, recvBuffer.Length); Console.WriteLine(Encoding.UTF8.GetString(recvBuffer, 0, byteCount)); var sendBuffer = Encoding.UTF8.GetBytes("Reply from server"); stream.Write(sendBuffer, 0, sendBuffer.Length); } }
protected override Stream OnAcceptUpgrade(Stream stream, out SecurityMessageProperty remoteSecurity) { #if SUPPORTS_WINDOWSIDENTITY // NegotiateStream // wrap stream NegotiateStream negotiateStream = new NegotiateStream(stream); // authenticate try { if (WcfEventSource.Instance.WindowsStreamSecurityOnAcceptUpgradeIsEnabled()) { WcfEventSource.Instance.WindowsStreamSecurityOnAcceptUpgrade(EventTraceActivity); } negotiateStream.AuthenticateAsServerAsync(_parent.ServerCredential, _parent.ProtectionLevel, TokenImpersonationLevel.Identification).GetAwaiter().GetResult(); } catch (AuthenticationException exception) { throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new SecurityNegotiationException(exception.Message, exception)); } catch (IOException ioException) { throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new SecurityNegotiationException( SR.Format(SR.NegotiationFailedIO, ioException.Message), ioException)); } remoteSecurity = CreateClientSecurity(negotiateStream, _parent.ExtractGroupsForWindowsAccounts); return negotiateStream; #else throw ExceptionHelper.PlatformNotSupported(ExceptionHelper.WinsdowsStreamSecurityNotSupported); #endif // SUPPORTS_WINDOWSIDENTITY }
protected override Stream OnAcceptUpgrade(Stream stream, out SecurityMessageProperty remoteSecurity) { #if FEATURE_NETNATIVE // NegotiateStream throw ExceptionHelper.PlatformNotSupported("NegotiateStream is not supported on UWP yet"); #else // wrap stream NegotiateStream negotiateStream = new NegotiateStream(stream); // authenticate try { if (TD.WindowsStreamSecurityOnAcceptUpgradeIsEnabled()) { TD.WindowsStreamSecurityOnAcceptUpgrade(EventTraceActivity); } negotiateStream.AuthenticateAsServerAsync(_parent.ServerCredential, _parent.ProtectionLevel, TokenImpersonationLevel.Identification).GetAwaiter().GetResult(); } catch (AuthenticationException exception) { throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new SecurityNegotiationException(exception.Message, exception)); } catch (IOException ioException) { throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new SecurityNegotiationException( SR.Format(SR.NegotiationFailedIO, ioException.Message), ioException)); } remoteSecurity = CreateClientSecurity(negotiateStream, _parent.ExtractGroupsForWindowsAccounts); return negotiateStream; #endif // FEATURE_NETNATIVE }