传入HttpRequestWrapper时,来自DotNetOpenAuth的ReadAuthorizationRequest的错误消息

时间:2013-04-24 21:57:16

标签: iis httprequest dotnetopenauth

我的OAuth控制器中的Authorize方法有自定义授权过滤器。当授权过滤器记录用户被记录时,它会将当前OAuth请求填充到会话中,然后将其发送出去以登录。

登录后,在我的/ OAuth / Authorize端点中,我检查该请求是否在会话中,而不是立即失败,因为当前请求没有附加授权请求。然后,我用该请求对象调用授权服务器。

我的授权操作中的代码如下所示:

    [AcceptVerbs(HttpVerbs.Get | HttpVerbs.Post)]
    [ExternalAppAuthorizeAttribute]
    public ActionResult Authorize() {
        Object requestObject = this.HttpContext.Session["AuthRequest"];
        HttpRequestBase request = null;
        if ((requestObject != null) && (requestObject.GetType() == typeof(HttpRequestWrapper)))
        {
            request = (HttpRequestWrapper)requestObject;
            this.HttpContext.Session.Remove("AuthRequest");
        }
        EndUserAuthorizationRequest pendingRequest = null;
        if (request != null)
        {
            pendingRequest = this.authorizationServer.ReadAuthorizationRequest(request);
        } else
        {
            pendingRequest = this.authorizationServer.ReadAuthorizationRequest();
        }
        if (pendingRequest == null) {
            throw new HttpException((int)HttpStatusCode.BadRequest, "Missing authorization request.");
        }
但是,当在会话中找到并恢复请求时,

ReadAuthorizationRequest失败。错误消息不是很有用:“值不在预期范围内。”

这是stacktrace:

[ArgumentException: Value does not fall within the expected range.]
   System.Runtime.InteropServices.Marshal.ThrowExceptionForHRInternal(Int32 errorCode, IntPtr errorInfo) +0
   System.Runtime.InteropServices.Marshal.ThrowExceptionForHR(Int32 errorCode) +10
   System.Web.Util.Misc.ThrowIfFailedHr(Int32 hresult) +9
   System.Web.Hosting.IIS7WorkerRequest.GetServerVariableInternal(String name) +36
   System.Web.Hosting.IIS7WorkerRequest.GetServerVariable(String name) +49
   System.Web.HttpRequest.AddServerVariableToCollection(String name) +22
   System.Web.HttpRequest.FillInServerVariablesCollection() +85
   System.Web.HttpServerVarsCollection.Populate() +36
   System.Web.HttpServerVarsCollection.Get(String name) +42
   System.Collections.Specialized.NameValueCollection.get_Item(String name) +10
   DotNetOpenAuth.Messaging.MessagingUtilities.GetPublicFacingUrl(HttpRequestBase request, NameValueCollection serverVariables) +61
   DotNetOpenAuth.Messaging.MessagingUtilities.GetPublicFacingUrl(HttpRequestBase request) +43
   DotNetOpenAuth.Messaging.Channel.ReadFromRequestCore(HttpRequestBase request) +69

我已经在飞行中检查了我的请求,其中的所有内容used by oauth看起来都很合适:标题,URI等等。我不知道可能导致此问题的原因。

有谁知道为什么会这样?或者,如果您在用户进行身份验证时有其他建议来存储oauth请求,我会向他们开放。

1 个答案:

答案 0 :(得分:4)

事实证明,HttpRequestWrapper上的ServerVariables在调用时抛出异常(现在从堆栈跟踪中可以看出这一点)。我相信这是因为请求没有达到控制器操作,因为它被过滤器拦截了。我想在控制器操作处理请求时,ServerVariables会被设置吗?

我通过创建一个实现HttpRequestBase的新类来解决这个问题。我传递了我存储的oauth请求和实际请求,并从ott请求返回了HttpRequestBase中的所有属性,除了ServerVariables,我从当前请求返回。