我创建了一个新的ASP.NET Core Web应用程序项目在VS17中使用"Web应用程序(模型视图控制器)"模板和".NET Framework"+"ASP.NET Core 2"作为配置.身份验证配置设置为"个人用户帐户".
我有以下示例端点:
[Produces("application/json")]
[Route("api/price")]
[Authorize(Roles = "PriceViwer", AuthenticationSchemes = "Cookies,Bearer")]
public class PriceController : Controller
{
public IActionResult Get()
{
return Ok(new Dictionary<string, string> { {"Galleon/Pound",
"999.999" } );
}
}
"Cookies,Bearer"
是通过连接CookieAuthenticationDefaults.AuthenticationScheme
和JwtBearerDefaults.AuthenticationScheme
得到的.
目标是能够配置端点的授权,以便可以使用令牌和Cookie身份验证方法访问它.
以下是我在启动时的身份验证设置.反恐精英:
services.AddAuthentication()
.AddCookie(cfg => { cfg.SlidingExpiration = true;})
.AddJwtBearer(cfg => {
cfg.RequireHttpsMetadata = false;
cfg.SaveToken = true;
cfg.TokenValidationParameters = new TokenValidationParameters() {
ValidIssuer = Configuration["Tokens:Issuer"],
ValidAudience = Configuration["Tokens:Issuer"],
IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(Configuration["Tokens:Key"]))
};
});
So, when I try to access the endpoint using a browser, I get the 401 response with a blank html page.
然后我登录,当我再次try 访问端点时,我得到相同的响应.
Then, I try to access the endpoint by specifying the bearer token. And that returns the desired result with the 200 response.
因此,如果我删除[Authorize(AuthenticationSchemes = "Cookies,Bearer")]
,情况就相反了——cookie身份验证工作并返回200,但是上面使用的相同的承载令牌方法不会给出任何结果,只会重定向到默认的AspIdentity登录页面.
我可以看到两个可能的问题:
1) ASP.NET Core不允许"组合"身份验证.
请告知.非常感谢.