none
[Question] How to get client authorization information from service layer ? RRS feed

  • Question

  • Hi everyone,

    I'm new to WCF, I'm trying to make a chat application with client authentication with Duplex chanel

    Here is my code :

    [ServiceContract(SessionMode = SessionMode.Required, CallbackContract = typeof(IDuplexCallback))]
        public interface IDuplexService
        {
            [OperationContract(IsOneWay = true)]
            void RegisterClient(string clientName);
    
            [OperationContract(IsOneWay = true)]
            void FormatString(string institution);
        }

    public interface IDuplexCallback
        {
            [OperationContract(IsOneWay = true)]
            void DuplexCallbackFunction(string requestString);
        }

    public class DuplexCallbackHandler : IDuplexCallback
        {
            #region IDuplexServiceCallback Members
            /// <summary>
            /// Callback function
            /// </summary>
            /// <param name="requestString"></param>
            public void DuplexCallbackFunction(string requestString)
            {
                Console.WriteLine("Request string = {0}", requestString);
            }
    
            #endregion
        }


    ServiceLayer

    var serviceAddress = "http://localhost:27020/DuplexService";
    
                // Create an isntance of service host which manages connection from clients.
                var serviceHost = new ServiceHost(typeof(DuplexService), new Uri(serviceAddress));
    
                // Create an instance of ServiceMetadataBehavior class to define what server will behave.
                var serviceMetadataBehavior = new ServiceMetadataBehavior();
                
                serviceHost.Description.Behaviors.Add(serviceMetadataBehavior);
                serviceHost.AddServiceEndpoint(typeof(IMetadataExchange),
                    MetadataExchangeBindings.CreateMexHttpBinding(), "mex");
    
                // Create an instance of BasicHttpBinding with default initialization.
                var wsDualHttpBinding = new WSDualHttpBinding();
    
                serviceHost.AddServiceEndpoint(typeof(IDuplexService), wsDualHttpBinding, serviceAddress);
                serviceHost.Open();
    
                Console.WriteLine("Service has started. Press enter to terminate");
                Console.ReadLine();

    ClientLayer

    var serviceAddress = "http://localhost:27020/DuplexService";
    
                var handler = new DuplexCallbackHandler();
                
                // Create an instance of HttpBinding with default initialization.
                var wsDualHttpBinding = new WSDualHttpBinding();
    
                // Create a channel factory to connect to service with default basic http binding.        
                var channelFactory = new DuplexChannelFactory<IDuplexService>(handler, wsDualHttpBinding);
                
                // Create End Point 
                var endpointAddress = new EndpointAddress(serviceAddress);
    
                // Create Channel 
                var proxy = channelFactory.CreateChannel(endpointAddress);
    
                if (proxy == null)
                {
                    Console.Write("Invalid proxy\n");
                    Console.ReadLine();
                    return;
                }
    
                proxy.RegisterClient(Guid.NewGuid().ToString());
                proxy.FormatString("This is a formatted string");
                Console.ReadLine();

    My question is :

    ->How can my client send its authentication information to service ? (Name and password - which are defined in a class, i.e : AuthenticationInfo)

    ->How can my service retrieve the authentication information from client (Name and password)

    Thank you

    Tuesday, December 8, 2015 12:55 PM

Answers

All replies

  • You should create a class that derives from UserNamePasswordValidator and set up your service to use to use a custom user name and password validator as described on MSDN here: https://msdn.microsoft.com/en-us/library/aa702565(v=vs.110).aspx

    Here is an example that includes the client side code used to pass the custom credentials to the server: http://www.codeproject.com/Articles/96028/WCF-Service-with-custom-username-password-authenti

    Hope that helps.

    Please remember to close your threads by marking helpful posts as answer and then start a new thread if you have a new question. Please don't ask several questions in the same thread.

    Tuesday, December 8, 2015 3:21 PM
  • Thank you for answering me :)

    As you see, I'm making a self-hosted application above.

    How can I implement it into my self-hosted application ?

    Wednesday, December 9, 2015 11:45 AM
  • >>How can I implement it into my self-hosted application ?

    As per my previous reply. You do the exact same thing to enable custom custom authentication regardless of whether your service is hosted in a managed desktop application, a windows service, in a web application or somewhere else.

    Please remember to close your threads by marking all helpful posts as answer and then start a new thread if you have a new question. Please don't ask several questions in the same thread and please don't ask the same question twice.

    Thursday, December 10, 2015 3:11 PM