DotNetOpenAuth不使用MVC 5 RC

时间:2013-09-25 19:21:37

标签: c# asp.net asp.net-mvc oauth-2.0 dotnetopenauth

我一直在使用DotNetOpenAuth。首先我们使用5.0.0-alpha1,但我们切换到v4.0.30319,因为我们找不到导致我们问题的原因。

我们正在使用Visual Studio 2013中的MVC 5 RC在.NET 4.5.1 RC上构建一个C#Web API项目。我们实现了IAuthorizationServerHostINonceStoreICryptoKeyStore。< / p>

我们遇到的问题是以下情况:

public class TokensController : Controller
{
    private readonly AuthorizationServer authorizationServer = new AuthorizationServer(new MyAuthorizationServer());

    /// <summary>
    /// This action will handle all token requests. 
    /// </summary>
    /// <returns>The action result that will output the token response.</returns>
    [HttpPost]
    public ActionResult Index()
    {
        var outgoingWebResponse = this.authorizationServer.HandleTokenRequest(this.Request);
        return outgoingWebResponse.AsActionResult();
    }
}

return outgoingWebResponse.AsActionResult();一个源自DotNetOpenAuth.MessagingMessagingUtilities静态类的方法。 DotNetOpenAuth.Core(包含此代码)引用MVC 4.0,HttpResponseMessageActionResult类继承自ActionResult

这意味着当前版本的DotNetOpenAuth与MVC 5不兼容。编译并尝试运行它只会出现500个错误。

有没有人有任何想法如何轻松修复(或可能不是)?

我没注意到DotNetOpenAuth Nuget包写了我的5.0包。因此,重新安装软件包并再次添加assemblyBinding之后:

<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
  <dependentAssembly>
    <assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35" />
    <bindingRedirect oldVersion="1.0.0.0-4.0.0.0" newVersion="5.0.0.0" />
  </dependentAssembly>
</assemblyBinding>

这让我们更进一步。现在错误归结为:

  

尝试使用安全透明方法'DotNetOpenAuth.Messaging.MessagingUtilities.AsActionResult(DotNetOpenAuth.Messaging.OutgoingWebResponse)'来访问安全关键类型'System.Web.Mvc.ActionResult'失败。

5 个答案:

答案 0 :(得分:50)

修复可用。

安装NuGet包DotNetOpenAuth.Mvc5并将AsActionResult()的所有用途更改为AsActionResultMvc5()

答案 1 :(得分:6)

经过进一步的调试并在GitHub https://github.com/DotNetOpenAuth/DotNetOpenAuth/issues/307与DotNetOpenAuth的人们交谈后得出结论,MVC 5有一个新的安全模型。

绑定重定向因此是不够的。除此之外还有两种选择:

1)抓住DotNetOpenAuth源代码并从所有项目中删除[assembly:AllowPartiallyTrustedCallers]。重新编译和成员以禁用强名称验证sn -Vr *。此代码无法在Medium Trust环境中运行。

2)抓住DotNetOpenAuth源代码并针对MVC 5重新编译它。

根据对GitHub的讨论,最好的未来解决方案是将所有相关的MVC内容移到单独的程序集中。

答案 2 :(得分:3)

针对这种情况的解决方法(可以与当前的beta nuget包一起使用):

  • 创建一个ActionResult类包装HttpResponseMessage

    public class WrapperHttpResponseMessageResult : ActionResult
    {
        private readonly HttpResponseMessage _response;
    
        public WrapperHttpResponseMessageResult(HttpResponseMessage response)
        {
            _response = response;
        }
    
        public override void ExecuteResult(ControllerContext context)
        {
            HttpResponseBase responseContext = context.RequestContext.HttpContext.Response;
            responseContext.StatusCode = (int)_response.StatusCode;
            responseContext.StatusDescription = _response.ReasonPhrase;
            foreach (KeyValuePair<string, IEnumerable<string>> keyValuePair in (HttpHeaders)_response.Headers)
            {
                foreach (string str in keyValuePair.Value)
                    responseContext.AddHeader(keyValuePair.Key, str);
            }
    
            if (_response.Content != null)
            {
                _response.Content.CopyToAsync(responseContext.OutputStream).Wait();
            }
        }
    }
    
  • return outgoingWebResponse.AsActionResult();更改为new WrapperHttpResponseMessageResult(outgoingWebResponse);

代码WrapperHttpResponseMessageResult是从AsActionResult复制的,因此它们可以执行相同的功能。

答案 3 :(得分:2)

使用它来确保授权程序正确传递。

  public class MvcAuthorizer : WebAuthorizer
{
    public ActionResult BeginAuthorization()
    {
        return new MvcOAuthActionResult(this);
    }

    public new ActionResult BeginAuthorization(Uri callback)
    {
        this.Callback = callback;
        return new MvcOAuthActionResult(this);
    }
}

'然后正确检索

public class MvcOAuthActionResult : ActionResult
{
    private readonly WebAuthorizer webAuth;

    public MvcOAuthActionResult(WebAuthorizer webAuth)
    {
        this.webAuth = webAuth;
    }

    public override void ExecuteResult(ControllerContext context)
    {
        webAuth.PerformRedirect = authUrl =>
        {
            HttpContext.Current.Response.Redirect(authUrl);
        };

        Uri callback =
            webAuth.Callback == null ?
                HttpContext.Current.Request.Url :
                webAuth.Callback;

        webAuth.BeginAuthorization(callback);
    }
}

答案 4 :(得分:0)

如果将它与OutgoingWebresponse一起使用(没有升级dotnetOpenAuth但是mvc yes为5)。

添加此类(从langtu的响应中攻击):

 public class WrapperHttpResponseMessageResult : ActionResult
{
    private readonly OutgoingWebResponse _response;

    public WrapperHttpResponseMessageResult(OutgoingWebResponse response)
    {
        _response = response;
    }

    public override void ExecuteResult(ControllerContext context)
    {
        HttpResponseBase responseContext = context.RequestContext.HttpContext.Response;
        responseContext.StatusCode = (int)_response.Status;
        responseContext.StatusDescription = _response.Status.ToString();
        foreach (string key in _response.Headers.Keys)
        {
            responseContext.AddHeader(key, _response.Headers[key]);
        }

        if (_response.Body != null)
        {
            StreamWriter escritor = new StreamWriter(responseContext.OutputStream);
            escritor.WriteAsync(_response.Body).Wait();
        }
    }
}

然后替换:

return response.AsActionResult();

返回新的WrapperHttpResponseMessageResult(响应);