2017-09-13 101 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'是如何工作的,我只是复制了代码中的值。你有没有尝试第一或第二个代码? –

相关问题