【发布时间】:2022-01-17 17:12:24
【问题描述】:
我已关注these instructions 以使身份验证与 ASP.net 核心集成测试一起工作。它几乎可以工作了。我的 HttpContext.User 正确填充了声明。但是我对具有[Authorize] 属性的Web API 控制器的请求仍然失败,除非我指定方案:[Authorize(AuthenticationSchemes = "Test")] 有效。当我在builder.ConfigureTestServices() 方法中添加“Test”作为默认方案时,我很小心地指定它,那么为什么不接受它作为默认方案呢?
builder.ConfigureTestServices(services =>
{
/* Overriding policies and adding Test Scheme defined in TestAuthHandler */
services.AddMvc(options =>
{
var policy = new AuthorizationPolicyBuilder(new[] { "Test" })
.RequireAuthenticatedUser()
.AddAuthenticationSchemes("Test")
.Build();
options.Filters.Add(new AuthorizeFilter(policy));
});
/* Adding Default Authentication schemes
This is enough to prevent unauthorized client (if you wish to have that kind of tests) */
services.AddAuthentication(options =>
{
options.DefaultAuthenticateScheme = "Test";
options.DefaultChallengeScheme = "Test";
options.DefaultScheme = "Test";
options.DefaultForbidScheme = "Test";
options.DefaultChallengeScheme = "Test";
options.DefaultSignInScheme = "Test";
options.DefaultSignOutScheme = "Test";
})
/* Here we're adding TestAuthentication Scheme with Handler that will Authenticate our client based on Claims */
.AddScheme<AuthenticationSchemeOptions, TestAuthHandler>("Test", options => { });
});
【问题讨论】:
标签: c# asp.net-core authentication