2011-02-14 4 views
0

WCF DataService를 만들었으며이 서비스에 대해 HTTP 헤더를 사용하여 사용자 지정 인증이 필요합니다. 그래서이 정보를 확인하거나 사용자가 볼 수 없으면 403 페이지를 던져주는 특수 기능을 작성했습니다.DataService : OnStartProcessingRequest가 호출되지 않았습니다.

이 자신을 위해 쉽게 만들려면,이 검사에게 모든 호출을 수행 할 수 OnStartProcessingRequest 덮어 쓰기를 시도했지만 어떤 이유로이 기능은 내 코드/WCF 서비스에 의해 호출되지 않습니다 : S

이의 코드 WCF 서비스 :

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 
} 
}  

문제가 무엇인지 지적 할 수있는 사람은 누구입니까?

답변

0

답변보다 주석이 많습니다 (주석을 추가하는 방법은 무엇입니까?)하지만 매우 비슷한 설정 인 것 같습니다. 위의 서명과 함께 OnStartProcessingRequest()를 호출 할 수 있습니다. 가상의 다른 것들도 나를 위해 일합니다.

protected override ent CreateDataSource() {} 
protected override void HandleException(HandleExceptionArgs args) {}