2014-02-07 2 views
1

Hmac 인증을위한 사용자 정의 Owin 미들웨어 AuthenticationHandler를 작성하고 있습니다.IOwinRequest Body에 액세스/처리하는 방법

오버 라이딩 된 'AuthenticateCoreAsync'메소드에서 요청의 http 헤더와 '본문'(내용)을 읽어야합니다.

나는 등

를 모든 작업, 클레임/인증 티켓을 가지고하지만 액세스하거나 IOwinRequest.Body으로 뭔가를 할 때마다 내 WebApi 컨트롤러 휴식. 그것들은 올바르게 호출되지만 모든 API 메소드 인수는 'null'입니다. 마치 본문 내용이 어딘가에서 과정에서 없어지는 것처럼 말입니다.

IOwinRequest.Body를 메모리 스트림에 복사하여 사용하려고 시도했지만 IOwinRequest.Body.Seek (0) 등을 사용하여 다시 설정해 보았습니다.

IOwinRequest의 본문 작업 방법에 대한 좋은 예제/문서를 찾을 수 없습니다.

누구나 올바른 방향으로 나를 가리킬 수 있습니까?

아래 코드에서 댓글을 남길 때 작동합니다. 실제로 인증을하기 위해 (그리고 Body에서 읽는) 줄들의 주석을 제거하면 webapi 메소드는 모든 null 인수를받습니다.

internal class HMacAuthenticationMiddleware : AuthenticationMiddleware<HMacAuthenticationOptions> 
{ 
    private readonly ILogger logger; 

    public HMacAuthenticationMiddleware(OwinMiddleware next, IAppBuilder app, HMacAuthenticationOptions options) 
     : base(next, options) 
    { 
     this.logger = app.CreateLogger<HMacAuthenticationMiddleware>(); 
    } 

    protected override AuthenticationHandler<HMacAuthenticationOptions> CreateHandler() 
    { 
     return new HMacAuthenticationHandler(this.logger); 
    } 
} 

HMacAuthenticationHandler :

protected override async Task<AuthenticationTicket> AuthenticateCoreAsync() 
    { 
     string[] apiKeys = null; 
     AuthenticationTicket authenticationTicket = null; 
     if (this.Request.Headers.TryGetValue(Configuration.ApiKeyHeader, out apiKeys)) 
     { 
      ////var bufferedStream = await CreateStreamBuffer(Request); 
      //var memStream = new MemoryStream(); 
      //await new StreamContent(Request.Body).CopyToAsync(memStream); 
      //Request.Body = memStream; 
      //memStream.Seek(0, SeekOrigin.Begin); 
      //var httpMessage = Request.CreateRequestMessage(new StreamContent(memStream)); 
      //var authHandler = new HMacInnerAuthenticationHandler(httpMessage, Options); 
      //var isAuthenticated = await authHandler.IsAuthenticated(); 
      //Request.Body.Seek(0, SeekOrigin.Begin); 

      var isAuthenticated = true; 
      if (isAuthenticated) 
      { 
       var userName = this.Options.UsernameLookup.GetByApiKey(apiKeys.First()); 
       var identity = new ClaimsIdentity(
        new[] 
         { 
          new Claim(ClaimTypes.NameIdentifier, apiKeys.First(), XmlSchemaString, this.Options.AuthenticationType), 
          new Claim(ClaimTypes.Name, userName, XmlSchemaString, this.Options.AuthenticationType) 
         }, 
        this.Options.AuthenticationType, 
        ClaimsIdentity.DefaultNameClaimType, 
        ClaimsIdentity.DefaultRoleClaimType); 

       authenticationTicket = new AuthenticationTicket(identity, null); 
      } 
     } 
     return authenticationTicket; 
    } 
+0

어쩌면 당신이 뭔가 잘못하고있는 중 ... 난 그냥 간단한 시나리오를 시도하고 잘 작동 ... 코드를 공유 할 수 있습니까? –

+0

일부 코드가 업데이트되었습니다. 나는 'Body'와 가짜 인증을 사용하지 않는다. 내 webapi 메서드가 올바르게 바인딩됩니다. 'Body'를 사용하면 모두 null입니다. 지금 여러 시간 동안 노력 해왔다. 모든 종류의 변형. 나는 여기서 명백한 것을 놓치고있을 것입니다 ... –

+0

'복잡한'버퍼 복사 코드에 대해서. 글쎄, 그 순간에 내가 가진 것입니다. 나는 몸에서 독서로 시작했다. 그게 작동하지 않는다면, 나는 당신의 방법 (단지 copystream)을 시도했다. 또한 작동하지 않았다. 이 코드가 katana 저장소에서 발견되었지만, 다시. 작동하지 않습니다. 항상 Body로 무엇인가 할 때, 내 webapi 메소드가 더 이상 인수를받지 못하게된다. –

답변

2

좋아, 그래서 aspnetweb 스택 소스에 주위를 파고 후. 특히 여기에 : https://aspnetwebstack.codeplex.com/SourceControl/latest#src/System.Web.Http.Owin/WebApiAppBuilderExtensions.cs

WebApi를 owin에 등록하는 방법을 사용하지 않은 것으로 나타났습니다.

나는 여전히 "GlobalConfiguration.Configure (WebApiConfig.Register);"를 사용하고있었습니다. " 다른 미들웨어 함께 Startup.cs 내부

 var httpConfig = new HttpConfiguration(); 
     WebApiConfig.Register(httpConfig); 
     app.UseWebApi(httpConfig); 

: Global.asax.cs

내부는 I로 대체 그 제거.

이제 WebApi가 파이프 라인의 올바른 위치에 등록되어 모든 것이 작동합니다.

관련 문제