UseJwtBearerAuthenticationASP.NET Core中的中间件可以轻松验证Authorization标头中传入的JSON Web令牌.
如何验证通过cookie而不是标头传递的JWT?有点像UseCookieAuthentication,但对于只包含JWT的cookie.
我想要的是:
代码怎么样:
public void Configuration(IAppBuilder app)
{
app.UseOAuthAuthorizationServer(new OAuthAuthorizationServerOptions
{
AuthorizeEndpointPath = "/Authorize",
AllowInsecureHttp = true,
Provider = new OAuthAuthorizationServerProvider
{
OnGrantCustomExtension = GrantCustomExtension,
OnValidateClientRedirectUri = ValidateClientRedirectUri,
OnValidateClientAuthentication = ValidateClientAuthentication,
}
});
app.UseOAuthBearerAuthentication(new OAuthBearerAuthenticationOptions
{
Provider = new OAuthBearerAuthenticationProvider
{
//Handles applying the authentication challenge to the response message.
ApplyChallenge=MyApplyChallenge,
//Handles processing OAuth bearer token.
RequestToken=MyRequestToken,
//Handles validating the identity produced from an OAuth bearer token.
ValidateIdentity = MyValidateIdentity,
}
});
app.UseWebApi(new WebApplication3.Config.MyWebApiConfiguration());
}
Run Code Online (Sandbox Code Playgroud)
问题是什么:
的3个属性OAuthBearerAuthenticationProvider …
我将基于 jwt 的身份验证添加到我的 api,将令牌添加到标头(授权:Bearer {tokenhere})。
我研究了如何在客户端存储它,最推荐的方法是使用 HttpOnly cookie,它设置在服务器上,因此客户端代码无法访问它。浏览器将获取它并将其附加到将来的请求中。
为此,服务器必须在响应中写入此 cookie,并能够验证它而不是标头中的令牌(授权:Bearer {tokenhere})。
我该如何设置?我搜索了how和low,但没有找到这方面的教程,它要么是cookie,要么是jwt,从来没有将jwt存储为httponly cookie。
在 ASP.NET Core API 项目中,我需要验证位于与 Authorization 标头不同的标头中的另一个 JWT Bearer 令牌。例如,假设发送一个 GET 请求来获取产品,/api/products并在名为 的标头中包含承载令牌AccessToken。
curl --location --request GET 'https://localhost/api/products' \
--header 'AccessToken: <bearer_token>'
Run Code Online (Sandbox Code Playgroud)
我引用Microsoft.AspNetCore.Authentication.JwtBearer包并在 API 项目中设置身份验证,如下所示:
curl --location --request GET 'https://localhost/api/products' \
--header 'AccessToken: <bearer_token>'
Run Code Online (Sandbox Code Playgroud)
但是,我在JwtBearerOptions Class中找不到有关标头名称的任何内容。
如何配置 JWT 身份验证以从名为“AccessToken”的标头读取 JWT? 是否可以使用 Microsoft.AspNetCore.Authentication.JwtBearer 包?
asp.net-core ×3
jwt ×3
cookies ×2
asp.net ×1
asp.net-core-authenticationhandler ×1
c# ×1
oauth-2.0 ×1
owin ×1