If I create an ASP.NET MVC 4 Web Application using the Internet Application template, it pre-installs all the components and configuration necessary to implement authentication using a range of OAuth and OpenID providers. Just adding my Twitter consumer key and secret to AuthConfig.cs activates authentication via Twitter.
However, it doesn’t seem to work as I would expect.
If I attempt to authenticate using Twitter, it invariably displays a Twitter sign-on page, regardless of whether I am already signed on to Twitter. It also logs me out of Twitter, so that I am forced to re-authenticate on my next browser visit to Twitter.
Is this a bug, or is some additional configuration necessary to transform this into the more usual seamless workflow (which is working correctly for other providers like Google)?
Thanks, in advance.
Tim
In case anyone else comes up against this issue, I’ll present here what I have discovered (together with a rather ugly workaround).
Using Fiddler to examine the HTTP traffic between
DotNetOpenAuthand Twitter, it is clear that the authentication request contains theforce_login=falsequerystring parameter, which suggests that DNOA is working correctly. However, if I use Fiddler’s scripting capability to modify the outbound request and remove theforce_loginparameter altogether, everything starts working correctly. I am guessing that Twitter’s implementation is at fault here, by treating the presence of anyforce_loginparameter as being equivalent toforce_login=true.Since I don’t imagine it will be possible to get Twitter to modify the behavior of their API, I have investigated whether there is a more accessible solution.
Looking at the DNOA code, I see that the
force_login=falseparameter is unconditionally added to the HTTP request by theDotNetOpenAuthWebConsumer.RequestAuthentication()method (and subsequently modified totruewhen required).So, the ideal solution would be for DNOA to offer finer-grained control over its authentication request parameters and for
TwitterClientto explicitly remove theforce_login=falseparameter. Unfortunately, the current DNOA codebase doesn’t directly support this, but it is possible to achieve the same effect by creating two custom classes.The first is a custom implementation of
IOAuthWebWorkerwhich is a direct copy of the originalDotNetOpenAuthWebConsumerclass, apart from a single-line change that initializes the redirect parameter dictionary as an empty dictionary:The other requirement is a custom
OAuthClientclass, based on the originalTwitterClientclass. Note that this requires a little more code than the originalTwitterClientclass, as it also needs to replicate a couple of methods that are internal to the DNOA base class or other utility classes:Having created these two custom classes, implementation simply entails registering an instance of the new
CustomTwitterClientclass in the MVC4AuthConfig.csfile: