【发布时间】:2018-03-07 09:43:09
【问题描述】:
我正在开发一个 web api core 2.0 项目。
我需要支持两种授权类型:jwt 和 basic。
在我的 ConfigureServices 方法中,我添加了以下代码:
services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme).AddJwtBearer((options) =>
{
options.Authority = $"...";
options.Audience = "...";
});
services.AddAuthentication(BasicAuthenticationDefaults.AuthenticationScheme)
.AddBasicAuthentication(credentials =>
Task.FromResult(
credentials.username == "username"
&& credentials.password == "password"));
在我的 Configure 方法中,我添加了以下代码:
app.UseAuthentication();
app.UseMvc();
最后我在我的控制器上添加了 AuthorizeAttribute:
[Authorize]
public class MioController : Controller
{ ... }
实际上只使用 ConfigureServices 上指定的最后一次身份验证。
如何支持这两种身份验证类型? 谢谢
注意:我正在使用这个 NuGet 包进行基本身份验证Bazinga.AspNetCore.Authentication.Basic。
【问题讨论】:
-
Authorize 属性具有相关属性:
[Authorize(AuthenticationSchemes = JwtBearerDefaults.AuthenticationScheme)]
标签: c# authentication asp.net-core