Asp.Net MVC 5 Owin Twitter Auth throwing 401 Exception
Asked Answered
B

2

39

I have set up OAuth authentication using the Owin middleware based off the default project that gets created when starting a new Web project using "Individual accounts" authentication.

I've tweaked it a bit, but Facebook and Google are working great, however, when I click on the "twitter" button to issue the challenge, it's throwing a 500 http exception with the error message:

Response status code does not indicate success: 401 (Unauthorized).

This only comes up with Twitter. Here is my auth config:

// Just a configuration section that reads from the web.config
var configuration = new OwinCookieConfigurationSection("owinCookieConfiguration");
app.UseCookieAuthentication(GetCookieAuthenticationOptions(configuration));
app.UseExternalSignInCookie(DefaultAuthenticationTypes.ExternalCookie);

app.UseTwitterAuthentication(
    consumerKey: "XXX",
    consumerSecret: "XXX"
);

app.UseFacebookAuthentication(
    appId: "XXX",
    appSecret: "XXX"
);

app.UseGoogleAuthentication();

It's definitely a 500 error, not a 401 error as it indicates and I have confirmed it never makes it passed the original post challenge to Twitter. The stack trace appears to show Twitter is not returning a success code from the ObtainRequestToken call:

Stack Trace

Any ideas?

Breannabreanne answered 8/2, 2014 at 20:7 Comment(0)
M
70

You need to go into the Twitter Dev App and add a website address. I know it seem silly but this will solve your problem.

  1. https://apps.twitter.com/
  2. Go to the Settings Tab
  3. Set a CallBack URL to any website. Even if it is not real.

This should solve you problem.

Minded answered 9/2, 2014 at 4:33 Comment(3)
A optional field that's not optional. haha!Beverleybeverlie
FYI It takes about 5 minutes to take effect after you set the url. I wasted a day on this.Rheotropism
and make sure that "Enable callback locking" in apps.twitter settings page is false, otherwise your dynamic callback will be off anywayHeder
K
2

I solved my problem the way Patrick described 1 year ago. Today I ran into the same issue again but this time I found the issue was caused because of the date/time setting in my computer. Since the date/time in this computer is usually out-of-sync I had to go to "Change Date and Time settings" -> "Internet Time" tab and enable internet sync.

Kabul answered 12/4, 2015 at 15:42 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.