//This is using created key which it should not happen in production private static void WS2007HttpRelayClientThree() { Console.WriteLine("Please enter any key to contact server..."); Console.ReadLine(); // Create the binding with default settings. WS2007HttpRelayBinding binding = new WS2007HttpRelayBinding(); // it must be false to use previously generated relay endpoint and its special credentials binding.IsDynamic = false; var cf = new ChannelFactory<IEchoContract>( binding, new EndpointAddress(ServiceBusEnvironment.CreateServiceUri("https", "johnsonwangnz", "MyService"))); cf.Endpoint.Behaviors.Add( new TransportClientEndpointBehavior { TokenProvider = TokenProvider.CreateSharedAccessSignatureTokenProvider("SendAccessKeyMyService", "e6b/gSiveCenfVEqBbUjTzuiAdRmjlcdT0ojMW1T2e8=") }); var ch = cf.CreateChannel(); var result = ch.Echo("HelloWorld!"); Console.WriteLine(result); Console.ReadLine(); }
static ServiceEndpointCollection QueryMexEndpoint(string mexAddress, Binding binding, string issuer, string secret) { Binding extendedBinding = null; if (binding is NetTcpRelayBinding) { NetTcpRelayBinding actualBinding = binding as NetTcpRelayBinding; actualBinding.MaxReceivedMessageSize *= MessageSizeMultiplier; extendedBinding = actualBinding; } if (binding is WS2007HttpRelayBinding) { WS2007HttpRelayBinding actualBinding = binding as WS2007HttpRelayBinding; actualBinding.MaxReceivedMessageSize *= MessageSizeMultiplier; extendedBinding = actualBinding; } MetadataExchangeClient mexClient = new MetadataExchangeClient(extendedBinding); mexClient.SetServiceBusCredentials(issuer, secret); MetadataSet metadata = mexClient.GetMetadata(new EndpointAddress(mexAddress)); MetadataImporter importer = new WsdlImporter(metadata); return(importer.ImportAllEndpoints()); }
//The same as two, but different style creating environments private static void WS2007HttpRelayClientOne() { Console.WriteLine("Please enter any key to contact server..."); Console.ReadLine(); // Configure the credentials through an endpoint behavior. TransportClientEndpointBehavior relayCredentials = new TransportClientEndpointBehavior(); relayCredentials.TokenProvider = TokenProvider.CreateSharedAccessSignatureTokenProvider("RootManageSharedAccessKey", "fBLL/4/+rEsCOiTQPNPS6DJQybykqE2HdVBsILrzMLY="); // Get the service address. // Use the https scheme because by default the binding uses SSL for transport security. Uri address = ServiceBusEnvironment.CreateServiceUri("https", "johnsonwangnz", "MyService"); // Create the binding with default settings. WS2007HttpRelayBinding binding = new WS2007HttpRelayBinding(); // Create a channel factory for the specified channel type. // This channel factory is used to create client channels to the service. // Each client channel the channel factory creates is configured to use the // WS2007HttpRelayBinding that is passed to the constructor of the channel. var channelFactory = new ChannelFactory<IEchoContract>( binding, new EndpointAddress(address)); channelFactory.Endpoint.Behaviors.Add(relayCredentials); // Create and open the client channel. IEchoContract echoService = channelFactory.CreateChannel(); var result = echoService.Echo("HelloWorld!"); Console.WriteLine(result); Console.ReadLine(); }
private static void Three() { Console.WriteLine("Starting service..."); // Configure the credentials through an endpoint behavior. TransportClientEndpointBehavior relayCredentials = new TransportClientEndpointBehavior(); relayCredentials.TokenProvider = TokenProvider.CreateSharedAccessSignatureTokenProvider("ListenAccessKeyMyService", "3VXc5wQwu479N/w2MaLtsk9fA7WWJsamsxtWcr8zbCY="); // Create the binding with default settings. WS2007HttpRelayBinding binding = new WS2007HttpRelayBinding(); // it must be false to use previously generated relay endpoint and its special credentials binding.IsDynamic = false; //binding.Security.Mode = EndToEndSecurityMode.Message; // Get the service address. // Use the https scheme because by default the binding uses SSL for transport security. Uri address = ServiceBusEnvironment.CreateServiceUri("https", "johnsonwangnz", "MyService"); // Create the service host. ServiceHost host = new ServiceHost(typeof(EchoService), address); // Add the service endpoint with the WS2007HttpRelayBinding. host.AddServiceEndpoint(typeof(IEchoContract), binding, address); // Add the credentials through the endpoint behavior. host.Description.Endpoints[0].Behaviors.Add(relayCredentials); // Start the service. host.Open(); Console.WriteLine("Listening..."); Console.ReadLine(); host.Close(); }
static void Main(string[] args) { ServiceBusEnvironment.SystemConnectivity.Mode = ConnectivityMode.Http; Console.Write("Your Service Namespace: "); string serviceNamespace = Console.ReadLine(); Console.Write("Your Issuer Name: "); string issuerName = Console.ReadLine(); // The issuer secret is the Service Bus namespace management key. Console.Write("Your Issuer Secret: "); string issuerSecret = Console.ReadLine(); // Create the service URI based on the service namespace. Uri address = ServiceBusEnvironment.CreateServiceUri(Uri.UriSchemeHttps, serviceNamespace, "RemoteService"); Console.WriteLine("Service address: " + address); // Create the credentials object for the endpoint. TransportClientEndpointBehavior sharedSecretServiceBusCredential = new TransportClientEndpointBehavior() { TokenProvider = TokenProvider.CreateSharedSecretTokenProvider(issuerName, issuerSecret) }; // Create the binding object. WS2007HttpRelayBinding binding = new WS2007HttpRelayBinding(); binding.Security.Mode = EndToEndSecurityMode.Transport; // Create the service host reading the configuration. ServiceHost host = new ServiceHost(typeof(RemoteService)); host.AddServiceEndpoint(typeof(IServiceEndpointPlugin), binding, address); // Create the ServiceRegistrySettings behavior for the endpoint. IEndpointBehavior serviceRegistrySettings = new ServiceRegistrySettings(DiscoveryType.Public); // Add the Service Bus credentials to all endpoints specified in configuration. foreach (ServiceEndpoint endpoint in host.Description.Endpoints) { endpoint.Behaviors.Add(serviceRegistrySettings); endpoint.Behaviors.Add(sharedSecretServiceBusCredential); } // Open the service. host.Open(); Console.WriteLine("Press [Enter] to exit"); Console.ReadLine(); // Close the service. Console.Write("Closing the service host..."); host.Close(); Console.WriteLine(" done."); }
static void Listen() { string serviceNamespace; string serviceIdentityName; string serviceIdentityKey; Console.WriteLine("Service Namespace:"); serviceNamespace = Console.ReadLine(); Console.WriteLine("Service Identity:"); serviceIdentityName = Console.ReadLine(); Console.WriteLine("Service Identity Key:"); serviceIdentityKey = Console.ReadLine(); TransportClientEndpointBehavior behavior = new TransportClientEndpointBehavior(); behavior.TokenProvider = SharedSecretTokenProvider.CreateSharedSecretTokenProvider(serviceIdentityName, serviceIdentityKey); var address = ServiceBusEnvironment.CreateServiceUri(Uri.UriSchemeHttps, serviceNamespace, "Demo/OneWayListener"); // binding var binding = new WS2007HttpRelayBinding(); binding.Security.Mode = EndToEndSecurityMode.Transport; var host = new ServiceHost(typeof (CRMOnlineOneWayListenerService)); var endPoint = host.AddServiceEndpoint(typeof (IServiceEndpointPlugin), binding, address); var serviceRegistrySettings = new ServiceRegistrySettings(DiscoveryType.Private); endPoint.Behaviors.Add(serviceRegistrySettings); endPoint.Behaviors.Add(behavior); try { // Open the service. host.Open(); Console.WriteLine("Host Open. Listening on endpoint Address: " + address); } catch (TimeoutException timeout) { Console.WriteLine("Opening the service timed out, details below:"); Console.WriteLine(timeout.Message); } Console.WriteLine("Press [Enter] to exit"); Console.ReadLine(); // Close the service. Console.Write("Closing the service host..."); host.Close(); Console.WriteLine(" done."); }
public static ServiceEndpoint[] GetEndpoints(string mexAddress,string issuer,string secret) { if(mexAddress == null || mexAddress == "") { throw new ArgumentException("mexAddress"); } Uri address = new Uri(mexAddress); ServiceEndpointCollection endpoints = null; Binding binding; if(address.Scheme == "sb") { binding = new NetTcpRelayBinding(); } else { Debug.Assert(address.Scheme == "http" || address.Scheme == "https"); binding = new WS2007HttpRelayBinding(); } try { endpoints = QueryMexEndpoint(mexAddress,binding,issuer,secret); } catch {} if(endpoints != null) { return endpoints.ToArray(); } else { return new ServiceEndpoint[]{}; } }
public static ServiceEndpoint[] GetEndpoints(string mexAddress, string issuer, string secret) { if (string.IsNullOrWhiteSpace(mexAddress)) { throw new ArgumentException("mexAddress"); } Uri address = new Uri(mexAddress); ServiceEndpointCollection endpoints = null; Binding binding; if (address.Scheme == "sb") { binding = new NetTcpRelayBinding(); } else { Debug.Assert(address.Scheme == "http" || address.Scheme == "https"); binding = new WS2007HttpRelayBinding(); } try { endpoints = QueryMexEndpoint(mexAddress, binding, issuer, secret); } catch {} if (endpoints != null) { return(endpoints.ToArray()); } else { return(new ServiceEndpoint[] {}); } }
public static ServiceEndpoint[] GetEndpoints(string mexAddress,TokenProvider tokenProvider) { if(string.IsNullOrWhiteSpace(mexAddress)) { throw new ArgumentException("mexAddress"); } Uri address = new Uri(mexAddress); ServiceEndpointCollection endpoints = null; Binding binding; if(address.Scheme == "sb") { binding = new NetTcpRelayBinding(); } else { Debug.Assert(address.Scheme == "http" || address.Scheme == "https"); binding = new WS2007HttpRelayBinding(); } try { endpoints = QueryMexEndpoint(mexAddress,binding,tokenProvider); } catch {} if(endpoints != null) { return endpoints.ToArray(); } else { return new ServiceEndpoint[]{}; } }
//This is using root key which should be avoided in production private static void Two() { Console.WriteLine("Starting service..."); // Configure the credentials through an endpoint behavior. TransportClientEndpointBehavior relayCredentials = new TransportClientEndpointBehavior(); relayCredentials.TokenProvider = TokenProvider.CreateSharedAccessSignatureTokenProvider("RootManageSharedAccessKey", "fBLL/4/+rEsCOiTQPNPS6DJQybykqE2HdVBsILrzMLY="); // Create the binding with default settings. WS2007HttpRelayBinding binding = new WS2007HttpRelayBinding(); //binding.Security.Mode = EndToEndSecurityMode.Message; // Get the service address. // Use the https scheme because by default the binding uses SSL for transport security. Uri address = ServiceBusEnvironment.CreateServiceUri("https", "johnsonwangnz", "MyService"); // Create the service host. ServiceHost host = new ServiceHost(typeof(EchoService), address); // Add the service endpoint with the WS2007HttpRelayBinding. host.AddServiceEndpoint(typeof(IEchoContract), binding, address); // Add the credentials through the endpoint behavior. host.Description.Endpoints[0].Behaviors.Add(relayCredentials); // Start the service. host.Open(); Console.WriteLine("Listening..."); Console.ReadLine(); host.Close(); }
/// <summary> /// Prompts for required information and hosts a service until the user ends the /// session. /// </summary> public void Run() { //<snippetTwoWayListener1> ServiceBusEnvironment.SystemConnectivity.Mode = ConnectivityMode.Http; Console.Write("Enter your Azure service namespace: "); string serviceNamespace = Console.ReadLine(); // The service namespace issuer name to use. If one hasn't been setup // explicitly it will be the default issuer name listed on the service // namespace. Console.Write("Enter your service namespace issuer name: "); string issuerName = Console.ReadLine(); // Issuer secret is the Windows Azure Service Bus namespace current management key. Console.Write("Enter your service namespace issuer key: "); string issuerKey = Console.ReadLine(); // Input the same path that was specified in the Service Bus Configuration dialog // when registering the Azure-aware plug-in with the Plug-in Registration tool. Console.Write("Enter your endpoint path: "); string servicePath = Console.ReadLine(); // Leverage the Azure API to create the correct URI. Uri address = ServiceBusEnvironment.CreateServiceUri( Uri.UriSchemeHttps, serviceNamespace, servicePath); Console.WriteLine("The service address is: " + address); // Create the shared secret credentials object for the endpoint matching the // Azure access control services issuer var sharedSecretServiceBusCredential = new TransportClientEndpointBehavior() { TokenProvider = TokenProvider.CreateSharedSecretTokenProvider(issuerName, issuerKey) }; // Using an HTTP binding instead of a SOAP binding for this endpoint. WS2007HttpRelayBinding binding = new WS2007HttpRelayBinding(); binding.Security.Mode = EndToEndSecurityMode.Transport; // Create the service host for Azure to post messages to. ServiceHost host = new ServiceHost(typeof(TwoWayEndpoint)); host.AddServiceEndpoint(typeof(ITwoWayServiceEndpointPlugin), binding, address); // Create the ServiceRegistrySettings behavior for the endpoint. var serviceRegistrySettings = new ServiceRegistrySettings(DiscoveryType.Public); // Add the service bus credentials to all endpoints specified in configuration. foreach (var endpoint in host.Description.Endpoints) { endpoint.Behaviors.Add(serviceRegistrySettings); endpoint.Behaviors.Add(sharedSecretServiceBusCredential); } // Begin listening for messages posted to Azure. host.Open(); Console.WriteLine(Environment.NewLine + "Listening for messages from Azure" + Environment.NewLine + "Press [Enter] to exit"); // Keep the listener open until Enter is pressed. Console.ReadLine(); Console.Write("Closing the service host..."); host.Close(); Console.WriteLine(" done."); //</snippetTwoWayListener1> }
private static void UserNameClient() { Console.WriteLine("Starting service..."); // Configure the credentials through an endpoint behavior. TransportClientEndpointBehavior relayCredentials = new TransportClientEndpointBehavior(); relayCredentials.TokenProvider = TokenProvider.CreateSharedAccessSignatureTokenProvider("ListenAccessKeyMyService", "3VXc5wQwu479N/w2MaLtsk9fA7WWJsamsxtWcr8zbCY="); // Create the binding with default settings. WS2007HttpRelayBinding binding = new WS2007HttpRelayBinding(); // it must be false to use previously generated relay endpoint and its special credentials binding.IsDynamic = false; binding.Security.Mode = EndToEndSecurityMode.TransportWithMessageCredential; binding.Security.Message.ClientCredentialType = MessageCredentialType.UserName; binding.Security.Message.EstablishSecurityContext = false; binding.Security.Message.NegotiateServiceCredential = false; // Get the service address. // Use the https scheme because by default the binding uses SSL for transport security. Uri address = ServiceBusEnvironment.CreateServiceUri("https", "johnsonwangnz", "MyService"); // Create the service host. ServiceHost host = new ServiceHost(typeof(EchoService), address); host.Credentials.ServiceCertificate.Certificate = MyCertificates.GetTestServiceCertificate(); //Change user name authentication host.Credentials.UserNameAuthentication. UserNamePasswordValidationMode = UserNamePasswordValidationMode.MembershipProvider; host.Credentials.UserNameAuthentication.MembershipProvider = new MyMemberShipProvider(); //set the authorization of user var serviceAuthroization = host.Authorization; serviceAuthroization.PrincipalPermissionMode = PrincipalPermissionMode.UseAspNetRoles; serviceAuthroization.RoleProvider = new MyRoleProvider(); // Add the service endpoint with the WS2007HttpRelayBinding. host.AddServiceEndpoint(typeof(IEchoContract), binding, address); // Add the credentials through the endpoint behavior. host.Description.Endpoints[0].Behaviors.Add(relayCredentials); // Start the service. host.Open(); Console.WriteLine("Listening..."); Console.ReadLine(); host.Close(); }
private static void WS2007HttpRelayUserNameClient() { Console.WriteLine("Please enter any key to contact server..."); Console.ReadLine(); EndpointIdentity spn = EndpointIdentity.CreateDnsIdentity("Johnson.Test.Service"); // Configure the credentials through an endpoint behavior. TransportClientEndpointBehavior relayCredentials = new TransportClientEndpointBehavior(); relayCredentials.TokenProvider = TokenProvider.CreateSharedAccessSignatureTokenProvider("SendAccessKeyMyService", "e6b/gSiveCenfVEqBbUjTzuiAdRmjlcdT0ojMW1T2e8="); // Get the service address. // Use the https scheme because by default the binding uses SSL for transport security. Uri address = ServiceBusEnvironment.CreateServiceUri("https", "johnsonwangnz", "MyService"); // Create the binding with default settings. WS2007HttpRelayBinding binding = new WS2007HttpRelayBinding(); // it must be false to use previously generated relay endpoint and its special credentials binding.IsDynamic = false; binding.Security.Mode = EndToEndSecurityMode.TransportWithMessageCredential; binding.Security.Message.ClientCredentialType = MessageCredentialType.UserName; binding.Security.Message.EstablishSecurityContext = false; binding.Security.Message.NegotiateServiceCredential = false; // Create a channel factory for the specified channel type. // This channel factory is used to create client channels to the service. // Each client channel the channel factory creates is configured to use the // WS2007HttpRelayBinding that is passed to the constructor of the channel. var channelFactory = new ChannelFactory<IEchoContract>( binding, new EndpointAddress(address,spn)); //When service negotiation is off, but no need of validation channelFactory.Credentials.ServiceCertificate.DefaultCertificate = MyCertificates.GetTestServiceCertificatePublicKeyOnly(); channelFactory.Credentials.UserName.UserName = "******"; channelFactory.Credentials.UserName.Password = "******"; channelFactory.Endpoint.Behaviors.Add(relayCredentials); // Create and open the client channel. IEchoContract echoService = channelFactory.CreateChannel(); var result = echoService.Echo("HelloWorld!"); Console.WriteLine(result); Console.ReadLine(); }
static void Main(string[] args) { ServiceBusEnvironment.SystemConnectivity.Mode = ConnectivityMode.Http; // The one specified when creating the azure bus Console.Write("Enter your Azure service namespace: "); string serviceNamespace = Console.ReadLine(); // The shared access key policy name Console.Write("Enter your shared access policy name: "); string sharedAccesKeyName = Console.ReadLine(); // The primary of they access key policy specificied above Console.Write("Enter your shared access policy key: "); string sharedAccessKey = Console.ReadLine(); // Input the same path that was specified in the Service Bus Configuration dialog // when registering the Azure-aware plug-in with the Plug-in Registration tool. Console.Write("Enter your endpoint path: "); string servicePath = Console.ReadLine(); // Leverage the Azure API to create the correct URI. Uri address = ServiceBusEnvironment.CreateServiceUri( Uri.UriSchemeHttps, serviceNamespace, servicePath); Console.WriteLine("Service address: " + address); // Create the credentials object for the endpoint. TransportClientEndpointBehavior sharedAccessServiceBusCredential = new TransportClientEndpointBehavior() { TokenProvider = TokenProvider.CreateSharedAccessSignatureTokenProvider(sharedAccesKeyName, sharedAccessKey) }; // Create the binding object. WS2007HttpRelayBinding binding = new WS2007HttpRelayBinding(); binding.Security.Mode = EndToEndSecurityMode.Transport; // Create the service host reading the configuration. ServiceHost host = new ServiceHost(typeof(RemoteService)); host.AddServiceEndpoint(typeof(IServiceEndpointPlugin), binding, address); // Create the ServiceRegistrySettings behavior for the endpoint. IEndpointBehavior serviceRegistrySettings = new ServiceRegistrySettings(DiscoveryType.Public); // Add the Service Bus credentials to all endpoints specified in configuration. foreach (ServiceEndpoint endpoint in host.Description.Endpoints) { endpoint.Behaviors.Add(serviceRegistrySettings); endpoint.Behaviors.Add(sharedAccessServiceBusCredential); } // Open the service. host.Open(); Console.WriteLine("Press [Enter] to exit"); Console.ReadLine(); // Close the service. Console.Write("Closing the service host..."); host.Close(); Console.WriteLine(" done."); }
/// <summary> /// Standard Main() method used by most SDK samples. /// </summary> static public void Main() { try { ServiceBusEnvironment.SystemConnectivity.Mode = ConnectivityMode.Http; string serviceNamespace = "democrmservicebus"; string issuerName = "owner"; string issuerKey = "<Your ACS Default Key Here>"; string servicePath = "Demo/TwoWay"; // Leverage the Azure API to create the correct URI. Uri address = ServiceBusEnvironment.CreateServiceUri( Uri.UriSchemeHttps, serviceNamespace, servicePath); Console.WriteLine("The service address is: " + address); // Using an HTTP binding instead of a SOAP binding for this endpoint. WS2007HttpRelayBinding binding = new WS2007HttpRelayBinding(); binding.Security.Mode = EndToEndSecurityMode.Transport; // Create the service host for Azure to post messages to. ServiceHost host = new ServiceHost(typeof(TwoWayEndpoint)); host.AddServiceEndpoint(typeof(ITwoWayServiceEndpointPlugin), binding, address); // Create the shared secret credentials object for the endpoint matching the // Azure access control services issuer var sharedSecretServiceBusCredential = new TransportClientEndpointBehavior() { TokenProvider = TokenProvider.CreateSharedSecretTokenProvider(issuerName, issuerKey) }; // Add the service bus credentials to all endpoints specified in configuration. foreach (var endpoint in host.Description.Endpoints) { endpoint.Behaviors.Add(sharedSecretServiceBusCredential); } // Begin listening for messages posted to Azure. host.Open(); Console.WriteLine(Environment.NewLine + "Listening for messages from Azure" + Environment.NewLine + "Press [Enter] to exit"); // Keep the listener open until Enter is pressed. Console.ReadLine(); Console.Write("Closing the service host..."); host.Close(); Console.WriteLine(" done."); } catch (FaultException <ServiceEndpointFault> ex) { Console.WriteLine("The application terminated with an error."); Console.WriteLine("Message: {0}", ex.Detail.Message); Console.WriteLine("Inner Fault: {0}", null == ex.InnerException.Message ? "No Inner Fault" : "Has Inner Fault"); } catch (System.TimeoutException ex) { Console.WriteLine("The application terminated with an error."); Console.WriteLine("Message: {0}", ex.Message); Console.WriteLine("Stack Trace: {0}", ex.StackTrace); Console.WriteLine("Inner Fault: {0}", null == ex.InnerException.Message ? "No Inner Fault" : ex.InnerException.Message); } catch (System.Exception ex) { Console.WriteLine("The application terminated with an error."); Console.WriteLine(ex.Message); // Display the details of the inner exception. if (ex.InnerException != null) { Console.WriteLine(ex.InnerException.Message); FaultException <ServiceEndpointFault> fe = ex.InnerException as FaultException <ServiceEndpointFault>; if (fe != null) { Console.WriteLine("Message: {0}", fe.Detail.Message); Console.WriteLine("Inner Fault: {0}", null == ex.InnerException.Message ? "No Inner Fault" : "Has Inner Fault"); } } } finally { Console.WriteLine("Press <Enter> to exit."); Console.ReadLine(); } }