调用Facebook/Google +时,ExternalLogin不会激活

Ped*_*nco 3 c# identity google-login facebook-login asp.net-core-2.1

我正在与Facebook和Google进行集成,以便在我的应用程序中使用此应用程序,但是当我点击按钮以便通过Facebook或Google登录时,我得到了302响应,这就是对Fiddler的响应.

在此输入图像描述

它通过命中ExternalLogin重定向到登录.

这是我的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();
Run Code Online (Sandbox Code Playgroud)

我也在使用Identity来创建用户.

我在AccountController上的ExternalLogin:

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

    return Challenge(props, provider);
}
Run Code Online (Sandbox Code Playgroud)

我的按钮用相应的外部登录:

<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>
Run Code Online (Sandbox Code Playgroud)

但是,当我点击这个"按钮"时,它不会在控制器中点击ExternalLogin并返回使用此URL登录:

https://开头本地主机:5000 /帐号/登录RETURNURL =%2FAccount%2FExternalLogin%3Fprovider%3DFacebook

我正在使用.NET Core 2.1

有什么想法解决这个问题?

Fel*_*ira 5

这会导致访问此路由的授权问题,您的AccountContoller使用属性[授权]?.如果是,则授权属性强制对该控制器的任何操作的任何请求将被重定向到登录.

要解决此问题,您只能将[授权]放在用户需要进行身份验证的操作中,或者在ExternalLogin操作中使用[AllowAnonymous].