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

  • SEARCH
  • Home
  • 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 6589105
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T17:07:48+00:00 2026-05-25T17:07:48+00:00

I am trying to implement security for the WCF rest service which will be

  • 0

I am trying to implement security for the WCF rest service which will be exposed over the net for consuming. Here are the requirements

Authorization for Service and Specific API’s

The service should authorize the partner and check if the partner has the access to the API which is called and i have multiple partners calling these restful APIs.

How do I authorize each of these partners for APIs in a centralized way?

Authentication for the User

I need to perform Authentication for users in order to perform the Add,Delete operations.

How do I authenticate the users for specific APIs in centralized way.

  • 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-25T17:07:49+00:00Added an answer on May 25, 2026 at 5:07 pm

    I used the following appraoch to implement the Authorization and Authentication fot the rest services.

    Used the Custom Attribute which Implements the Attribute, IOperationBehavior, IParameterInspector

    Here is the Implementation.

    public class AuthorizationAttribute : Attribute, IOperationBehavior, IParameterInspector
    {
    
        public SLCE.Operations Operation { get; set; }
    
        public bool IsAuthenticationRequired { get; set; }
    
        #region IOperationBehavior Members
    
        public void AddBindingParameters(OperationDescription operationDescription, BindingParameterCollection bindingParameters)
        {
    
        }
    
        public void ApplyClientBehavior(OperationDescription operationDescription, ClientOperation clientOperation)
        {
    
        }
    
        public void ApplyDispatchBehavior(OperationDescription operationDescription, DispatchOperation dispatchOperation)
        {           
            dispatchOperation.ParameterInspectors.Add(this);
        }
    
        public void Validate(OperationDescription operationDescription)
        {
    
        }
    
        #endregion
    
        #region IParameterInspector Members
    
        public void AfterCall(string operationName, object[] outputs, object returnValue, object correlationState)
        {
    
        }
    
        public object BeforeCall(string operationName, object[] inputs)
        {
            string publicKey = WebOperationContext.Current.IncomingRequest.Headers["Authorization"];
            bool flag = AuthorizationHelper.CheckPartnerAuthorization(this.Operation, publicKey);
            if (!flag)
            {   
                LicensingValidationHelper.ThrowLicensingException(HttpStatusCode.Unauthorized, SLCE.LicensingStatus.PartnerNotAuthorized.ToString());
            }
            else if(IsAuthenticationRequired)
            {
                string authenticationKey = WebOperationContext.Current.IncomingRequest.Headers["Authentication"];
                bool isAuthenticated = AuthorizationHelper.CheckUserAuthentication(authenticationKey);
    
                if (!isAuthenticated)
                {
                    LicensingValidationHelper.ThrowLicensingException(HttpStatusCode.Unauthorized, SLCE.LicensingStatus.UserNotAuthorized.ToString());                  
                }
            }
            return null;
        }
    
        #endregion      
    
    }
    

    Implemented a custom Beahavior to handle the exceptions.

    public class LicensingBehavior : WebHttpBehavior
    {
    
        protected override void AddServerErrorHandlers(ServiceEndpoint endpoint, EndpointDispatcher endpointDispatcher)
        {
            int errorHandlerCount = endpointDispatcher.ChannelDispatcher.ErrorHandlers.Count;
            base.AddServerErrorHandlers(endpoint, endpointDispatcher);
            IErrorHandler webHttpErrorHandler = endpointDispatcher.ChannelDispatcher.ErrorHandlers[errorHandlerCount];
            endpointDispatcher.ChannelDispatcher.ErrorHandlers.RemoveAt(errorHandlerCount);
            RestErrorHandler newHandler = new RestErrorHandler(webHttpErrorHandler,DefaultOutgoingResponseFormat);
            endpointDispatcher.ChannelDispatcher.ErrorHandlers.Add(newHandler);
        }        
    
    }
    

    Then implemented the IErrorhandler to send the status code and description if the Authorization or authentication fails.

    public class RestErrorHandler : IErrorHandler
    {
        IErrorHandler _originalErrorHandler;
        WebMessageFormat _format;
        public RestErrorHandler(IErrorHandler originalErrorHandler,WebMessageFormat format)
        {
            this._originalErrorHandler = originalErrorHandler;
            this._format = format;
        }
    
        public bool HandleError(Exception error)
        {
            return error is WebProtocolException;
        }
    
        public void ProvideFault(Exception error, MessageVersion version, ref Message fault)
        {
            WebProtocolException licensingException = error as WebProtocolException;
            if (licensingException != null)
            {
    
                fault = Message.CreateMessage(version, null, new ValidationErrorBodyWriter(licensingException));
                if (_format == WebMessageFormat.Json)
                {
                    HttpResponseMessageProperty prop = new HttpResponseMessageProperty();
                    prop.StatusCode = licensingException.StatusCode;
                    prop.Headers[HttpResponseHeader.ContentType] = "application/json; charset=utf-8";
                    fault.Properties.Add(HttpResponseMessageProperty.Name, prop);
                    fault.Properties.Add(WebBodyFormatMessageProperty.Name, new WebBodyFormatMessageProperty(WebContentFormat.Json));
                }
                else if(_format == WebMessageFormat.Xml)
                {
                    HttpResponseMessageProperty prop = new HttpResponseMessageProperty();
                    prop.StatusCode = licensingException.StatusCode;
                    prop.Headers[HttpResponseHeader.ContentType] = "application/xml; charset=utf-8";
                    fault.Properties.Add(HttpResponseMessageProperty.Name, prop);
                    fault.Properties.Add(WebBodyFormatMessageProperty.Name, new WebBodyFormatMessageProperty(WebContentFormat.Xml));
                }
            }
            else
            {
                this._originalErrorHandler.ProvideFault(error, version, ref fault);
            }
        }
    
        class ValidationErrorBodyWriter : BodyWriter
        {
            private WebProtocolException validationException;
            Encoding utf8Encoding = new UTF8Encoding(false);
    
            public ValidationErrorBodyWriter(WebProtocolException validationException)
                : base(true)
            {
                this.validationException = validationException;
            }
    
            protected override void OnWriteBodyContents(XmlDictionaryWriter writer)
            {
                writer.WriteStartElement("root");
                writer.WriteAttributeString("type", "object");
    
                writer.WriteStartElement("StatusCode");
                writer.WriteAttributeString("type", "string");
                writer.WriteString(this.validationException.StatusCode.ToString());
                writer.WriteEndElement();
    
                writer.WriteStartElement("Description");
                writer.WriteAttributeString("type", "string");
                writer.WriteString(this.validationException.StatusDescription);
                writer.WriteEndElement();
    
                writer.WriteEndElement();
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to implement a very granular security module in an ASP.NET MVC 3
I'm trying to implement a front end for a reporting solution which is security
I'm trying to implement spring-security to handle authentication and authorization of my web application.
I am trying to create a WCF client that operates with an http rest
im trying to implement simple secured client server communiction using WCF. when im launching
I am using form-login for security and I am trying to implement an authentication
I am trying to implement security for the following architecture: Web tier: Tomcat 7
I'm trying to implement row-based security checks for Django models. The idea is that
I'm trying to implement Authorization and Authentication in my current winforms project. The Authentication
I'm trying to implement adaptive payments but keep getting this weird error. Here's 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.