C# Owin login results in identity=null on producti

2019-04-15 17:15发布

问题:

I've got an asp.net MVC 5 web project which is running fine on my development system. But for some reason, the login using Microsoft Owin with Facebook stops working as soon as I deploy the solution on my production system.

The callback always retrieves ....error=access_denied as parameter and I tracked it back to the fact that owin returns null for my identity. Any clue whats going on here?

UPDATE

I implemented log4net in my Owin code and was able to dive deeper:

Response status code does not indicate success: 400 (Bad Request).
Stack trace:        
at System.Net.Http.HttpResponseMessage.EnsureSuccessStatusCode()
at Microsoft.Owin.Security.Facebook.FacebookAuthenticationHandler<AuthenticateCoreAsync>d__0.MoveNext()

Please not that I have already modified the facebook app to match the production urls, responses etc.

    private void ConfigureAuth(IAppBuilder app)
    {
        var cookieOptions = new CookieAuthenticationOptions
        {
            AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
            LoginPath = new PathString("/Account/Login")
        };

        app.UseCookieAuthentication(cookieOptions);
        app.UseExternalSignInCookie(DefaultAuthenticationTypes.ExternalCookie); // Passive

        app.SetDefaultSignInAsAuthenticationType(cookieOptions.AuthenticationType);
        app.UseFacebookAuthentication(new FacebookAuthenticationOptions
        {
            AppSecret = ConfigurationManager.AppSettings["FacebookAppSecret"],
            AppId = ConfigurationManager.AppSettings["FacebookAppId"],
            Provider = new FacebookAuthenticationProvider()
            {
                OnAuthenticated = (context) =>
                {
                    context.Identity.AddClaim(new Claim(
                        IdentityUtility.ExtendedClaimTypes.IdentityProvider,
                        "Facebook"));
                    return Task.FromResult(0);
                },
                OnReturnEndpoint = (context) =>
                {
                    if(context.Identity == null)
                        throw new Exception(context.Response.StatusCode.ToString());
                    return Task.FromResult(0);
                }
            }
        });
    }

Regards, Martin