2012-01-06 45 views
0

我很難試圖讓DotNetOpenAuth ctp 4.0工作。這裏的情況: 我有一個資源服務器,就像OAuth2示例中的資源服務器一樣,但是我使用WCF Web Api預覽版6,所以我編寫了一個可擴展性點,負責驗證執行操作請求的客戶機是已經授權這樣做,爲了完成這個,方法ResourceServer.VerifyAccess被調用。這種方法拋出一個空的異常,我還沒有找到原因。DotNetOpenAuth ctp 4.0 ResourceServer.VerifyAccess()方法拋出null異常

這是我寫的我的操作處理程序:

protected override HttpRequestMessage OnHandle(HttpRequestMessage input) 
    { 
     var principal = VerifyOAuth2(input); 
     if(principal == null) 
     { 
      throw new HttpResponseException(new HttpResponseMessage 
               { 
                StatusCode = HttpStatusCode.Unauthorized, 
                Content = new StringContent("Invalid Access Token") 
               }); 
     } 

     var roles = _authorizationAttribute.Roles.Split(new[] {" "}, StringSplitOptions.RemoveEmptyEntries); 
     if(!roles.Any(role => principal.IsInRole(role))) 
     { 
      throw new HttpResponseException(new HttpResponseMessage 
      { 
       StatusCode = HttpStatusCode.Forbidden, 
       Content = new StringContent("User has not permission to access this resource") 
      }); 
     } 

     return input; 
    } 

    private static IPrincipal VerifyOAuth2(HttpRequestMessage request) 
    { 
     var headers = request.Headers; 
     var headersCollection = new WebHeaderCollection(); 
     foreach (var header in headers) 
     { 
      headersCollection.Add(header.Key, header.Value.ToString()); 
     } 

     using (var signing = MvcApplication.CreateAuthorizationServerSigningServiceProvider()) 
     { 
      using (var encrypting = MvcApplication.CreateResourceServerEncryptionServiceProvider()) 
      { 
       var resourceServer = new ResourceServer(new StandardAccessTokenAnalyzer(signing, encrypting)); 

       IPrincipal result; 
       var httpRequestInfo = new HttpRequestInfo(request.Method.ToString(), request.RequestUri, 
                  request.RequestUri.AbsoluteUri, headersCollection, request.Content.ReadAsStreamAsync().Result);//Since I dont have an HttpResourceInfo Object I need to build one from my request, using an overloaded method. 
       var error = resourceServer.VerifyAccess(httpRequestInfo, out result); //here is where the exception is thrown. 
       // TODO: return the prepared error code. 
       return error != null ? null : result; 
      } 
     } 

我不知道如果這個代碼可以幫助,但如果沒有的話,你能告訴我什麼時候這個方法拋出一個空引用例外??也許這會幫助我一點!先謝謝你。

+0

問題..在這裏你正在做的分裂和通過新的[] {」「}你有偶然得到一個錯誤..不使用看到它這樣做的方式 – MethodMan 2012-01-06 21:20:29

+0

沒有,異常在之前拋出,在VerifyOAuth2()方法中 – Daniel 2012-01-06 21:24:10

+0

IPrincipal是來自.net框架的接口。和輸入是HttpRequestMessage的一個目的是從WCF網絡API – Daniel 2012-01-06 21:35:35

回答

1

NullReferenceException的堆棧跟蹤將會很有幫助。

的代稱,你試過從WCF獲得HttpRequestMessageProperty並傳遞一個到HttpRequestInfo構造的OAuthAuthorizationManager做樣品的方式?

+0

是的,這解決了問題! – Daniel 2012-01-11 18:50:46