gpt4 book ai didi

cors - 当与 ASP.NET Web Api 2.0 一起使用时,Microsoft.Owin.Cors 中间件有什么作用?

转载 作者:行者123 更新时间:2023-12-04 14:57:57 25 4
gpt4 key购买 nike

我有一个带有 token 身份验证的 ASP.NET Web Api 2.0 项目,并且主要在本文之后完成了所有工作:

Token Based Authentication using ASP.NET Web API 2, Owin, and Identity , 一点技术

但是我很难理解 Startup.cs 中的这行代码到底做了什么:

app.UseCors(Microsoft.Owin.Cors.CorsOptions.AllowAll);

这不会使 Web Api 将 Access-Control-Allow-Origin header 添加到我的 API 响应中,换句话说,它不会在我的 Web Api 中启用 Cors(顺便说一下,仍在尝试了解如何执行此操作)。它甚至没有将它添加到我的不记名 token 认证服务器响应中。我必须将此代码添加到我的 OAuthAuthorizationServerProvider:
public override Task GrantResourceOwnerCredentials(OAuthGrantResourceOwnerCredentialsContext context)
{
context.OwinContext.Response.Headers.Add("Access-Control-Allow-Origin", new[] { "*" });

在我的 token 提供程序端点响应上启用 Cors。

那么这个 Microsoft.Owin.Cors 中间件到底有什么用呢?因为我到处都读到 Web Api 2.0 和 Cors 这行代码
app.UseCors(Microsoft.Owin.Cors.CorsOptions.AllowAll);

过来:

最佳答案

感谢您关注我的教程。

本行 app.UseCors(Microsoft.Owin.Cors.CorsOptions.AllowAll);用于为 API 本身启用 CORS(从 ApiController 继承的任何 Controller )。

但是对于 Authz 服务器和端点 /token这没有任何影响,这就是为什么我必须添加 context.OwinContext.Response.Headers.Add("Access-Control-Allow-Origin", new[] { "*" });这个端点不是 API 的一部分,也不是从 ApiController 继承的。类(class)。

希望这能回答你的问题。

关于cors - 当与 ASP.NET Web Api 2.0 一起使用时,Microsoft.Owin.Cors 中间件有什么作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26888481/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com