在 jwt 令牌的上下文中,使用了“承载者”一词。我在谷歌上找不到很多关于承载者的信息。我想知道这些术语来自哪里以及它代表什么?为什么它有前缀?
我OAuthAuthorizationServer在 OWIN ASP.NET C# web API 中使用 OWIN库来生成和处理不记名令牌。
现在,我有一个端点(您在OAuthAuthorizationServerOptions结构中设置),它接受来自前端的grant_type,username和password字段。我创建了一个执行验证的提供程序类,然后相应地调用context.Validated()或context.SetError()。然后中间件处理生成令牌并将其返回给用户,并“接管”登录端点,在内部完成所有工作。
现在,我正在向我的 API 添加一个新功能,用户可以在其中更改他们的“角色”(例如,管理员可以将自己设置为普通用户以查看他们的工作结果,用户可以在多个角色中进行选择等)由于我已经通过不记名令牌处理了这个问题(我将用户的角色存储在那里,我的所有端点都使用不记名令牌来确定当前角色),我现在有理由从 API 后端更新不记名令牌的内容。
我想要做的是允许前端调用api/set_role将接受参数的端点(例如)。用户请求某个角色,他们当前的不记名令牌将伴随请求。然后服务器将检查是否允许相关用户使用该特定角色,如果允许,将生成一个新令牌并将其在响应正文中返回给用户。然后前端将在本地存储中更新其令牌。或者,当然,如果不允许用户切换到该角色,后端会返回适当的错误,前端会做出相应的反应。
为此,我基本上希望能够手动生成令牌。与我identity.AddClaim()在登录提供程序中的使用方式类似,我希望能够在 API 代码中的任意位置执行此操作。该方法将负责将任何必要的现有信息(例如用户的用户名)转移到新令牌中,因为它已经拥有现有的令牌。
我想要的伪代码:
if (!userCanUseRole(requestedRoleId)) return Request.CreateErrorResponse(...);
// we have a struct containing parsed information for the current token in the variable cToken
bearerToken newToken = new bearerToken();
newToken.AddClaim(new Claim("user", cToken.user));
newToken.AddClaim(new Claim("role", requestedRoleId));
string tokenToReturnToFrontend = newToken.getTokenString(); // string suitable for using in Authorization Bearer …Run Code Online (Sandbox Code Playgroud) 我目前正在 Xamarin.Forms 中构建我的第一个移动应用程序。该应用程序有一个 facebook 登录,在用户登录后,我存储 facebook 令牌,因为我想将它用作不记名令牌来验证针对 API 的任何进一步请求。
该 API 是一个 .NET core 2.0 项目,我正在努力使身份验证正常工作。
在我的 Xamarin.Forms 应用程序中,使用以下代码将 facebook 令牌设置为不记名令牌;
_httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", UserToken);
Run Code Online (Sandbox Code Playgroud)
据我所知,这正确地在请求的标头中设置了承载令牌。我和我的一位同事谈过这个问题,他告诉我看一下 Identityserver4,它应该支持这个。但目前,我决定不这样做,因为对我来说,目前实施这一点是有开销的。因此,我决定坚持使用 Facebook 代币作为不记名代币的想法并验证这一点。
因此,我的下一步是找到一种方法来通过 Facebook 验证传入的不记名令牌,以检查它是否(仍然)有效。所以我为我的 API 项目配置了启动,如下所示;
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.AddAuthentication(o =>
{
o.DefaultAuthenticateScheme = …Run Code Online (Sandbox Code Playgroud) 我不明白为什么这不起作用。我假设这是我忽略的简单事情。所有其他不使用令牌的测试方法都可以正常工作。目前令牌没有过期时间,我可以通过 Postman 很好地使用它。
@Test
public void getUser() throws Exception {
String token = "eyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiJsd2lsbGlhbXMxNiIsInJvbGVzIjoidXNlciIsImlhdCI6MTUxNDQ0OTgzM30.WKMQ_oPPiDcc6sGtMJ1Y9hlrAAc6U3xQLuEHyAnM1FU";
MvcResult mvcResult = mockMvc.perform(
MockMvcRequestBuilders.get("/api/users/lwilliams16")
.header("authentication", "Bearer " + token))
.andExpect(status().isOk())
.andExpect(content().contentType(MediaType.APPLICATION_JSON))
.andDo(print())
.andReturn();
System.out.println(mvcResult.getResponse().getContentAsString());
}
Run Code Online (Sandbox Code Playgroud) 使用承载令牌身份验证。如果响应失败,则需要返回附加字段以及以下消息:
401 UnAuthorize response
{Message: "Authorization has been denied for this request"}
Run Code Online (Sandbox Code Playgroud)
如何在 401 响应消息中包含附加字段。它将如下所示:(包括指示故障跟踪 ID 的附加字段“ID”)。
{Message: "Authorization has been denied for this request",
Id: 1}
Run Code Online (Sandbox Code Playgroud)
filter.config 如下:
config.Filters.Add(new HostAuthenticationFilter(OAuthDefaults.AuthenticationType));
Run Code Online (Sandbox Code Playgroud) 我有一个 AuthService,主要有两种方法:
getAuthToken(返回 Promise,因此可以延迟调用/在单个集合上阻塞等待多次调用)
refreshToken(还返回一个 Promise,使用原始 JWT 上可用的刷新令牌来请求新的身份验证令牌)
我想自动
这是代码:
import { HttpEvent, HttpHandler, HttpHeaders, HttpInterceptor, HttpRequest } from "@angular/common/http";
import { from, Observable } from "rxjs";
import { Injectable } from "@angular/core";
import { AuthService } from "./auth.service";
@Injectable()
export class AuthHttpInterceptor implements HttpInterceptor {
constructor(
private _authService: AuthService,
) {
}
intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
return from(this.addBearerToken(req, next));
}
private async addBearerToken(req: HttpRequest<any>, next: HttpHandler): Promise<HttpEvent<any>> {
const token = …Run Code Online (Sandbox Code Playgroud) 有没有办法解密 API 管理策略中的不记名令牌,以便创建 acr_values 条件,例如租户。
看看 MS 文档,这似乎不可能,我希望实现类似的目标:
<when condition="@(context.Request.Headers["Authorization"] --DO MAGIC HERE-- .acr_values["tenant"] == "contoso" ">
<set-backend-service base-url="http://contoso.com/api/8.2/" />
</when>
Run Code Online (Sandbox Code Playgroud)
或者类似于此处的示例,但用于设置支持的服务:
http://devjourney.com/blog/2017/03/23/extract-jwt-claims-in-azure-api-management-policy/
我想从 Postman 生成 Azure 令牌,以便在我的项目中进行 API 授权。我能够使用下面的 API 请求生成令牌,但在另一个 API 请求中使用生成的令牌时收到以下错误消息“此请求的授权被拒绝”。
端点#
https://login.microsoftonline.com/:tenant_id/oauth2/token
Run Code Online (Sandbox Code Playgroud)
参数#
tenant_id:As per id generation by azure.
Run Code Online (Sandbox Code Playgroud)
正文#(表单数据)
grant_type:client_credentials
client_id:As per id generation by azure.
client_secret:As per id generation by azure.
resource:Required URL
Run Code Online (Sandbox Code Playgroud)
回复#
"token_type": "Bearer",
"expires_in": "foo",
"ext_expires_in": "foo",
"expires_on": "foo",
"not_before": "foo",
"resource": "foo",
"access_token":foo
Run Code Online (Sandbox Code Playgroud)
由于上面返回的令牌不被接受,我也在请求正文中传递了用户名和密码,但最终得到了相同的结果。即使我的凭据是错误的,天蓝色也没有考虑我的凭据。
您能否帮助我在响应中还需要发送哪些内容才能获取有效的令牌 ID?
azure access-token azure-active-directory postman bearer-token
我使用https://generator.swagger.io/ 上的在线 Swagger Codegen为这个 API生成了一个 Python 客户端库。API 使用 Bearer 身份验证:
openapi: 3.0.0
...
paths:
/sandbox/register:
post:
...
security:
- sso_auth: []
...
components:
securitySchemes:
sso_auth:
type: http
scheme: bearer
Run Code Online (Sandbox Code Playgroud)
但是,Configuration生成的 Python 客户端中的类没有access_token字段。
使用生成的客户端库时如何填写不记名访问令牌?
代码生成端点POST /gen/clients/{language}具有authorizationValue和securityDefinition参数 - 我是否需要以某种方式配置这些参数?
"authorizationValue": {
"value": "string",
"type": "string",
"keyName": "string"
},
"securityDefinition": {
"type": "string",
"description": "string"
}
Run Code Online (Sandbox Code Playgroud) 我一直在关注有关 Vue + Laravel 身份验证的教程,一切都已设置完毕,但随后教程转到将令牌存储在本地存储中。我读到这不是应该遵循的最佳实践,因为它更容易受到 XSS 攻击。
问题是很难找到关于在 cookie 中存储令牌的教程(特别是 Laravel + Vue)。任何人都可以帮助如何实现在 cookie 中存储令牌吗?
非常感谢任何可以提供帮助的人。
这是我当前的代码。
控制器
public function login(Request $request)
{
$http = new\GuzzleHttp\Client;
try {
$response = $http->post(config('services.passport.login_endpoint'), [
'form_params' => [
'grant_type' => 'password',
'client_id' => config('services.passport.client_id'),
'client_secret' => config('services.passport.client_secret'),
'username' => $request->username,
'password' => $request->password,
]
]);
return $response->getBody();
} catch (\GuzzleHttp\Exception\BadResponseException $e) {
if ($e->getCode() === 400) {
return response()->json('Invalid Request. Please enter a username or a password.', $e->getCode());
} else if ($e->getCode() === …Run Code Online (Sandbox Code Playgroud) laravel single-page-application bearer-token vue.js laravel-passport
bearer-token ×10
azure ×2
c# ×2
jwt ×2
access-token ×1
angular ×1
asp.net ×1
asp.net-core ×1
authorize ×1
facebook ×1
https ×1
interceptor ×1
java ×1
laravel ×1
mockmvc ×1
oauth ×1
postman ×1
python ×1
spring-boot ×1
swagger ×1
vue.js ×1