ExternalLogin not hiting when call to Facebook/Google+

Viewed 83

I'm making an integration with Facebook and Google to log with this application in mine, but when i click in the button supose to login with Facebook or Google i'm gotting 302 response, ckeched this responde on Fiddler.

enter image description here

It's redirect to Login withou hit ExternalLogin.

This is my Startup.cs

services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme)
    .AddFacebook(fOptions => {
        fOptions.AppId = "myAppId";
        fOptions.AppSecret = "myAppSecret";
        fOptions.SignInScheme = CookieAuthenticationDefaults.AuthenticationScheme; })
    .AddGoogle(gOptions => {
        gOptions.ClientId = "myClintId";
        gOptions.ClientSecret = "myClientSecret;
        gOptions.SignInScheme = CookieAuthenticationDefaults.AuthenticationScheme; })
    .AddCookie();

I'm using Identity to create users too.

My ExternalLogin on AccountController:

public IActionResult ExternalLogin(string provider, string returnUrl)
{
    var props = new AuthenticationProperties()
    {
        RedirectUri = Url.Action("ExternalLoginCallback"),
        Items =
        {
            { "returnUrl", returnUrl },
            { "scheme", provider },
        }
    };

    return Challenge(props, provider);
}

And my buttons to login with respective external:

<div class="col_full text-center nomargin" style="margin-bottom: 5px !important">
    <a asp-action="ExternalLogin" asp-route-provider="Facebook" asp-route-returnUrl="@ViewData["ReturnUrl"]" class="button button-rounded nomargin si-facebook si-colored" style="width: 100%" type="button">
        <i class="fa fa-facebook"></i> Facebook
    </a>
</div>

<div class="col_full text-center">
    <a asp-action="ExternalLogin" asp-route-provider="Google" asp-route-returnUrl="@ViewData["ReturnUrl"]" type="button" class="button button-rounded nomargin si-google si-colored" style="width: 100%">
        <i class="fa fa-google"></i> Google
    </a>
</div>

But when i click in this "button" it't not hiting ExternalLogin in controller and getting back to Login with this URL:

https://localhost:5000/Account/Login?ReturnUrl=%2FAccount%2FExternalLogin%3Fprovider%3DFacebook

I'm using .NET Core 2.1

some idea to solve this problem?

1 Answers

This appers a problem of authorization to access this route, your AccountContoller use an Attribute [Authorize]?. If yes, the Authorize Attribute force any request of any action of this controller will be redirected to Login.

To solve it, you can put [Authorize] only in actions that user need be authenticated or using [AllowAnonymous] in ExternalLogin Action.

Related