2011-12-13 33 views
5

Hiện tại tôi đang phát triển một máy chủ ủy quyền OAuth2 sử dụng phiên bản CTP DotNetOpenAuth. Máy chủ ủy quyền của tôi nằm trong asp.net MVC3 và dựa trên mẫu được cung cấp bởi thư viện. Mọi thứ hoạt động tốt cho đến khi ứng dụng đạt tới điểm mà người dùng ủy quyền cho ứng dụng khách hàng.PrepareResponse() AsActionResult() ném ngoại lệ không được hỗ trợ DotNetOpenAuth CTP

Có một hành động bên trong điều khiển OAuth của tôi mà sẽ chăm sóc của quá trình cấp phép, và rất giống với hành động tương đương trong mẫu:

[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(); 
    } 

Mỗi lần chương trình đạt được dòng này:

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

Hệ thống ném một ngoại lệ mà tôi đã nghiên cứu không thành công. Ngoại lệ là như sau: Chỉ các nhà xây dựng và trình khởi tạo không có tham số mới được hỗ trợ trong LINQ to Entities.

Các stack trace: http://pastebin.com/TibCax2t

Điều duy nhất tôi đã làm khác so với mẫu là tôi đã sử dụng mã tiếp cận đầu tiên thực thể khuôn khổ của, một tôi nghĩ rằng mẫu được thực hiện bằng một nhà thiết kế mà autogenerated các thực thể.

Cảm ơn bạn trước.

+0

Bạn đã xác định điều này? Tôi nhận được cùng một vấn đề. – fuzz

Trả lời

1

Có vẻ như việc triển khai ICryptoKeyStore của bạn có thể đang cố gắng lưu trữ trực tiếp CryptoKey, nhưng không phải là lớp tương thích với khung Entity (do không có hàm dựng mặc định công khai). Thay vào đó, hãy xác định lớp thực thể của riêng bạn để lưu trữ dữ liệu trong CryptoKeyICryptoKeyStore của bạn chịu trách nhiệm chuyển đổi giữa hai loại dữ liệu để duy trì và truy xuất.

+0

Duh! Cảm ơn Andrew ... :) – Jammer

5

Nếu bạn bắt đầu từ ví dụ này, vấn đề mà Andrew đang nói về việc lưu trú trong DatabaseKeyNonceStore.cs. Trường hợp ngoại lệ được nâng lên bởi một trên hai phương pháp:

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())); 
    } 

tôi đã giải quyết di chuyển khởi tạo bên ngoài của truy vấn:

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>>(); 
    } 

Tôi không chắc chắn rằng đây là cách tốt nhất, nhưng nó công trinh!

+0

Chỉ ... chỉ ... cảm ơn bạn !! – joshcomley

Các vấn đề liên quan