Exemplo n.º 1
0
        private static TokenProvider CreateTokenProvider(IEnumerable <Uri> stsEndpoints, string issuerName, string issuerKey, string sharedAccessKeyName, string sharedAccessKey, string windowsDomain, string windowsUser, SecureString windowsPassword, string oauthDomain, string oauthUser, SecureString oauthPassword)
        {
            if (!string.IsNullOrWhiteSpace(sharedAccessKey))
            {
                return(TokenProvider.CreateSharedAccessSignatureTokenProvider(sharedAccessKeyName, sharedAccessKey));
            }
            if (!string.IsNullOrWhiteSpace(issuerName))
            {
                if (stsEndpoints == null || !stsEndpoints.Any <Uri>())
                {
                    return(TokenProvider.CreateSharedSecretTokenProvider(issuerName, issuerKey));
                }
                return(TokenProvider.CreateSharedSecretTokenProvider(issuerName, issuerKey, stsEndpoints.First <Uri>()));
            }
            bool flag  = (stsEndpoints == null ? false : stsEndpoints.Any <Uri>());
            bool flag1 = (string.IsNullOrWhiteSpace(windowsUser) || windowsPassword == null ? false : windowsPassword.Length > 0);
            bool flag2 = (string.IsNullOrWhiteSpace(oauthUser) || oauthPassword == null ? false : oauthPassword.Length > 0);

            if (!flag)
            {
                return(null);
            }
            if (flag2)
            {
                return(TokenProvider.CreateOAuthTokenProvider(stsEndpoints, (string.IsNullOrWhiteSpace(oauthDomain) ? new NetworkCredential(oauthUser, oauthPassword) : new NetworkCredential(oauthUser, oauthPassword, oauthDomain))));
            }
            if (!flag1)
            {
                return(TokenProvider.CreateWindowsTokenProvider(stsEndpoints));
            }
            return(TokenProvider.CreateWindowsTokenProvider(stsEndpoints, (string.IsNullOrWhiteSpace(windowsDomain) ? new NetworkCredential(windowsUser, windowsPassword) : new NetworkCredential(windowsUser, windowsPassword, windowsDomain))));
        }
 internal TokenProvider CreateTokenProvider()
 {
     if (this.WindowsAuthentication != null && this.WindowsAuthentication.IsValid)
     {
         if (this.WindowsAuthentication.UseDefaultCredentials)
         {
             return(TokenProvider.CreateWindowsTokenProvider(this.WindowsAuthentication.StsUris.Addresses));
         }
         return(TokenProvider.CreateWindowsTokenProvider(this.WindowsAuthentication.StsUris.Addresses, new NetworkCredential(this.WindowsAuthentication.UserName, this.WindowsAuthentication.Password, this.WindowsAuthentication.Domain)));
     }
     if (this.SharedAccessSignature != null && this.SharedAccessSignature.IsValid)
     {
         return(TokenProvider.CreateSharedAccessSignatureTokenProvider(this.SharedAccessSignature.KeyName, this.SharedAccessSignature.Key, this.SharedAccessSignature.TokenScope));
     }
     return(TokenProvider.CreateSharedSecretTokenProvider(this.SharedSecret.IssuerName, this.SharedSecret.IssuerSecret, this.SharedSecret.TokenScope));
 }
Exemplo n.º 3
0
        /// <summary>
        /// Create new client for servicebus connection. This method is slow!
        /// </summary>
        /// <param name="connectionString">Connection string</param>
        /// <param name="operationTimeoutForClients">Operation timeout for clients</param>
        /// <returns>Object that can handle messaging to the service bus</returns>
        internal static MessagingFactory CreateMessagingFactoryWithTimeout(string connectionString, TimeSpan operationTimeoutForClients)
        {
            var connBuilder     = new ServiceBusConnectionStringBuilder(connectionString);
            var factorySettings = new MessagingFactorySettings
            {
                OperationTimeout = operationTimeoutForClients
            };

            if (connBuilder.SharedAccessKey != null)
            {
                factorySettings.TokenProvider = TokenProvider.CreateSharedAccessSignatureTokenProvider(connBuilder.SharedAccessKeyName, connBuilder.SharedAccessKey);
            }
            else if (connBuilder.SharedSecretIssuerName != null)
            {
                factorySettings.TokenProvider = TokenProvider.CreateSharedSecretTokenProvider(connBuilder.SharedSecretIssuerName, connBuilder.SharedSecretIssuerSecret);
            }
            else if (connBuilder.OAuthUsername != null)
            {
                if (!string.IsNullOrEmpty(connBuilder.OAuthDomain))
                {
                    TokenProvider.CreateOAuthTokenProvider(connBuilder.StsEndpoints,
                                                           new NetworkCredential(connBuilder.OAuthUsername,
                                                                                 connBuilder.OAuthPassword,
                                                                                 connBuilder.OAuthDomain));
                }
                else
                {
                    TokenProvider.CreateOAuthTokenProvider(connBuilder.StsEndpoints,
                                                           new NetworkCredential(connBuilder.OAuthUsername,
                                                                                 connBuilder.OAuthPassword));
                }
            }
            else if (connBuilder.StsEndpoints.Count > 0)
            {
                factorySettings.TokenProvider = TokenProvider.CreateWindowsTokenProvider(connBuilder.StsEndpoints);
            }


            factorySettings.EnableAdditionalClientTimeout = true;

            MessagingFactory messageFactory = MessagingFactory.Create(connBuilder.GetAbsoluteRuntimeEndpoints(), factorySettings);

            messageFactory.RetryPolicy = RetryPolicy.Default;

            return(messageFactory);
        }
        static void Main()
        {
            const string queueName = "ServiceBusSecurityQueueSample";

            //Create a NamespaceManager instance (for management operations)
            // NOTE: Namespace-level security is handled via PowerShell configuration on the namespace itself
            var namespaceManager = NamespaceManager.Create();


            // Example of granting a domain user listen permissions to a queue
            var          queue        = new QueueDescription(queueName);
            const string issuer       = "ServiceBusDefaultNamespace";
            var          domainUser   = string.Format(@"{0}@{1}", "REPLACE WITH USERNAME", Environment.GetEnvironmentVariable("USERDNSDOMAIN"));
            var          accessRights = new List <AccessRights> {
                AccessRights.Listen
            };

            AuthorizationRule listenRule = new AllowRule(issuer, "nameidentifier", domainUser, accessRights);

            queue.Authorization.Add(listenRule);

            if (namespaceManager.QueueExists(queueName))
            {
                namespaceManager.DeleteQueue(queueName);
            }
            queue = namespaceManager.CreateQueue(queue);


            //List out the access rules for the queue
            ListAccessRules(queue);


            //Create a MessagingFactory instance (for sending and receiving messages)
            const string hostname    = "REPLACE WITH FULLY-QUALIFIED SERVER NAME";
            const string sbNamespace = "ServiceBusDefaultNamespace";
            var          stsUris     = new List <Uri> {
                new Uri(string.Format(@"sb://{0}:9355/", hostname))
            };
            var tokenProvider = TokenProvider.CreateWindowsTokenProvider(stsUris);

            var runtimeAddress = string.Format("sb://{0}:9354/{1}/", hostname, sbNamespace);
            //var messageFactory = MessagingFactory.Create(runtimeAddress,
            //    new MessagingFactorySettings() { TokenProvider = tokenProvider,
            //        OperationTimeout = TimeSpan.FromMinutes(30) });
            var messageFactory = MessagingFactory.Create(runtimeAddress, tokenProvider);



            //Create a queue client to send and receive messages to and from the queue
            var myQueueClient = messageFactory.CreateQueueClient(queueName);

            //Create a simple brokered message and send it to the queue
            var sendMessage = new BrokeredMessage("Hello World!");

            myQueueClient.Send(sendMessage);
            Console.WriteLine("Message sent: Body = {0}", sendMessage.GetBody <string>());

            //Receive the message from the queue
            var receivedMessage = myQueueClient.Receive(TimeSpan.FromSeconds(5));

            if (receivedMessage != null)
            {
                Console.WriteLine("Message received: Body = {0}", receivedMessage.GetBody <string>());
                receivedMessage.Complete();
            }

            //Close the connection to the Service Bus
            messageFactory.Close();

            Console.WriteLine("Press Enter to close.");
            Console.ReadLine();
        }