我正在创建一个ASP.NET核心Web API,并希望集成Microsoft Azure AD身份验证服务。在编译时,一切正常。但是,当我访问要返回质询的路由时,会得到一个空URI异常(下面的Stacktrace)。我尝试在我的ConfigureServices中指定质询URI:
public void ConfigureServices(IServiceCollection services)
{
services.AddDbContext<AppContext>(options => options.UseSqlite("Data Source=app.db"));
// Identity/Authorization Services
services.AddAuthentication(sharedOptions =>
{
sharedOptions.DefaultScheme = CookieAuthenticationDefaults.AuthenticationScheme;
sharedOptions.DefaultChallengeScheme = OpenIdConnectDefaults.AuthenticationScheme;
})
.AddAzureAD(options =>
{
options.ClientId = Configuration["Authentication:Microsoft:ApplicationId"];
options.ClientSecret = Configuration["Authentication:Microsoft:Password"];
options.SignedOutCallbackPath = "/api/v1/accounts/signout";
})
.AddCookie(options => options.AccessDeniedPath = "/api/v1/accounts/accessdenied");
services.AddTransient<IRecognitionService, RecognitionService>();
services.AddTransient<IProfileService, ProfileService>();
services.AddMvc();
}
Run Code Online (Sandbox Code Playgroud)
我也尝试过更改路由中的重定向URI:
[Route("api/v1/accounts/[action]")]
public class AccountController : Controller
{
public async Task<IActionResult> AccessDenied()
{
return Unauthorized();
}
[HttpGet]
public async Task<IActionResult> Signin()
{
return Challenge(new AuthenticationProperties {RedirectUri = "/accessdenied"},
AzureADDefaults.AuthenticationScheme);
} …Run Code Online (Sandbox Code Playgroud) 我已经有了一个可以使用 MVC 和 API 控制器的 ASP.NET Core 2.2 实现,并且我正在整理一个集成测试项目来涵盖已经手动测试过的所有内容 - 主要是基本的 CRUD。除了使用 PostAsync 来 POST 数据的测试之外,一切正常。这些测试总是收到 500 内部服务器错误作为客户端的响应,但我一辈子都无法弄清楚原因。哎呀!
TestServer 设置是一种非常标准的方法,我在许多不同的博客和文章中都看到过。TestStartup 类扩展了标准 Startup,并覆盖配置以使用带有种子数据的内存数据库。然后,我的测试夹具基础使用 TestStartup 创建服务器和客户端,并有一个我知道效果很好的方法来提取防伪令牌并将其添加到表单和标头中。验证 CRUD 所有其他方面的所有其他测试都正常工作,证明可以通过 MVC 和 API 调用检索种子数据。
最终因 500 内部服务器错误而失败的 POST 调用确实会进入控制器和后续存储库,这很好。有了所有这些方面,我还没有能够看到 500 的来源。
[Fact]
public async void CreatePost_ShouldReturnViewWithNewlyCreatedLabelData()
{
// Arrange
var formData = await EnsureAntiForgeryTokenOnForm(new Dictionary<string, string>()
{
{ "Name", TestDataGraph.Labels.LabelNew.Name },
{ "WebsiteUrl", TestDataGraph.Labels.LabelNew.WebsiteUrl }
});
// Act
var response = await Client.PostAsync("/labels/create", new FormUrlEncodedContent(formData));
// Assert
Assert.Equal(HttpStatusCode.Found, response.StatusCode);
Assert.Equal("/Labels", response.Headers.Location.ToString());
}
Run Code Online (Sandbox Code Playgroud)
这是 Xunit …
c# post integration-testing internal-server-error asp.net-core