【发布时间】:2017-10-03 16:31:39
【问题描述】:
我有使用 ASP.NET Core 开发的 Web API,我需要能够为同一服务使用基本和不记名身份验证方案。 由于某种原因,它不起作用:它始终将呼叫视为承载呼叫。 这是我的代码:
这是我在控制器中的属性:
[Authorize(ActiveAuthenticationSchemes = "Basic,Bearer")]
[ResponseCache(NoStore = true, Duration = 0, VaryByHeader = "Authorization")]
这是我的 startup.cs:
这部分用于基本认证:
app.UseBasicAuthentication(new BasicAuthenticationOptions
{
AutomaticAuthenticate = false,
AutomaticChallenge = false,
Realm = "test",
Events = new BasicAuthenticationEvents
{
OnValidateCredentials = context =>
{
if (svc.IsValidCredential(context.Username, context.Password))
{
var claims = new[]
{
new Claim(ClaimTypes.NameIdentifier, context.Username),
new Claim(ClaimTypes.Name, context.Username)
};
context.Ticket = new AuthenticationTicket(
new ClaimsPrincipal(
new ClaimsIdentity(claims, context.Options.AuthenticationScheme)),
new AuthenticationProperties(),
context.Options.AuthenticationScheme);
}
return Task.FromResult<object>(null);
}
}
});
还有这段用于Bearer认证的代码:
app.UseAPIKeyAuthentication(new BearerApiKeyOptions
{
AuthenticationScheme = BearerApiKeySchema,
AutomaticAuthenticate = false
});
【问题讨论】:
-
目前没有回复。没有人知道如何使用多重身份验证?
标签: asp.net-mvc authentication asp.net-core basic-authentication bearer-token