C# asp.net core 2.1与windows身份验证和active directory的集成测试

C# asp.net core 2.1与windows身份验证和active directory的集成测试,c#,authentication,asp.net-core,active-directory,integration-testing,C#,Authentication,Asp.net Core,Active Directory,Integration Testing,多年来,我一直懒得对我的应用程序进行任何集成测试,现在我决定尝试一下。因此,现在我正在尝试对我的.net core 2.1应用程序进行集成测试,该应用程序在通过Visual Studio进行正常调试或部署到IIS服务器时工作正常,但当我在Visual Studio中运行集成测试以测试控制器时,我只得到以下错误:- 未指定身份验证方案,也未找到DefaultChallengeScheme 我正在角色中使用Authorize属性和Active Directory组。如果我从控制器中删除[Authoi

多年来,我一直懒得对我的应用程序进行任何集成测试,现在我决定尝试一下。因此,现在我正在尝试对我的.net core 2.1应用程序进行集成测试,该应用程序在通过Visual Studio进行正常调试或部署到IIS服务器时工作正常,但当我在Visual Studio中运行集成测试以测试控制器时,我只得到以下错误:-

未指定身份验证方案,也未找到DefaultChallengeScheme

我正在角色中使用Authorize属性和Active Directory组。如果我从控制器中删除[Authoize(Roles=“MYDOMAIN\SomeGroup”)],集成测试工作正常

起初我认为这是因为我没有在ConfigureServices方法中执行“AddAuthentication”,在Configure方法中没有执行“UseAuthentication”,但这似乎没有什么区别

以下是我的测试代码:-

public class TestMyController : IClassFixture<WebApplicationFactory<Startup>>
{
    private readonly WebApplicationFactory<Startup> _factory;
    private readonly HttpClient _client;

    public TestMyController(WebApplicationFactory<Startup> factory)
    {
        _factory = factory;
        _client = _factory.CreateClient();
    }        

    [Theory]
    [InlineData("api/somecontroller/someaction")]
    public async Task TestSomeControllerSomeAction(string url)
    {            
        var response = await _client.GetAsync(url);
        response.EnsureSuccessStatusCode();

        var responseBody = await _client.GetStringAsync(url);

        Assert.NotEmpty(responseBody);
    }
}
公共类TestMyController:IClassFixture
{
私有只读WebApplicationFactory\u工厂;
私有只读HttpClient\u客户端;
公共TestMyController(WebApplicationFactory)
{
_工厂=工厂;
_client=_factory.CreateClient();
}        
[理论]
[InlineData(“api/somecontroller/someaction”)]
公共异步任务TestSomeControllerSomeAction(字符串url)
{            
var response=await\u client.GetAsync(url);
response.EnsureSuccessStatusCode();
var responseBody=await_client.GetStringAsync(url);
Assert.NotEmpty(responseboody);
}
}
同样,此测试代码也可以工作,但前提是我从控制器中删除[Authorize]属性。在我所关注的Microsoft文档中,我找不到任何与此相关的内容,我正在努力寻找在授权属性中使用Active Directory“角色”时遇到此特殊问题的人-可能我做了一些错误或愚蠢的事情:-)


有人能帮忙或提供建议吗?

hey@padigan。你能解决这个问题吗?我遇到了同样的问题。@ramisharef不,还没有,我确实在github上找到了一个值得研究的repo,那里的一些代码可能会“启用”这个功能。看见