首页 文章

取消外部登录时的例外情况

提问于
浏览
0

(不要害怕这个大的描述,我只是试图具体,以便回答者变得更容易)

我正在使用ASP.Net Core 2.1构建一个Web应用程序,其中包含外部登录 . 但是当外部(facebook)登录被取消并且facebook重定向到源应用程序时发生内部服务器错误 . 这意味着,您单击Facebook外部登录按钮,然后单击"Not Now"按钮取消它 . Facebook重定向回您的应用程序( https://localhost:port/signin-facebook?... );瞧 - 瞧 - 异常 .

处理请求时发生未处理的异常 . 例外:access_denied;描述=权限错误未知位置异常:处理远程登录时遇到错误 . Microsoft.AspNetCore.Authentication.RemoteAuthenticationHandler.HandleRequestAsync()

enter image description here

当来自Startup.cs类的Asp.net Core系统准备facebook身份验证时,Facebook身份验证提供程序将自动生成'https://.../signin-facebook'路由,如Microsoft docs和Github / aspnet中所述:

如果我在没有任何查询字符串的情况下直接点击“https://localhost:port/signin-facebook”,则会显示以下异常:OAuth状态丢失或无效 .
enter image description here

但预期的行为是 - 它将被重定向到默认登录页面 .

这是startup.cs片段:

services.ConfigureApplicationCookie(options => options.LoginPath = "/Account/LogIn");
services
    .AddAuthentication(o => o.DefaultAuthenticateScheme = CookieAuthenticationDefaults.AuthenticationScheme)
    .AddCookie(options =>
    {
        options.LoginPath = "/Account/Login";
        options.LogoutPath = "/Account/Logout";
    });

services.AddAuthentication()
    .AddFacebook(o =>
    {   
        o.AppId = Configuration.GetValue<string>("Facebook:AppId");
        o.AppSecret = Configuration.GetValue<string>("Facebook:AppSecret");
    });

我配置了一个自定义回调路径(如microsoft doc中所述),但同样的例外 .

发生什么了?出了什么问题?什么是解决方案?

仅供参考,我不是从应用程序访问数据库并使用 .UseModel() 使用默认 IdentityDbContext 和使用 HttpContext.SigninAsync 进行cookie身份验证 . 完成外部登录而不是取消时,一切都很好 .

1 回答

  • 1

    根据Asp.Net Core Github回购,Asp.net核心团队是working on it now . 目前,开发人员正在优雅地使用 OnRemoteFailure 事件并执行所需的操作 .

    Startup.cs:

    services.AddAuthentication()
        .AddFacebook(o =>
        {   
            o.AppId = Configuration.GetValue<string>("Facebook:AppId");
            o.AppSecret = Configuration.GetValue<string>("Facebook:AppSecret");
            o.Events.OnRemoteFailure = (context) =>
            {
                context.Response.Redirect("/account/login");
                context.HandleResponse();
                return System.Threading.Tasks.Task.FromResult(0);
            };
        });
    

相关问题