CORS 预检请求在 Azure 托管 Web API 中通过 302 重定向进行响应

2023-12-20

设想:

我有两个分别托管在 Windows Azure 上的 ASP.NET Web 应用程序,并且都与同一个 Azure Active Directory 租户关联:

  1. 具有 AngularJS SPA 前端的 MVC 应用程序和adal.js https://github.com/AzureAD/azure-activedirectory-library-for-js用于在客户端上处理 Azure AD 身份验证的库。

  2. 带有 Microsoft OWIN 中间件的 Web API,用于处理服务器上的 Azure AD 身份验证。

Problem:

当 Angular 引导客户端应用程序时,页面在经过 oauth 重定向到正确的身份验证机构后正确加载,并且 adal.js 库正确检索并存储每个应用程序的不同令牌(通过检查 Chrome 开发工具中的资源/会话存储选项卡进行验证)。但是,当客户端应用程序尝试访问或更新 API 中的任何数据时,CORS 预检请求将通过 302 重定向响应到身份验证机构,这会导致控制台中出现以下错误:

XMLHttpRequest 无法加载https://webapi.azurewebsites.net/api/items https://webapi.azurewebsites.net/api/items。 请求被重定向到 'https://login.windows.net/ https://login.windows.net/{authority-guid}/oauth2/authorize?response_type=id_token&redirect_uri=....等..等..', 对于需要预检的跨源请求,这是不允许的。

示例标头(匿名):


Request
OPTIONS /api/items HTTP/1.1
Host: webapi.azurewebsites.net
Connection: keep-alive
Access-Control-Request-Method: GET
Access-Control-Request-Headers: accept, authorization
Origin: https://mvcapp.azurewebsites.net
User-Agent: Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.99 Safari/537.36
Accept: */*
Referer: https://mvcapp.azurewebsites.net/

Response
HTTP/1.1 302 Found
Content-Length: 504
Location: https://login.windows.net/{authority-guid}/oauth2/authorize?response_type=id_token&redirect;_uri=https%3A%2F%2F....etc..etc.%2F&client;_id={api-guid}&scope;=openid+profile+email&response;_mode=form_post&state;=...etc...
Server: Microsoft-IIS/8.0
X-Powered-By: ASP.NET
Set-Cookie: ARRAffinity=4f51...snip....redact....db6d;Path=/;Domain=webapi.azurewebsites.net  

我所做的/尝试过的

  1. 确保 Azure AD 租户允许 OAuth2 隐式流,如所述here http://www.cloudidentity.com/blog/2014/10/28/adal-javascript-and-angularjs-deep-dive/和其他地方。
  2. 确保 API公开访问权限 https://msdn.microsoft.com/en-us/library/azure/dn132599.aspx#BKMK_Exposing并且 MVC/SPA寄存器访问 https://msdn.microsoft.com/en-us/library/azure/dn132599.aspx#BKMK_Accessing使用这些公开的权限。
  3. 在 API 的 web.config 中显式添加了 OPTIONS 动词处理程序(见下文)。
  4. 在 API 服务器、OWIN 本身以及 EnableCorsAttribute 上使用了启用 CORS 的各种组合(见下文)。

问题

有没有办法让与 Azure AD 租户关联的 Web API 不重定向 CORS 预检请求? 我是否缺少 adal.js 库和/或 OWIN 启动代码中的一些初始化设置(见下文)? Azure 门户中是否有允许 OPTIONS 请求通过 OWIN 管道的设置?

相关代码:

adal.js 初始化

angular.module("myApp", ["ngRoute", "AdalAngular"])

.config(["$routeProvider", "$locationProvider", "$httpProvider", "adalAuthenticationServiceProvider",
    function ($routeProvider, $locationProvider, $httpProvider, adalProvider) {

        $routeProvider.when("/", { // other routes omitted for brevity
            templateUrl: "/content/views/home.html",
            requireADLogin: true // restrict to validated users in the Azure AD tenant
        });

        // CORS support (I've tried with and without this line)
        $httpProvider.defaults.withCredentials = true;

        adalProvider.init({
            tenant: "contoso.onmicrosoft.com",
            clientId: "11111111-aaaa-2222-bbbb-3333cccc4444", // Azure id of the web app
            endpoints: {
                // URL and Azure id of the web api
                "https://webapi.azurewebsites.net/": "99999999-zzzz-8888-yyyy-7777xxxx6666"
            }
        }, $httpProvider);
    }
]);

OWIN中间件初始化

public void ConfigureAuth(IAppBuilder app)
{
    // I've tried with and without the below line and also by passing
    // in a more restrictive and explicit custom CorsOptions object
    app.UseCors(CorsOptions.AllowAll);

    app.UseWindowsAzureActiveDirectoryBearerAuthentication(
        new WindowsAzureActiveDirectoryBearerAuthenticationOptions
        {
            TokenValidationParameters = new TokenValidationParameters
            {
                // Azure id of the Web API, also tried the client app id
                ValidAudience = "99999999-zzzz-8888-yyyy-7777xxxx6666"
            },
            Tenant = "contoso.onmicrosoft.com"
        }
    );

    // I've tried with and without this
    app.UseWebApi(GlobalConfiguration.Configuration);
}

WebApiConfig 初始化

public static void Register(HttpConfiguration config)
{
    // I've tried with and without this and also using both this
    // and the OWIN CORS setup above. Decorating the ApiControllers
    // or specific Action methods with a similar EnableCors attribute
    // also doesn't work.
    var cors = new EnableCorsAttribute("https://mvcapp.azurewebsites.net", "*", "*")
    {
        cors.SupportsCredentials = true // tried with and without
    };
    config.EnableCors(cors);

    // Route registration and other initialization code removed
}

API 选项 动词处理程序注册

<system.webServer>
  <handlers>
    <remove name="ExtensionlessUrlHandler-Integrated-4.0" />
    <remove name="OPTIONSVerbHandler" />
    <remove name="TRACEVerbHandler" />
    <add name="OPTIONSHandler" path="*" verb="OPTIONS" modules="IsapiModule" scriptProcessor="C:\Windows\Microsoft.NET\Framework64\v4.0.30319\aspnet_isapi.dll" />
    <add name="ExtensionlessUrlHandler-Integrated-4.0" path="*." verb="*" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" />
  </handlers>
</system.webServer>

相关资源

我曾经尝试过以下(以及更多)论坛和博客文章以及 github 示例代码中的几乎所有可以想象到的组合。

  1. ADAL JavaScript 和 AngularJS – 深入探讨 http://www.cloudidentity.com/blog/2014/10/28/adal-javascript-and-angularjs-deep-dive/
  2. 使用 Azure Active Directory、Owin 中间件和 ADAL 保护 ASP.NET Web API 2 http://bitoftech.net/2014/09/12/secure-asp-net-web-api-2-azure-active-directory-owin-middleware-adal/
  3. 使用 ASP.NET Web API 2、Owin 和 Identity 进行基于令牌的身份验证 http://bitoftech.net/2014/06/01/token-based-authentication-asp-net-web-api-2-owin-asp-net-identity/
  4. AzureADSamples/SinglePageApp-DotNet (github) https://github.com/AzureADSamples/SinglePageApp-DotNet
  5. AngularJSCORS (github) https://github.com/matvelloso/AngularJSCORS
  6. 如何在WebAPI 2中进行CORS身份验证? https://stackoverflow.com/q/20079813/51242
  7. WebApi 上的 AngularJS 和 OWIN 身份验证 https://stackoverflow.com/q/24461605/51242

我遇到了类似的问题来找出合适的软件包。只需要设置 Owin cors 即可。请先检查 owin.cors 的软件包。

<package id="Microsoft.Owin" version="3.0.0" targetFramework="net45" />
<package id="Microsoft.Owin.Cors" version="2.1.0" targetFramework="net45" />

处理程序的 Web 配置选项:

<system.webServer>
<handlers>
  <remove name="ExtensionlessUrlHandler-Integrated-4.0" />
  <remove name="OPTIONSVerbHandler" />
  <remove name="TRACEVerbHandler" />
  <add name="ExtensionlessUrlHandler-Integrated-4.0" path="*." verb="*" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" />
</handlers>

您在 owin 配置中指定 cors 选项是正确的。

 public void ConfigureAuth(IAppBuilder app)
    {
        app.UseWindowsAzureActiveDirectoryBearerAuthentication(
            new WindowsAzureActiveDirectoryBearerAuthenticationOptions
            {
                Audience = ConfigurationManager.AppSettings["ida:Audience"],
                Tenant = ConfigurationManager.AppSettings["ida:Tenant"]

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

控制器不需要CORS相关属性。

   [Authorize]
public class ContactsController : ApiController
{

    // GET api/<controller>
    public IEnumerable<string> Get()
    {
        return new string[] { "person1", "person2" };
    }

    // GET api/<controller>/5
    public string Get(int id)
    {
        return "person" + id;
    }

WebAPIConfig 不需要 CORS 相关条目。

工作示例在这里:https://github.com/omercs/corsapisample https://github.com/omercs/corsapisample

您可以使用以下代码在您的应用程序中进行测试:

app.factory('contactService', ['$http', function ($http) {
var serviceFactory = {};

var _getItems = function () {
    $http.defaults.useXDomain = true;
    delete $http.defaults.headers.common['X-Requested-With'];
    return $http.get('http://yourhostedpage/api/contacts');
};

serviceFactory.getItems = _getItems;

return serviceFactory;

}]);

飞行前响应示例:

Remote Address:127.0.0.1:8888
Request URL:http://localhost:39725/api/contacts
Request Method:OPTIONS
Status Code:200 OK
Request Headersview source
Accept:*/*
Accept-Encoding:gzip, deflate, sdch
Accept-Language:en-US,en;q=0.8
Access-Control-Request-Headers:accept, authorization
Access-Control-Request-Method:GET
Host:localhost:39725
Origin:http://localhost:49726
Proxy-Connection:keep-alive
Referer:http://localhost:49726/myspa.html
User-Agent:Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.99 Safari/537.36
Response Headersview source
Access-Control-Allow-Credentials:true
Access-Control-Allow-Headers:authorization
Access-Control-Allow-Origin:http://localhost:49726
Content-Length:0
Date:Fri, 23 Jan 2015 01:10:54 GMT
Server:Microsoft-IIS/8.0
X-Powered-By:ASP.NET
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

CORS 预检请求在 Azure 托管 Web API 中通过 302 重定向进行响应 的相关文章

  • 包含routeChangeSuccess的AngularJS测试控制器

    我正在尝试创建单元测试来测试导航列表控制器 但在创建测试时遇到问题 这是控制器的代码 navListModule controller NavListCtrl scope NavList function scope NavList sco
  • 为什么从浏览器上传到 S3 时出现 403 错误?

    因此 我尝试查看此处之前的答案 但似乎没有任何效果 我正在使用 Dropzone 它似乎发出 OPTIONS 请求来获取所有允许的 CORS 相关信息 但它似乎没有正确返回 因此 通过查看 Chrome 开发工具 我有以下请求标头 Host
  • 直接从浏览器将文件上传到 Azure Blob 存储?

    是否可以创建一个 html 表单以允许 Web 用户直接将文件上传到 azure blob 存储 而无需使用其他服务器作为中介 S3 和 GAW blobstore 都允许这样做 但我找不到任何对 azure blob 存储的支持 编辑 2
  • 当请求新页面时,如何将 AngularJS 路由与 Express (Node.js) 结合使用?

    我正在使用 Express 它从静态目录加载 AngularJS 一般情况下我会要求http localhost 其中 Express 为我服务index html以及所有正确的 Angular 文件等 在我的 Angular 应用程序中
  • Angular UI 模式的范围问题

    我无法理解 使用角度 UI 模式的范围 虽然这里不是很明显 但我已经正确设置了模块和所有内容 据我所知 但这些代码示例尤其是我发现错误的地方 index html 其中重要部分 div class btn group div
  • 在控制台应用程序中使用 SendGrid

    是否可以在 C 的控制台应用程序中使用发送网格 我的代码不起作用 我真的不知道为什么 你可以帮帮我吗 using System using System Net using System Net Mail using SendGrid na
  • 如何在 Visual Studio 中更改 Azure 数据库表的列顺序

    我整个下午都在寻找在 MS Visual Studio 2022 中重新排序 Azure 数据库表列的方法 没有运气 在其他应用程序中 可以通过拖动或剪切和粘贴轻松重新排列列 这里无能为力 此时 我什至不确定可以在 VS 中移动列 我只对
  • Angular.js:如何从无序列表中获取 orderBy 或过滤器来工作?

    尝试根据价格和评级 在返回的对象中 进行排序 我宁愿用 ng click 和 li 来代替使用选择菜单 有没有办法做到这一点 我环顾四周 这是我能想到的最接近的 ul class restaurant filter li i class i
  • 从 mvc 控制器使用 Web api 控制器操作

    我有两个控制器 一个mvc控制器和一个api控制器 它们都在同一个项目中 HomeController Controller DataController ApiController 如果我想从 HomeController 中使用 Dat
  • 用户脚本 - 有没有办法将 jquery 代码注入 angularjs dom?

    所以我正在尝试为一个网站创建用户脚本 我无法更改网站的任何源代码 因为它不是我的 网站到处都使用 AngularJS 控制器 我研究了几天如何做到这一点 但没有成功 所以我尝试注入代码 nav after div test div 当我通过
  • 使用 JS 在 Angular 中创建删除按钮的确认警报

    我有一个带有删除按钮的表单 我想创建一个单击删除按钮时弹出的确认框 删除按钮当前有效 我在 javascript 中尝试了一些方法 但没有成功 我正在使用角度 这是最好的方法吗 另外 有谁知道这方面的任何例子 我还没有找到任何有效的例子 d
  • 更改表分布(Azure 数据仓库)

    在急着下班的过程中 我在一个采用循环分配的新桌子上运行了一个大型插入 我知道这不太可能 但是有没有办法将表的分布从循环更改为哈希分布 很遗憾ALTER TABLE不支持更改 Azure SQL 数据仓库中表的分布 您可以做的下一个最好的事情
  • asp.net core / kestrel中的线程管理

    我正在解决我们已迁移到 asp net core 2 0 的 asp net 应用程序的性能 可扩展性问题 我们的应用程序作为应用程序服务托管在 azure 上 并且在任何中等流量的情况下都很容易崩溃 让我困惑的一件事是如何处理多个并发请求
  • 从Web API同步调用外部api

    我需要从我的 Web API 2 控制器调用外部 api 类似于此处的要求 使用 HttpClient 从 Web API 操作调用外部 HTTP 服务 https stackoverflow com questions 13222998
  • 从另一台设备访问 Azure 模拟器

    我有两个不同的项目 Windows Phone 8 应用程序 我正在真实的物理开发设备上运行 Azure 云服务 其中包含一个简单的 WebRole 端点 其中包含 ASP NET MVC WebAPI 我的目标很简单 使用从真实设备运行的
  • Azure函数无法读取应用程序设置和连接字符串

    我在 Azure 中创建了一个 HTTP 触发器函数 希望从 Azure SQL 数据库读取数据 我在函数应用程序的连接字符串部分配置了连接字符串 如下图所示 我使用下面的代码尝试读取连接字符串 var cnnString Configur
  • ngRepeat 中的函数执行过于频繁

    我有三个tabs里面有不同的htmlng include 这些选项卡使用以下方式显示ng repeat 只有一个 HTML 模板包含函数调用 但它执行了 3 次 每个模板执行一次 ng repeat迭代 这里出了什么问题以及如何解决它 va
  • 如何在点击时添加和删除活动类 - 角度方式

    我正在这样做 div class tiles div 控制器 scope select function item scope selected item scope isActive function item return scope
  • 在 Dapper 中处理 Oracle 数据库连接

    我正在尝试连接到 Oracle 数据库并尝试执行查询 下面是我的模型类 using System using System Collections Generic using System Linq using System Web usi
  • 是否可以在 ASP.NET Web API 和 SPA 中使用基于 cookie 的身份验证?

    我想创建基于 angularjs 前端和 ASP NET Web API 的 Web 应用程序 我需要创建安全 api 但我无法在将实施此 Web 应用程序的公司服务器上使用基于令牌的身份验证 是否可以对 SPA 和 ASP NET Web

随机推荐