【发布时间】:2019-07-10 04:17:11
【问题描述】:
同时使用 Cookie 身份验证中间件和 JWT 身份验证中间件。 当我登录用户时,我创建自定义声明并将它们附加到基于 cookie 的身份。我还从外部来源获得了一个 jwt 令牌,它有自己的声明(我使用这个令牌来访问外部资源)。 启用身份验证时,我的控制器类看起来像这样
[Authorize(AuthenticationSchemes = AuthSchemes)]
public class MixedController : Controller
// Requires the following imports:
// using Microsoft.AspNetCore.Authentication.Cookies;
// using Microsoft.AspNetCore.Authentication.JwtBearer;
private const string AuthSchemes =
CookieAuthenticationDefaults.AuthenticationScheme + "," +
JwtBearerDefaults.AuthenticationScheme;
根据上面的代码 sn-p,如果 Cookie 或 JWT 验证成功,则请求被视为已验证。如果 Cookie Auth 或 JWT auth 失败,我的要求是拒绝该请求。对于我的情况,只使用一个模式不是一个好的选择。如果我的 cookie 有效但我的令牌已过期,我想以“未通过身份验证”为由使请求失败。我该怎么做?
【问题讨论】:
标签: security authentication cookies asp.net-core jwt