401带有有效的Bear令牌,怎么可能出问题? [英] 401 with a valid bear token, what could go wrong?

查看:162
本文介绍了401带有有效的Bear令牌,怎么可能出问题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我应该寻找什么来查找/跟踪这样的问题:

What should I look into to find out / track down a issue like this:

我们有一个调用.Net Core Web API的Angular应用,并启用了CORS ,并通过Azure AD进行身份验证,获得Bearer令牌。使用Bearer令牌,API调用会失败,有时会出现401 Unauthorized,有时是200。但是我将相同的Bearer Token放入任何REST客户端,并且API调用始终有效。

We have an Angular App calling a .Net Core Web API, with CORS enabled, and it authenticate through Azure AD, getting a Bearer token. With the Bearer token, the API calls fail with 401 Unauthorized from time to time, other times, 200. Yet I put the same Bearer Token into any REST client and API calls always work.

非常感谢任何输入。

更多上下文/分析:


  • API可以与其他应用程序正常工作。因此,如果问题出在服务器端
    上,则它不是代码中经常执行的部分。
    问题可能在客户端Angular代码中。

  • 此Angular应用是
    唯一使用CORS的应用。显然,这是我正在研究的东西。

  • API works fine with other applications. So if the issue is on the server side, it is not an often executed part of the code. The issue could be in the client side Angular code.
  • This Angular app is the only app use CORS. This obviously is something I'm looking into.

此Angular应用程序的本地开发服务器(Webpack开发服务器)和部署服务器(IIS)不同,但是产生
相同的结果,因此它可能不是服务器配置问题。

Local Development Server(Webpack dev server) and deployment server(IIS) for this Angular app are different, yet produce the same result, so it might not be a server config issue.

推荐答案

我知道了。
比赛条件。承载令牌尝试检索令牌时尚不可用。
到我在REST客户端中手动进行API调用时,该令牌已可用。仅当我有一些后续的API调用时,无论第一个是什么,我都可以更快地识别出此问题。

I figured it out. Race condition. Bearer token is not available there yet when it try to retrieve it. By the time I manually do API call in REST client, the token is available. Only if I have some subsequent API calls regardless of the first one, I would have identify this issue faster.

一个简单的超时就可以证明其有效。

A simple timeout would demonstration that it works.

我们将优化Bearer令牌存储和某些缓存管理,以提供永久解决方案。

We will optimize the Bearer token storage and some cache management for a permanent solution.

这篇关于401带有有效的Bear令牌,怎么可能出问题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆