Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/287.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# 为什么OAuth在使用ASP.NET Identity Core时失败?_C#_Asp.net Core_Asp.net Identity - Fatal编程技术网

C# 为什么OAuth在使用ASP.NET Identity Core时失败?

C# 为什么OAuth在使用ASP.NET Identity Core时失败?,c#,asp.net-core,asp.net-identity,C#,Asp.net Core,Asp.net Identity,我有一个ASP.NET Core 2.x项目,配置如下: services .AddAuthentication(options => options.DefaultScheme = CookieAuthenticaitonDefaults.AuthenticationScheme) .AddCookie(CookieAuthenticationDefaults.AuthenticationScheme) .AddFacebook(ConfigureFacebook); 可以

我有一个ASP.NET Core 2.x项目,配置如下:

services
  .AddAuthentication(options => options.DefaultScheme = CookieAuthenticaitonDefaults.AuthenticationScheme)
  .AddCookie(CookieAuthenticationDefaults.AuthenticationScheme)
  .AddFacebook(ConfigureFacebook);
可以预见的是,当我从我的一个行动中打电话时:

返回质询(新的AuthenticationProperties{RedirectUri=“/test”},“Facebook”)

。。。然后,我浏览了Facebook OAuth序列。当我找到返回应用程序的方法时,
HttpContext.User.Identity
将填充相关详细信息:

  • User.Identity.Name
    -Facebook用户名
  • User.Identity.AuthenticationType
    -字符串
    “Facebook”
  • User.Identity.IsAuthenticated
    -
    true
这一切都很好,正如预期的那样。但是,如果我在应用程序配置中添加以下内容

services.AddIdentity<MyUserType, MyRoleType>()
  .AddEntityFrameworkStores<MyDbContext>();
services.AddIdentity()
.AddEntityFrameworkStores();
突然,OAuth流以
User.Identity
匿名结束,没有任何其他更改。如果深入到IdentityServiceCollectionExtensions.cs,我们会发现:

options.DefaultAuthenticateScheme= IdentityConstants.ApplicationScheme;options.DefaultChallengeScheme= IdentityConstants.ApplicationScheme;options.DefaultSignenscheme= IdentityConstants.ExternalScheme

除其他外


这是怎么回事?为什么标识会干扰Cookie进程,从OAuth提供程序返回用户的正确方法是什么?

要组合OAuth和Asp.Net核心标识,您需要使用
CookieAuthenticationDefaults.AuthenticationScheme
配置
facebookOptions.Signenscheme

请尝试以下代码:

services
    .AddAuthentication(options => options.DefaultScheme = CookieAuthenticationDefaults.AuthenticationScheme)
    .AddCookie(CookieAuthenticationDefaults.AuthenticationScheme)
    .AddFacebook(facebookOptions =>
    {
        facebookOptions.SignInScheme = CookieAuthenticationDefaults.AuthenticationScheme;
        facebookOptions.AppId = "xx";
        facebookOptions.AppSecret = "xxx";
    });

结合ASP.NET Identity和OAuth时,需要考虑以下几点:

配置服务: 添加
AddCookie(CookieAuthenticationDefaults.AuthenticationScheme)
不再是必需的,因为Identity添加了自己的cookie处理程序

将外部用户作为ClaimsPrincipal获取: 如果要在
HttpContext.user
下填充外部用户,请执行以下操作:

.AddFacebook(options => {
    options.SignInScheme = IdentityConstants.ApplicationScheme;
})
在被重定向到质询的
AuthenticationProperties
中的
RedirectUri
后,将填充您的
HttpContext.User

将外部用户获取为
ExternalLoginInfo
: 如果您需要了解有关用户的信息,例如:

  • 他们来自哪个供应商
  • 他们在提供商上的唯一密钥是什么
  • 您的服务应按如下方式配置:

    services.AddAuthentication()
        .AddFacebook(options =>
        {
            options.AppId = "";
            options.AppSecret = "";
        });
    
    services.AddIdentity<IdentityUser, IdentityRole>()
        .AddEntityFrameworkStores<MyDbContext>();
    
    在返回操作中,使用
    getexternallogininfosync
    获取用户的外部详细信息:

    public async Task<IActionResult> LoginCallback() {
        var loginInfo = await SignInManager.GetExternalLoginInfoAsync();
        // This object will tell you everything you need to know about the incoming user.
    }
    
    public异步任务LoginCallback(){
    var loginInfo=await SignInManager.getexternallogininfosync();
    //此对象将告诉您需要了解的有关传入用户的所有信息。
    }
    
    不幸的是,这不起作用。从Facebook(或其他提供商)返回时,
    用户.Identity
    是匿名的。要添加到我的其他备注中,FacebookOptions.Signnscheme会返回到AuthenticationOptions.DefaultSignnscheme和AuthenticationOptions.DefaultScheme,因此添加该行是不相关的。
    public IActionResult LoginExternal() {
        var props = SignInManager.ConfigureExternalAuthenticationProperties("Facebook", "/");
        return Challenge(props, "Facebook");
    }
    
    public async Task<IActionResult> LoginCallback() {
        var loginInfo = await SignInManager.GetExternalLoginInfoAsync();
        // This object will tell you everything you need to know about the incoming user.
    }