2017-09-13 24 views
0

我試圖按照本指南中的.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類已將AuthenticationSchemeAutomaticAuthenticate屬性從.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

回答

0

你不能這樣做是爲了工作方式了。 AuthenticationScheme只能在Startup.cs中設置。像你這樣分支是不可能的。 而不是測試計劃,您應該使用您的應用程序中使用的計劃。

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

    public TestAuthenticationOptions 
    { 
     //nothing to do here. 
    } 
} 

在啓動你希望:

services.AddAuthentication("<ProductionScheme>"); 

也許這可以幫助你:

https://github.com/aspnet/Announcements/issues/262

0

您可以設置配置選項在startup.cs

services.AddAuthentication(config => { 
    this.AuthenticationScheme = "TestAuthenticationMiddleware"; 
    this.AutomaticAuthenticate = true; 
}); 

我在.net core 2.0中找到的大多數代碼都使用這種方式來配置AuthenticationOptions。但是如果你需要創建自己的AuthenticationOptions類並使用。您可以制定自己的計劃,這意味着您還需要製作AuthenticationHandler<TestAuthenticationOptions>課程(讓我們稱之爲TestAuthenticationHandler)並將其添加爲計劃。

services.AddAuthentication(config => { 
    this.AuthenticationScheme = "TestAuthenticationMiddleware"; 
    this.AutomaticAuthenticate = true; 
}) 
.AddScheme<TestAuthenticationHandler, TestAuthenticationOptions>("TestAuthenticationMiddleware", o => {}); 
+0

我收到此錯誤信息: AuthenticationOptions'不包含定義‘AutomaticAuthenticate’,沒有擴展方法‘AutomaticAuthenticate’接受一個類型的第一個參數‘AuthenticationOptions’可以發現 當我輸入驗證碼: services.AddAuthentication(選項=> { options.AuthenticationScheme = 「TestAuthenticationMiddleware」; options.AutomaticAuthenticate = TRUE; }); –

+0

當我輸入你建議的內容時: 'Startup'沒有包含'AuthenticationScheme'的定義,也沒有可以找到接受類型'Startup'的第一個參數的擴展方法'AuthenticationScheme' –

+0

好像它一旦你設置'AutomaticAuthenticate'爲true,則需要更多的設置。我不完全確定'AutomaticAuthenticate'是如何工作的,我只是複製了代碼中的值。你有沒有嘗試第一或第二個代碼? –

相關問題