ASP.NET MVC 5 (VS2013 final): Facebook login with

2019-02-12 17:44发布

I installed the VS2013 final bits that were released yesterday, and I'm trying to get an example working where I enable an external Facebook login. My first question:

In this controller code (which I did not touch and left as is from the sample template):

    //
    // GET: /Account/ExternalLoginCallback
    [AllowAnonymous]
    public async Task<ActionResult> ExternalLoginCallback(string returnUrl)
    {
        var loginInfo = await AuthenticationManager.GetExternalLoginInfoAsync();
        if (loginInfo == null)
        {
            return RedirectToAction("Login");
        }

I set a breakpoint on the line await AuthenticationManager.GetExternalLoginInfoAsync(). The code returns (after I do my Facebook login), and "loginInfo" is null. In the UI, the login page continues to be displayed, with no change. How can I debug this? I was trying to find the code inside of GetExternalLoginInfoAsync() but according to this thread:

Where is Microsoft.AspNet.Identity.Owin.AuthenticationManager in Asp.Net Identity RTM version?

AuthenticationManager is now gone. (This does not seem to be the case per above.)

My second question: Is anyone else able to get the sample working with Facebook login with no changes to the ASP.NET MVC5 sample code (other than uncommenting app.UseFacebookAuthentication and filling in your FB app details)? (You'll have to configure an alias host with Facebook, such as "localtest.me" and configure it with IIS express.)

Thanks for any help...

-Ben

8条回答
beautiful°
2楼-- · 2019-02-12 18:18

Ok, this was silly of me. First off, I started debugging using Fiddler, and saw that Facebook was sending back this:

WWW-Authenticate: OAuth "Facebook Platform" "invalid_request" "The request is invalid because the app secret is the same as the client token"

This message was inaccurate, but searching for it, I found another thread where a user specified that resetting the app secret solved the problem. (I hadn't used this app in a long time so that may have been related.) So I took those steps, but still saw the problem. But the message in Fiddler changed:

WWW-Authenticate: OAuth "Facebook Platform" "invalid_request" "The request is invalid because the app is configured as a desktop app"

Indeed, the app was misconfigured in Facebook. I set it to be a web app, and it works perfectly now! Hopefully this can help others, as this was not clear until firing up Fiddler.

查看更多
Root(大扎)
3楼-- · 2019-02-12 18:22

I had this same problem because I hadn't enabled the API within google. Fiddler was returning an error status of access_denied.

I had created the project and got the client id and secret data, but you also need to go to the developer console and enable the Google+ api.

查看更多
疯言疯语
4楼-- · 2019-02-12 18:25

Finally I found the answer for OWIN authentication fails. I really want to know what is happening with my ASP.NET MVC 5 application when i am trying to add Facebook authentication. After I tried every solution out there on Internet.

Just change your Facebook App with new Facebook Developer App or change its app Secret from Facebook developer site.

查看更多
老娘就宠你
5楼-- · 2019-02-12 18:26

I had this problem while authenticating to Google (OpenID , not OAuth). So there really was nothing to mess up, but sure enough it stopped working after some simple code changes around session variables. Using fiddler I determined that Google was indeed returning valid information.

Someone smarter than me figured out my issue and describes the fix here.

ASP.NET_SessionId + OWIN Cookies do not send to browser

All I had to do was add Session["salt"] = "salt"; to the Account/Login controller, and viola it magically started working perfectly. Not sure if this is your exact issue, but this was mine.

查看更多
孤傲高冷的网名
6楼-- · 2019-02-12 18:29

I started getting this in the latest VS 2013.3 template and realized the authentication wasn't playing nice with FormsAuthentication that I unnecessarily ported from one of my other projects. Here's what I did to fix it:

added <system.web><authentication mode="None" />...

added <system.webServer><modules><remove name="FormsAuthentication" /></modules>...

查看更多
Fickle 薄情
7楼-- · 2019-02-12 18:40

For me this failed because I assumed that facebook always has access to birthday property, but this is not the case:

// facebook doesn't guarantee that this value exists always,
// thus we can't really add this claim to our identity object, if it's null.
var birthday = context.User.Value<string>("birthday");
if (birthday != null)
{
    context.Identity.AddClaim(new Claim("Birthdate",
         context.User.Value<string>("birthday")));
}

This extends to pretty much everything:

1) some of the users don't have last name (allowed)

2) some of the users don't have email in fb (allowed)

3) some of the users don't give out birthday,

You have to take care of everything.

查看更多
登录 后发表回答