如何获得Angular应用程序的授权令牌?

发布于 2025-02-05 02:56:44 字数 260 浏览 1 评论 0原文

  • 前端:角
  • 后端:.NET核心API
  • 环境:MS Azure;

要求:我们希望.NET Core API接受具有访问令牌的授权UI应用程序的请求。我们目前正在尝试获取访问令牌,以便我们可以在标题中包括并发送到.NET Core API,但是我们面临的问题是,在本地运行Angular应用程序时,我们无法从Online.microsoftonline.com获得访问令牌。而是臭名昭著的CORS错误。

我们可能会怎么做?

  • Frontend: Angular
  • Backend: .NET core API
  • Environment: MS Azure;

Requirement: We want .NET core API to accept requests from authorized UI application who are having access token. We are current trying to get the access token so that we can include in header and send to .NET core API, but we are facing an issue that while running the angular application locally we are unable to get access token from online.Microsoftonline.com instead the infamous CORS error.

What is wrong we may be doing?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

つ可否回来 2025-02-12 02:56:44

我们知道拦截器和牢记。但是在要求令牌时
从Angular中,我们从Microsoft网站获得CORS错误。

很少有解决方法,

请确保您的API与另一个域没有连接,这可能是无法从CORS策略访问的原因,因此可能是由于这个原因,您将无法调用API 。

例如启用cors属性;

[EnableCors(origins: "http://webapp.net ", headers: "*", methods: "*")]
      public class TestController : ApiController
      {
          // Controller methods not shown...
      }
  }

有关更多信息,请参阅此 ms q& a @deepdave-mt提出的讨论在评论中提出。

而且,此博客 | ASP.NET Core API使用Azure AD Auth和用户访问令牌

we are aware of interceptors and CORS. But while requesting for token
from Angular we were getting cors error from Microsoft website.

There are few of the workarounds,

Make sure that your API is not connected with another domain which can be reason for not able to get access from CORs policy ,so may be for that reason you will not be able to call the API.

For example to enable CORs attribite ;

[EnableCors(origins: "http://webapp.net ", headers: "*", methods: "*")]
      public class TestController : ApiController
      {
          // Controller methods not shown...
      }
  }

For more information please refer this MS Q&A discussion which is suggested by @DeepDave-MT in comment.

And, this Blog|ANGULAR SPA WITH AN ASP.NET CORE API USING AZURE AD AUTH AND USER ACCESS TOKENS.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文