Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/271.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# ASP.NET Core中带Facebook登录的AuthenticateTasync存在问题_C#_Authentication_Asp.net Core - Fatal编程技术网

C# ASP.NET Core中带Facebook登录的AuthenticateTasync存在问题

C# ASP.NET Core中带Facebook登录的AuthenticateTasync存在问题,c#,authentication,asp.net-core,C#,Authentication,Asp.net Core,在我的ASP.NET核心web应用程序中启用Facebook身份验证时遇到问题。我使用的是ASP.NET核心身份验证,但不是身份验证。auth在启动时的配置如下: 服务 .AddAuthentication(选项=> { options.DefaultScheme=CookieAuthenticationDefaults.AuthenticationScheme; }) .AddCookie(CookieAuthenticationDefaults.AuthenticationScheme) .

在我的ASP.NET核心web应用程序中启用Facebook身份验证时遇到问题。我使用的是ASP.NET核心身份验证,但不是身份验证。auth在启动时的配置如下:

服务
.AddAuthentication(选项=>
{
options.DefaultScheme=CookieAuthenticationDefaults.AuthenticationScheme;
})
.AddCookie(CookieAuthenticationDefaults.AuthenticationScheme)
.AddFacebook(选项=>
{
options.ClientId=“ClientId”;
options.ClientSecret=“secret”;
options.CallbackPath=“/signinfacebookcallback”;
});
如代码所示,我想使用cookie auth,但也允许人们使用Facebook登录。一旦他们成功登录,我想设置身份验证cookie

为了展示挑战,我有以下行动:

[HttpGet]
[路线(“signinfacebook”)]
公共行动结果签名Facebook()
{
返回质询(FacebookDefaults.AuthenticationScheme);
}
这会将用户重定向到Facebook登录屏幕。登录后,用户将被重定向到配置中指定的URL:

[HttpGet]
[路线(“signinfacebookcallback”)]
公共异步任务SignInFacebookCallback()
{
var result=await HttpContext.AuthenticateAsync();
如果(!result.successed)返回重定向(“/login/”);
...
}
调试代码时,
result.succeed
返回false,并且
AuthenticationResult
对象不包含有关为什么
succeed
为false的详细信息

我验证了应用程序id和密码是否正确


这里可能有什么问题?

OpenID Connect中间件中的
回调路径是用于OpenID Connect协议的身份验证流的内部路径。在Identity provider将用户重定向到应用程序中的该url后,Middware将处理令牌验证、令牌解码、交换令牌并最终填充用户原则,该过程将在控制器参与之前启动

由于
CallbackPath
是内部的,将由OpenID Connect中间件自动处理,因此您无需在意,请确保在facebook允许的重定向url中注册回调,并让中间件处理回调。如果您想在身份验证后将用户重定向到特定的路由/页面,将url放到
AuthenticationProperties

if (!User.Identity.IsAuthenticated)
{
    return Challenge(new AuthenticationProperties() { RedirectUri = "/home/Privacy" } ,FacebookDefaults.AuthenticationScheme);
}
您应该删除应用程序中的回调路径路由(
signinfacebookcallback

更新

如果您想访问数据库并管理本地用户,可以使用中间件中的内置事件,对于
AddFacebook
中间件,您可以使用
OnTicketReceived
添加access数据库、管理用户并向用户原则添加声明:

.AddFacebook(options =>
{
    options.ClientId = "xxxxxxxxxxxxx";
    options.ClientSecret = "xxxxxxxxxxxxxxxxxxxx";
    options.CallbackPath = "/signinfacebookcallback";
    options.Events = new OAuthEvents
    {
        OnTicketReceived = ctx =>
        {

            //query the database to get the role
            var db = ctx.HttpContext.RequestServices.GetRequiredService<YourDbContext>();
            // add claims
            var claims = new List<Claim>
            {
                new Claim(ClaimTypes.Role, "Admin")
            };
            var appIdentity = new ClaimsIdentity(claims);

            ctx.Principal.AddIdentity(appIdentity);

            return Task.CompletedTask;
        },
    };
});
.AddFacebook(选项=>
{
options.ClientId=“xxxxxxxxxxxx”;
options.ClientSecret=“xxxxxxxxxxxxxxxxx”;
options.CallbackPath=“/signinfacebookcallback”;
options.Events=新的OAuthEvents
{
OnTicketReceived=ctx=>
{
//查询数据库以获取角色
var db=ctx.HttpContext.RequestServices.GetRequiredService();
//添加索赔
var索赔=新列表
{
新索赔(ClaimTypes.Role,“Admin”)
};
var appIdentity=新的索赔实体(索赔);
ctx.委托人.额外性(appIdentity);
返回Task.CompletedTask;
},
};
});

OpenID Connect中间件中的
回调路径是用于OpenID Connect协议的身份验证流的内部路径。在Identity provider将用户重定向到应用程序中的该url后,Middware将处理令牌验证、令牌解码、交换令牌并最终填充用户原则,该过程将在控制器参与之前启动

由于
CallbackPath
是内部的,将由OpenID Connect中间件自动处理,因此您无需在意,请确保在facebook允许的重定向url中注册回调,并让中间件处理回调。如果您想在身份验证后将用户重定向到特定的路由/页面,将url放到
AuthenticationProperties

if (!User.Identity.IsAuthenticated)
{
    return Challenge(new AuthenticationProperties() { RedirectUri = "/home/Privacy" } ,FacebookDefaults.AuthenticationScheme);
}
您应该删除应用程序中的回调路径路由(
signinfacebookcallback

更新

如果您想访问数据库并管理本地用户,可以使用中间件中的内置事件,对于
AddFacebook
中间件,您可以使用
OnTicketReceived
添加access数据库、管理用户并向用户原则添加声明:

.AddFacebook(options =>
{
    options.ClientId = "xxxxxxxxxxxxx";
    options.ClientSecret = "xxxxxxxxxxxxxxxxxxxx";
    options.CallbackPath = "/signinfacebookcallback";
    options.Events = new OAuthEvents
    {
        OnTicketReceived = ctx =>
        {

            //query the database to get the role
            var db = ctx.HttpContext.RequestServices.GetRequiredService<YourDbContext>();
            // add claims
            var claims = new List<Claim>
            {
                new Claim(ClaimTypes.Role, "Admin")
            };
            var appIdentity = new ClaimsIdentity(claims);

            ctx.Principal.AddIdentity(appIdentity);

            return Task.CompletedTask;
        },
    };
});
.AddFacebook(选项=>
{
options.ClientId=“xxxxxxxxxxxx”;
options.ClientSecret=“xxxxxxxxxxxxxxxxx”;
options.CallbackPath=“/signinfacebookcallback”;
options.Events=新的OAuthEvents
{
OnTicketReceived=ctx=>
{
//查询数据库以获取角色
var db=ctx.HttpContext.RequestServices.GetRequiredService();
//添加索赔
var索赔=新列表
{
新索赔(ClaimTypes.Role,“Admin”)
};
var appIdentity=新的索赔实体(索赔);
ctx.委托人.额外性(appIdentity);
返回Task.CompletedTask;
},
};
});

Hmm,通常不实现回调路径处理程序。。它应该由Facebook身份验证处理程序的RemoteAuthenticate处理。这是因为您使用的是Identity吗?或者你有没有一个例子,说明如何在没有标识的情况下实现它?没有,没有使用标识。至少OpenID连接身份验证处理程序自己处理回调。嗯,通常不实现回调路径处理程序。。它应该由Facebook身份验证处理程序的RemoteAuthenticate处理。这是bec吗