首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >DataService :未调用OnStartProcessingRequest

DataService :未调用OnStartProcessingRequest
EN

Stack Overflow用户
提问于 2011-02-14 18:11:00
回答 1查看 1.4K关注 0票数 0

我已经创建了一个WCF,对于这个服务,我需要通过使用DataService头进行一些自定义身份验证。因此,我编写了一些特殊的函数来验证此信息,或者在用户不允许查看此信息时将其抛出403页面。

为了方便起见,我尝试重写OnStartProcessingRequest,以便在每次调用时都执行此检查,但由于某些原因,我的代码/WCF服务从未调用过此函数:

这是WCF服务的代码:

代码语言:javascript
复制
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
}
}    

谁能指出问题出在哪里?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2011-05-20 08:15:42

与其说是回答,不如说是评论(如何添加评论?)但是有了一个非常相似的设置,我可以说带有上面签名的OnStartProcessingRequest()在这里被调用了。这些人对我来说也是虚拟的。

代码语言:javascript
复制
protected override ent CreateDataSource() {}
protected override void HandleException(HandleExceptionArgs args) {}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/4990908

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档