2017-11-18 164 views
0

我正在使用已運行且需要從Core 1.0遷移到Core 2.0並需要在服務身份驗證中使用和遷移字段的代碼。如何在Core 2.0中使用字段? (我在微軟審查遷移文件太多,但不能發現任何東西。)https://docs.microsoft.com/en-us/aspnet/core/migration/1x-to-2x/identity-2xASP.NET Core 1.0升級到ConfigureServices中的ASP.NET Core 2.0升級身份驗證 - 如何在Core 2.0中使用字段?

public void ConfigureServices(IServiceCollection services) 

,我遇到了下列麻煩:(怎樣添加在覈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核2.0

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

回答

1

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」)

相關問題