C# .net核心集成测试:未从测试中调用API控制器操作。如何在测试项目中模拟OPIND connect身份验证?
启动cs文件.net核心:(在创建测试服务器时也会调用此文件) 控制器操作装饰为:C# .net核心集成测试:未从测试中调用API控制器操作。如何在测试项目中模拟OPIND connect身份验证?,c#,.net,.net-core,openid,openid-connect,C#,.net,.net Core,Openid,Openid Connect,启动cs文件.net核心:(在创建测试服务器时也会调用此文件) 控制器操作装饰为: [Route("api/[controller]")] public class SearchController : BaseController [Route("TestMethod")] [ActionName("TestMethod")] [HttpGet] public async Task<EmptyResult> Test()
[Route("api/[controller]")]
public class SearchController : BaseController
[Route("TestMethod")]
[ActionName("TestMethod")]
[HttpGet]
public async Task<EmptyResult> Test()
获取状态为302且SearchController操作未获取
打电话。所有依赖项都使用启动配置解决
文件
有什么想法吗?您可以检查
var responseString=wait response.content.ReadAsStringAsync()的内容
查看内容是什么
我假设这是登录页面,这是因为您需要授权
首先,尝试删除下面的代码进行尝试
services.AddMvc(config =>
{
//var policy = new AuthorizationPolicyBuilder()
.RequireAuthenticatedUser()
.Build();
//config.Filters.Add(new Microsoft.AspNetCore.Mvc.Authorization.AuthorizeFilter(policy));
config.Filters.Add(typeof(ExceptionFilter));
});
对于身份验证
,您需要模拟登录过程,这里有一个标识链接,您可以尝试为AAD实现自己的登录
为什么要更改客户端的基本地址?IIRC您不需要这样做。@juunas如果我将其保留为localhost302表示重定向,那么仍然存在相同的问题,您能检查一下它希望将您的请求从“Location”响应头重定向到哪里吗?你也可以分享你的创业课程吗?@PéterCsajtai添加了创业课程file@TaoZhou是的,您有一个配置的Authorization action filter属性,它会尝试将您发出的每个请求重定向到您的登录页面,这就是为什么您得到302我们没有这样的登录页面,因为它是基于令牌的azure广告authentication@SagarK使用aad实现登录过程以获取令牌。知道如何实现假开放id连接身份验证以进行测试吗
public DemoTest()
{
// Set up server configuration
var configuration = new ConfigurationBuilder()
.AddJsonFile(@"appsettings.json")
.Build();
// Create builder
var builder = new WebHostBuilder()
.UseStartup<Startup>()
.UseConfiguration(configuration);
// Create test server
var server = new TestServer(builder);
// Create database context
this._context = server.Host.Services.GetService(typeof(DBContext)) as DBContext;
// Create client to query server endpoints
this._client = server.CreateClient();
_client.BaseAddress = new Uri("https://localhost:44316/");
}
[Fact]
public async Task Test()
{
try
{
var response = await this._client.GetAsync("/api/Search/TestMethod");
response.EnsureSuccessStatusCode();
var responseString = await response.Content.ReadAsStringAsync();
//Assert.False(result != null);
}
catch (Exception ex)
{
throw;
}
}
services.AddMvc(config =>
{
//var policy = new AuthorizationPolicyBuilder()
.RequireAuthenticatedUser()
.Build();
//config.Filters.Add(new Microsoft.AspNetCore.Mvc.Authorization.AuthorizeFilter(policy));
config.Filters.Add(typeof(ExceptionFilter));
});