【问题标题】:PrepareResponse().AsActionResult() throws unsupported exception DotNetOpenAuth CTPPrepareResponse().AsActionResult() 抛出不受支持的异常 DotNetOpenAuth CTP
【发布时间】:2011-12-13 22:33:38
【问题描述】:

目前我正在使用 DotNetOpenAuth CTP 版本开发 OAuth2 授权服务器。我的授权服务器在 asp.net MVC3 中,它基于库提供的示例。在应用程序到达用户授权消费者客户端之前,一切正常。

我的 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

我所做的与示例唯一不同的是我使用实体框架的代码优先方法,我认为示例是使用自动生成实体的设计器完成的。

提前谢谢你。

【问题讨论】:

  • 你发现了吗?我遇到了同样的问题。

标签: asp.net-mvc-3 dotnetopenauth oauth-2.0


【解决方案1】:

如果您从示例开始,Andrew 所说的问题仍然存在于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>>();
    }

我不确定这是不是最好的方法,但它确实有效!

【讨论】:

    【解决方案2】:

    看起来您的ICryptoKeyStore 实现可能正在尝试直接存储CryptoKey,但它不是与实体框架兼容的类(由于没有公共默认构造函数)。相反,定义您自己的实体类来存储CryptoKey 中的数据,而您的ICryptoKeyStore 负责在两种数据类型之间转换以进行持久化和检索。

    【讨论】:

      猜你喜欢
      • 2012-02-04
      • 2020-04-01
      • 2014-02-27
      • 2020-09-02
      • 1970-01-01
      • 1970-01-01
      • 2020-06-22
      • 1970-01-01
      相关资源
      最近更新 更多