我試圖按照本指南中的.NET核心運行測試時,讓用戶的登錄2如何在.NET Core 2中設置AuthenticationScheme和AutomaticAuthenticate?
http://geeklearning.io/how-to-deal-with-identity-when-testing-an-asp-net-core-application/
它說我應該用於測試目的,這樣的配置認證中間件:
public class TestAuthenticationOptions : AuthenticationOptions
{
public virtual ClaimsIdentity Identity { get; } = new ClaimsIdentity(new Claim[]
{
new Claim("http://schemas.xmlsoap.org/ws/2005/05/identity/claims/nameidentifier", Guid.NewGuid().ToString()),
new Claim("http://schemas.microsoft.com/identity/claims/tenantid", "test"),
new Claim("http://schemas.microsoft.com/identity/claims/objectidentifier", Guid.NewGuid().ToString()),
new Claim("http://schemas.xmlsoap.org/ws/2005/05/identity/claims/givenname", "test"),
new Claim("http://schemas.xmlsoap.org/ws/2005/05/identity/claims/surname", "test"),
new Claim("http://schemas.xmlsoap.org/ws/2005/05/identity/claims/upn", "test"),
}, "test");
public TestAuthenticationOptions()
{
this.AuthenticationScheme = "TestAuthenticationMiddleware";
this.AutomaticAuthenticate = true;
}
}
由於AuthenticationOptions類已將AuthenticationScheme
和AutomaticAuthenticate
屬性從.NET Core 2 cf中的AuthenticationOptions實例中刪除,因此這不起作用。 https://docs.microsoft.com/en-us/aspnet/core/migration/1x-to-2x/identity-2x#setting-default-authentication-schemes
所以,現在我不知道怎麼去登錄在測試中的.NET核心2
我收到此錯誤信息: AuthenticationOptions'不包含定義‘AutomaticAuthenticate’,沒有擴展方法‘AutomaticAuthenticate’接受一個類型的第一個參數‘AuthenticationOptions’可以發現 當我輸入驗證碼: services.AddAuthentication(選項=> { options.AuthenticationScheme = 「TestAuthenticationMiddleware」; options.AutomaticAuthenticate = TRUE; }); –
當我輸入你建議的內容時: 'Startup'沒有包含'AuthenticationScheme'的定義,也沒有可以找到接受類型'Startup'的第一個參數的擴展方法'AuthenticationScheme' –
好像它一旦你設置'AutomaticAuthenticate'爲true,則需要更多的設置。我不完全確定'AutomaticAuthenticate'是如何工作的,我只是複製了代碼中的值。你有沒有嘗試第一或第二個代碼? –