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 5848841
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T12:54:58+00:00 2026-05-22T12:54:58+00:00

I’ve created a WCF DataService and for this service I require some custom authentication

  • 0

I’ve created a WCF DataService and for this service I require some custom authentication through the use of HTTP Headers. So i’ve written special functions that validate this information or throw an 403 page in the user’s face when he’s not allowed to see it.

To make it easy for myself, I tried to overwrite the OnStartProcessingRequest to perform this check every call, but for some reason this function is never called by my code/WCF service :S

This is the code of the WCF Service:

using System;
using System.Data.Services;
using System.Linq;
using System.Text;
using System.Web;

namespace TenForce.Execution.Web.OData
{
public class TenForceApi : DataService<Entities>
{
    // This method is called only once to initialize service-wide policies.
    public static void InitializeService(IDataServiceConfiguration config)
    {
        config.SetEntitySetAccessRule("*", EntitySetRights.All);
        config.UseVerboseErrors = true;
        config.SetServiceOperationAccessRule("*", ServiceOperationRights.All);
    }

    /// <summary>
    /// <para>This function is called prior to handeling requests. The function will perform basic
    /// authentication using the Headers supplied by the client.</para>
    /// </summary>
    /// <param name="args">The arguments supplied for this call.</param>
    protected override void OnStartProcessingRequest(ProcessRequestArgs args)
    {
        HttpContext context = HttpContext.Current;
        string customAuthHeader = ExtractAuthenticationToken(context);
        ValidateAuthentication(customAuthHeader.Split('|'), context);
        base.OnStartProcessingRequest(args);
    }

    #region Private Members

    /// <summary>
    /// <para>This function will extract the custom tenforce authentication header from the
    /// http context and return the value of that header. If the header cannot be found, a
    /// DataServiceException is thrown.</para>
    /// </summary>
    /// <param name="context">The HttpContext object containing the custom HTTP header.</param>
    /// <returns>The value of the header</returns>
    /// <exception cref="DataServiceException">No Authentication Header provided.</exception>
    private static string ExtractAuthenticationToken(HttpContext context)
    {
        if (!context.Request.Headers.AllKeys.Contains(@"TenForce-Auth"))
            throw new DataServiceException(403, @"No authentication header provided.");
        return Encoding.UTF8.GetString(Convert.FromBase64String(context.Request.Headers[@"TenForce-Auth"]));
    }

    /// <summary>
    /// <para>Validates the authentication credentials stored inside the array.</para>
    /// </summary>
    /// <param name="values">Array holding the required authentication details.</param>
    /// <param name="context">The HttpContext holding the Request and Response for this call.</param>
    private static void ValidateAuthentication(string[] values, HttpContext context)
    {
        if (values.Length != 2) throw new DataServiceException(403, @"insufficient parameters provided for the authentication.");
        string username = values[0] ?? string.Empty;
        string password = values[1] ?? string.Empty;
        string database = Api2.Implementation.Authenticator.ConstructDatabaseId(context.Request.Url.AbsoluteUri);

        if (!Api2.Implementation.Authenticator.Authenticate(username, password, database))
        {
            AddResponseHeader(context, @"TenForce-RAuth", "DENIED");
            throw new DataServiceException(403, @"Incorrect authentication credentials.");
        }
    }

    /// <summary>
    /// <para>Add the specific HTTP Header to the Response of the provided HttpContext.</para>
    /// </summary>
    /// <param name="context">The HttpContext object holding the HTTP Response.</param>
    /// <param name="header">The name of the header to add to the response.</param>
    /// <param name="value">The value of the header.</param>
    private static void AddResponseHeader(HttpContext context, string header, string value)
    {
        if (!context.Request.ServerVariables[@"SERVER_SOFTWARE"].Contains(@"Microsoft-IIS/7."))
            context.Response.AddHeader(header, value);
        else
            context.Response.Headers.Add(header, value);
    }

    #endregion
}
}    

Anyone who could point out what the problem is?

  • 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-22T12:54:58+00:00Added an answer on May 22, 2026 at 12:54 pm

    More of a comment than an answer (how to just add comment?) but having what appears to be a very similar setup, I can say that OnStartProcessingRequest() with the signiture you have above is getting called here. These others virtual also work for me.

    protected override ent CreateDataSource() {}
    protected override void HandleException(HandleExceptionArgs args) {}
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

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.