2011-12-13 85 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