我试图按照本指南中的.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'是如何工作的,我只是复制了代码中的值。你有没有尝试第一或第二个代码? –