Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1056901
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T17:47:02+00:00 2026-05-16T17:47:02+00:00

i need to pass a value from client everytime a request sent to WCF

  • 0

i need to pass a value from client everytime a request sent to WCF and check that value on server and decide to make the request or not, can any one write an example of that ?im not sure how is that is going be implemented

case:
im generating a key based on the hardware of the client and i want to send that key to the server with every request to check if the key is accepted in the server db and then decide to process the request or not.

thanks in advance.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T17:47:03+00:00Added an answer on May 16, 2026 at 5:47 pm

    first we need to implement Behavior and inspector in the client side to send Key to authenticate the client :

     class AuthenticationBehaviour : IEndpointBehavior
    {
        #region IEndpointBehavior Members
    
        public void AddBindingParameters(ServiceEndpoint endpoint, System.ServiceModel.Channels.BindingParameterCollection bindingParameters)
        {
        }
    
        public void ApplyClientBehavior(ServiceEndpoint endpoint, System.ServiceModel.Dispatcher.ClientRuntime clientRuntime)
        {
            AuthenticationMessageInspector inspector = new AuthenticationMessageInspector();
            clientRuntime.MessageInspectors.Add(inspector);
        }
    
        public void ApplyDispatchBehavior(ServiceEndpoint endpoint, System.ServiceModel.Dispatcher.EndpointDispatcher endpointDispatcher)
        {
            //AuthenticationMessageInspector inspector = new AuthenticationMessageInspector();
            //endpointDispatcher.DispatchRuntime.MessageInspectors.Add(inspector);
        }
    
        public void Validate(ServiceEndpoint endpoint)
        {
        }
    
        class AuthenticationMessageInspector : IClientMessageInspector
    {
        private const string HeaderKey = "Authentication";
    
        public object BeforeSendRequest(ref Message request, IClientChannel channel)
        {
    
            if (Session.MachineId == 0)
            {
                Session.MachineId = LicenseGenerator.GenerateLicense();
            }
    
    
            request.Headers.Add(MessageHeader.CreateHeader(HeaderKey, string.Empty, Session.MachineId));
            return null;
        }
    
        public void AfterReceiveReply(ref Message reply, object correlationState)
        {
    
        }
    }
    

    now we need to implement Behavior and inspector in the server side (WCF service) to inspect every request made and extract the header then validate it :

      public class AuthenticationBehaviour : IEndpointBehavior
    {
        #region IEndpointBehavior Members
    
        public void AddBindingParameters(ServiceEndpoint endpoint, System.ServiceModel.Channels.BindingParameterCollection bindingParameters)
        {
        }
    
        public void ApplyClientBehavior(ServiceEndpoint endpoint, System.ServiceModel.Dispatcher.ClientRuntime clientRuntime)
        {
            //AuthenticationMessageInspector inspector = new AuthenticationMessageInspector();
            //clientRuntime.MessageInspectors.Add(inspector);
        }
    
        public void ApplyDispatchBehavior(ServiceEndpoint endpoint, System.ServiceModel.Dispatcher.EndpointDispatcher endpointDispatcher)
        {
            AuthenticationMessageInspector inspector = new AuthenticationMessageInspector();
            endpointDispatcher.DispatchRuntime.MessageInspectors.Add(inspector);
            //Console.WriteLine("Dispatcher Applied!");
        }
    
        public void Validate(ServiceEndpoint endpoint)
        {
        }
    
        #endregion
    }
    
       public class AuthenticationMessageInspector : IDispatchMessageInspector
    
    {
    
        #region Members
        private string conStr = "", commStr = "";
        public IDbConnection Connection { get; set; }
        public IDbCommand Command { get; set; }
        public IDataReader Reader { get; set; }
    
        #endregion        
        private const string HeaderKey = "Authentication";
        public object AfterReceiveRequest(ref Message request, IClientChannel channel, InstanceContext instanceContext)
        {
            //Console.WriteLine("recieved Request! ");
            int headerIndex = request.Headers.FindHeader(HeaderKey, string.Empty);
            if (headerIndex < 0 || string.IsNullOrEmpty(request.Headers.GetHeader<String>(headerIndex)))
            {
    
                throw (new Exception("Access Denied!\n"));
                return null;
            } 
    
    
            bool valid = Validate(request.Headers.GetHeader<String>(headerIndex));
            if (!valid)
            {
                Console.WriteLine("recieved Request! From " + request.Headers.GetHeader<String>(headerIndex) + " and Access Denied!\n");
                throw (new Exception("Access Denied!\n" + request.Headers.GetHeader<String>(headerIndex) + " License Number is not athourized! "));         
            }
            if (headerIndex != -1)
            {
                Console.WriteLine("recieved Request! From " + request.Headers.GetHeader<String>(headerIndex));
            }
            return null;
    
        }
    
        public void BeforeSendReply(ref Message reply, object correlationState)
        {
    
        }
    }
    

    now lets register the behavior :

                _routingHost.Description.Endpoints[0].Behaviors.Add(new Gateway.Controllers.AuthenticationBehaviour());
    _routingHost.Open();
    

    that is it thanks.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a JSON object that I need to pass from the server to
I need to pass a variable which is captured from client side via jquery
I need to pass (via string content of a HTTP request/response body) name value
I need to pass a value from PHP to C++. I think I can
I'm uwsing MVC and jqgrid and I need to pass a value from dropdownlist
I've some scenarios where i need to pass value type as reference without changed
I have an ASP control custom built and I need to pass a value
I am trying pass class as value in hashmap. I need to get the
I need to pass an extra parameter :mobilejs => true from jQuery to a
I need to pass a parameter from an EditText and when I click the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.