提问者:小点点

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


我正在处理的代码已经工作,需要从Core 1.0迁移到Core 2.0,需要使用和迁移服务认证中的字段。我如何在Core 2.0中使用字段?(我也查看了微软的迁移文档,但什么也没找到。)https://learn . Microsoft . com/en-us/aspnet/core/migration/1x至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.NETCore 2.0

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

共1个答案

匿名用户

< code>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");
});

但是,这实际上不是必需的,因为这些是默认设置的。请参阅源代码中的代码片段:

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

看起来即使在以前版本的ASP.NETCore中也没有必要,但是您的代码可以正常工作,因为您只是替换了默认值(没有name)。如果您真的不想请求name,您可以使用facebookOptions. Field.Remove("name")