MVC 5 support

Oct 18, 2013 at 6:57 AM
I believe there is a problem with MVC 5 support and twittertolinq. I'm using a stock template from Visual Studio 2013 RTM.

Trying to follow your basic documentation when I call auth.BeginAuthorization(specialUri) I get an exception error:

An exception of type 'System.TypeAccessException' occurred in Costoda.dll but was not handled in user code

Additional information: Attempt by security transparent method 'LinqToTwitter.MvcAuthorizer.BeginAuthorization(System.Uri)' to access security critical type 'System.Web.Mvc.ActionResult' failed.

Searching around on the Google's I found discussion about the change in security with MVC 5.0 and DotNetOpenAuth which I have to assume you are using for oAuth. You might want to check out this thread:

https://github.com/DotNetOpenAuth/DotNetOpenAuth/issues/307/

Any chance you have already encountered this and have a work around?

-Costoda
Coordinator
Oct 18, 2013 at 7:19 AM
Edited Oct 18, 2013 at 7:21 AM
Hi Custoda,

Thanks for the link. It looks like a problem because my code is compiled against an earlier version of MVC. There are other version-related issues of having MVC support inside the LINQ to Twitter assembly and I'm thinking of pulling it out into a separate assembly. I haven't tested this yet, but I think you can put this code into an assembly built on the MVC 5 bits. First the Authorizer:
    public class MvcAuthorizer : WebAuthorizer
    {
        public ActionResult BeginAuthorization()
        {
            return new MvcOAuthActionResult(this);
        }

        public new ActionResult BeginAuthorization(Uri callback)
        {
            this.Callback = callback;
            return new MvcOAuthActionResult(this);
        }
    }
Notice that it returns MvcOAuthActionResult, so you'll need that too:
    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);
        }
    }
Right now, my code is in the LinqToTwitter namespace, so you'll probably want to put them into another namespace and fully qualify or alias them.

I'd be interested in hearing if this works for you since there will probably be others eager to get it working too.
@JoeMayo
Oct 18, 2013 at 5:02 PM

Joe,

I’ll add this to my code this morning and see what I come up with. Thanks for the quick response…

-Costoda

Oct 18, 2013 at 5:33 PM

Joe,

Yep – that worked! Thanks for the quick response.

-Costoda

Coordinator
Oct 18, 2013 at 5:53 PM
Thanks for the follow up.

@JoeMayo
Dec 24, 2013 at 5:25 AM
I had the exact same issue. This solution also worked for me.