Exemple #1
0
        public async Task<bool> ConnectAsGuestAsync(string domain, CancellationToken cancellationToken)
        {
            var tcpClient = new TcpClient();

#if DEBUG
            ITraceWriter traceWriter = new DebugTraceWriter("Client");
#else
            ITraceWriter traceWriter = new FileTraceWriter("client.log");
#endif

            var transport = new TcpTransport(
                new TcpClientAdapter(tcpClient),
                new EnvelopeSerializer(),
                hostName: _clientUri.Host,
                traceWriter: traceWriter
                );

            Channel = new ClientChannel(transport, TimeSpan.FromSeconds(60), autoReplyPings: true, autoNotifyReceipt: true);

            await Channel.Transport.OpenAsync(_clientUri, cancellationToken);

            var identity = new Identity()
            {
                Name = Guid.NewGuid().ToString(),
                Domain = domain
            };

            var resultSession = await Channel.EstablishSessionAsync(
                c => c.First(),
                e => SessionEncryption.TLS,
                identity,
                (s, r) => new GuestAuthentication(),
                Environment.MachineName,
                cancellationToken);

            if (resultSession.State != SessionState.Established)
            {
                return false;
            }

            return true;
        }
Exemple #2
0
        public async Task<bool> ConnectWithPasswordAsync(Identity identity, string password, CancellationToken cancellationToken)
        {
            var tcpClient = new TcpClient();

#if DEBUG
            ITraceWriter traceWriter = new DebugTraceWriter("Client"); 
#else
            ITraceWriter traceWriter = new FileTraceWriter("client.log");
#endif

            var transport = new TcpTransport(
                new TcpClientAdapter(tcpClient),
                new EnvelopeSerializer(),                
                hostName: _clientUri.Host,
                traceWriter: traceWriter
                );

            Channel = new ClientChannel(transport, TimeSpan.FromSeconds(60), autoReplyPings: true, autoNotifyReceipt: true);
            
            await Channel.Transport.OpenAsync(_clientUri, cancellationToken);

            var resultSession = await Channel.EstablishSessionAsync(
                c => c.First(),
                e => SessionEncryption.TLS,
                identity,
                (s, r) => { var auth = new PlainAuthentication(); auth.SetToBase64Password(password); return auth; },
                Environment.MachineName,
                cancellationToken);

            if (resultSession.State != SessionState.Established)            
            {
                return false;
            }

            return true;
        }
Exemple #3
0
 /// <summary>
 /// Tries to parse the string to a valid Identity;
 /// </summary>
 /// <param name="s">The s.</param>
 /// <param name="value">The value.</param>
 /// <returns></returns>
 public static bool TryParse(string s, out Identity value)
 {
     try
     {
         value = Parse(s);
         return true;
     }
     catch
     {
         value = null;
         return false;
     }
 }
Exemple #4
0
		private static async Task<Session> AuthenticateAsync(IClientChannel channel, Identity identity, string password)
		{
			var authentication = new PlainAuthentication ();
			authentication.SetToBase64Password (password);

			return await channel.EstablishSessionAsync (
				(compressionOptions) => compressionOptions[0],
				(encryptionOptions) => encryptionOptions[0],
				identity,
				(schemeOptions, roundtrip) => authentication,
				"default",
				CancellationToken.None);
		}
Exemple #5
0
		/// <summary>
		/// Authenticate the identity
		/// in the transport layer
		/// </summary>
		/// <param name="identity">The identity to be authenticated</param>
		/// <returns>
		/// Indicates if the identity is authenticated
		/// </returns>
		public Task<DomainRole> AuthenticateAsync(Identity identity)
		{
			var role = DomainRole.Unknown;
			
			if (identity == null)
			{
				throw new ArgumentNullException("identity");
			}

			var sslStream = _stream as SslStream;

			if (sslStream != null &&
				sslStream.IsAuthenticated &&
				sslStream.RemoteCertificate != null)
			{                
				var certificate = new X509Certificate2(sslStream.RemoteCertificate);
				var identityCertificateName = certificate.GetNameInfo(X509NameType.SimpleName, false);

				Identity certificateIdentity;

				if (identityCertificateName.Equals(identity.Domain, StringComparison.OrdinalIgnoreCase))
				{
					role = DomainRole.Authority;
				}
				else if (Identity.TryParse(identityCertificateName, out certificateIdentity) &&
						 certificateIdentity.Equals(identity))
				{
					role = DomainRole.Member;
				}               
			}

			return Task.FromResult(role);
		}
Exemple #6
0
 public IdentityDocument(Identity value) : base(MediaType)
 {
     Value = value;
 }
Exemple #7
0
        /// <summary>
        /// Convert the relative
        /// path to a Uri, using
        /// the identity as the
        /// URI authority.
        /// </summary>
        /// <param name="authority"></param>
        /// <returns></returns>
        public Uri ToUri(Identity authority)
        {
            if (_absoluteUri != null)
            {
                throw new InvalidOperationException("The URI path is absolute");
            }

            if (authority == null)
            {
                throw new ArgumentNullException("authority");
            }

            var baseUri = GetBaseUri(authority);
            return new Uri(baseUri, Path);
        }
Exemple #8
0
 public static Uri GetBaseUri(Identity authority)
 {
     return new Uri(string.Format("{0}://{1}/", LIME_URI_SCHEME, authority));
 }