C# ASP.NET Core 1.0升级到ASP.NET Core 2.0升级ConfigureServices中的身份验证-如何使用Core 2.0中的字段?
我正在使用的代码已经运行,需要从Core1.0迁移到Core2.0,并且需要使用和迁移服务身份验证中的字段。如何在Core 2.0中使用字段?(我也在Microsoft查看了迁移文档,但找不到任何内容。) 我遇到了以下问题:(如何在Core2.0中添加以下内容) 下面是我的代码 ASP.NET核心1.0C# ASP.NET Core 1.0升级到ASP.NET Core 2.0升级ConfigureServices中的身份验证-如何使用Core 2.0中的字段?,c#,asp.net-core,asp.net-core-2.0,C#,Asp.net Core,Asp.net Core 2.0,我正在使用的代码已经运行,需要从Core1.0迁移到Core2.0,并且需要使用和迁移服务身份验证中的字段。如何在Core 2.0中使用字段?(我也在Microsoft查看了迁移文档,但找不到任何内容。) 我遇到了以下问题:(如何在Core2.0中添加以下内容) 下面是我的代码 ASP.NET核心1.0 app.UseFacebookAuthentication(new FacebookOptions { AppId = Configuration["Authentication:Tes
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"];
});
字段
为只读,但您可以修改其内容。以您的示例为例,代码级迁移可能如下所示:
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.NET内核中也没有必要这样做,但是您的代码可以正常工作,因为您只是在替换默认值(没有name
)。如果确实不想请求名称
,可以使用facebook选项.Fields.Remove(“name”)
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");
// ...
}