2011-12-13 86 views
5

目前我正在開發一個使用DotNetOpenAuth CTP版本的OAuth2授權服務器。我的授權服務器在asp.net MVC3中,它基於庫提供的示例。一切工作正常,直到應用程序達到用戶授權客戶端的地步。PrepareResponse()。AsActionResult()拋出不受支持的異常DotNetOpenAuth CTP

有我的OAuth控制器內部的作用,這需要授權過程的關懷,是非常相似的樣品中的等效操作:

[Authorize, HttpPost, ValidateAntiForgeryToken] 
    public ActionResult AuthorizeResponse(bool isApproved) 
    { 
     var pendingRequest = this.authorizationServer.ReadAuthorizationRequest(); 

     if (pendingRequest == null) 
     { 
      throw new HttpException((int)HttpStatusCode.BadRequest, "Missing authorization request."); 
     } 

     IDirectedProtocolMessage response; 
     if (isApproved) 
     { 
      var client = MvcApplication.DataContext.Clients.First(c => c.ClientIdentifier == pendingRequest.ClientIdentifier); 
      client.ClientAuthorizations.Add(
       new ClientAuthorization 
       { 
        Scope = OAuthUtilities.JoinScopes(pendingRequest.Scope), 
        User = MvcApplication.LoggedInUser, 
        CreatedOn = DateTime.UtcNow, 
       }); 
      MvcApplication.DataContext.SaveChanges(); 
      response = this.authorizationServer.PrepareApproveAuthorizationRequest(pendingRequest, User.Identity.Name); 
     } 
     else 
     { 
      response = this.authorizationServer.PrepareRejectAuthorizationRequest(pendingRequest); 
     } 

     return this.authorizationServer.Channel.PrepareResponse(response).AsActionResult(); 
    } 

每次程序運行到這一行:

this.authorizationServer.Channel.PrepareResponse(response).AsActionResult(); 

系統拋出一個我沒有研究過的異常。以下例外: LINQ to Entities僅支持無參數的構造函數和初始值設定項。

堆棧跟蹤:http://pastebin.com/TibCax2t

我已經從不同的樣品做的唯一的事情是,我使用實體框架的代碼第一個方法,一個我認爲樣品是使用其自動生成實體設計師完成的。

預先感謝您。

+0

你知道了嗎?我遇到了同樣的問題。 – fuzz 2012-04-21 06:04:38

回答

1

看起來您的ICryptoKeyStore實現可能試圖直接存儲CryptoKey,但它不是與Entity框架兼容的類(由於沒有公共默認構造函數)。相反,在CryptoKey中定義自己的實體類以存儲數據,並且您的ICryptoKeyStore負責在兩種數據類型之間進行持久性和檢索。

+0

呃!謝謝安德魯... :) – Jammer 2012-11-26 18:45:57

5

如果你從這個例子開始,安德魯談論的問題停留在DatabaseKeyNonceStore.cs。唯一的例外是由一個在這兩種方法提出:

public CryptoKey GetKey(string bucket, string handle) { 
     // It is critical that this lookup be case-sensitive, which can only be configured at the database. 
     var matches = from key in MvcApplication.DataContext.SymmetricCryptoKeys 
         where key.Bucket == bucket && key.Handle == handle 
         select new CryptoKey(key.Secret, key.ExpiresUtc.AsUtc()); 

     return matches.FirstOrDefault(); 
    } 

    public IEnumerable<KeyValuePair<string, CryptoKey>> GetKeys(string bucket) { 
     return from key in MvcApplication.DataContext.SymmetricCryptoKeys 
       where key.Bucket == bucket 
       orderby key.ExpiresUtc descending 
       select new KeyValuePair<string, CryptoKey>(key.Handle, new CryptoKey(key.Secret, key.ExpiresUtc.AsUtc())); 
    } 

我已經解決移動初始化查詢外:

public CryptoKey GetKey(string bucket, string handle) { 
     // It is critical that this lookup be case-sensitive, which can only be configured at the database. 
     var matches = from key in db.SymmetricCryptoKeys 
         where key.Bucket == bucket && key.Handle == handle 
         select key; 

     var match = matches.FirstOrDefault(); 

     CryptoKey ck = new CryptoKey(match.Secret, match.ExpiresUtc.AsUtc()); 

     return ck; 
    } 

    public IEnumerable<KeyValuePair<string, CryptoKey>> GetKeys(string bucket) { 
     var matches = from key in db.SymmetricCryptoKeys 
       where key.Bucket == bucket 
       orderby key.ExpiresUtc descending 
       select key; 

     List<KeyValuePair<string, CryptoKey>> en = new List<KeyValuePair<string, CryptoKey>>(); 

     foreach (var key in matches) 
      en.Add(new KeyValuePair<string, CryptoKey>(key.Handle, new CryptoKey(key.Secret, key.ExpiresUtc.AsUtc()))); 

     return en.AsEnumerable<KeyValuePair<string,CryptoKey>>(); 
    } 

我不知道,這是最好的方式,但它作品!

+0

只是...只是...謝謝你! – joshcomley 2012-10-23 23:49:34