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

标签 iis httprequest dotnetopenauth

我的 OAuth Controller 中的授权方法有一个自定义授权过滤器。当授权过滤器注意到用户已登录时,它会将当前的 OAuth 请求填充到 session 中,并将它们发送出去以登录。

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

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

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

但是,当请求在 session 中找到并恢复时,ReadAuthorizationRequest 失败。错误消息不是很有帮助:“值不在预期范围内。”

这是堆栈跟踪:

[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它看起来完全没问题: header 、URI 等。我不知道是什么原因造成的。

有人知道为什么会这样吗?或者,如果您对在用户进行身份验证时存储 oauth 请求有其他建议,我愿意接受。

最佳答案

事实证明 HttpRequestWrapper 上的 ServerVariables 在调用时抛出异常(现在从堆栈跟踪中可以明显看出)。我相信这是因为请求尚未命中 Controller 操作,因为它被过滤器拦截了。我猜是在 Controller 操作处理请求时设置了 ServerVariables?

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

关于iis - 传入 HttpRequestWrapper 时来自 DotNetOpenAuth 的 ReadAuthorizationRequest 的错误消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16202921/

相关文章:

dotnetopenauth - 尝试使用asp.net中的DotNetOpenAuth登录名制作最简单的OpenID

IIS 重写规则未设置 HTTP_COOKIE

python - 回滚事务SQL

asp.net - 如何保护已经使用 Forms 身份验证的 ASP .NET 网站?

c# - 如何将计量使用情况发送到 AppDirect

javascript - RxJS + Node.js Http 请求

asp.net - 如何检查 Asp.Net 请求管道?

c# - 间歇性 SecureChannelFailure 错误

android - 从 android 发送 oauth token 导致 KeyNotFoundException

asp.net-mvc - 在 ASP.NET MVC 4 中使用 OAuthWebSecurity 时获取 Google 用户名?