ASP.NET Core 1.0 升级到 ASP.NET Core 2.0 升级 ConfigureServices 中的身份验证 - 如何使用 Core 2.0 中的字段?

ASP.NET Core 1.0 upgrade to ASP.NET Core 2.0 Upgrade Authentication in ConfigureServices - How do I use Fields in Core 2.0?

我正在使用已经运行并且需要从 Core 1.0 迁移到 Core 2.0 并且需要使用和迁移服务身份验证中的字段的代码。我如何使用 Core 2.0 中的字段? (我也查看了 Microsoft 的迁移文档,但找不到任何内容。)https://docs.microsoft.com/en-us/aspnet/core/migration/1x-to-2x/identity-2x

public void ConfigureServices(IServiceCollection services)

我遇到以下问题:(如何在 Core 2.0 中添加以下内容)

Fields = { "email", "last_name", "first_name" },

下面是我的代码。

ASP.NET 核心 1.0

app.UseFacebookAuthentication(new FacebookOptions
{
    AppId = Configuration["Authentication:Test:Facebook:AppId"],
    AppSecret = Configuration["Authentication:Test:Facebook:AppSecret"],
    Fields = { "email", "last_name", "first_name" },
});

需要迁移到 ASP.NET Core 2.0

services.AddAuthentication().AddFacebook(facebookOptions =>
{
    facebookOptions.AppId = Configuration["Authentication:Test:Facebook:AppId"];
    facebookOptions.AppSecret = Configuration["Authentication:Test:Facebook:AppSecret"];
});

Fields是只读的,但是你可以修改它的内容。以您的示例为例,代码级迁移可能如下所示:

services.AddAuthentication().AddFacebook(facebookOptions =>
{
    facebookOptions.AppId = Configuration["Authentication:Test:Facebook:AppId"];
    facebookOptions.AppSecret = Configuration["Authentication:Test:Facebook:AppSecret"];
    facebookOptions.Fields.Clear();
    facebookOptions.Fields.Add("email");
    facebookOptions.Fields.Add("last_name");
    facebookOptions.Fields.Add("first_name");
});

然而,这实际上不是必需的,因为这些是 set by default。查看源代码片段:

public FacebookOptions()
{
    // ...
    Fields.Add("name");
    Fields.Add("email");
    Fields.Add("first_name");
    Fields.Add("last_name");
    // ...
}

看起来即使在 ASP.NET 核心的 previous version 中也没有必要,但是您的代码可以正常工作,因为您只是替换默认值(没有 name) .如果实在不想请求name,可以使用facebookOptions.Fields.Remove(“name”).