【问题标题】:How can I allow multiple domains in a .Net Web API with OAuth token authentication using CORS?如何使用 CORS 在 .Net Web API 中使用 OAuth 令牌身份验证允许多个域?
【发布时间】:2021-10-06 18:31:56
【问题描述】:

我们有一个 .Net Framework Web API,具有基于令牌的 OAuth 身份验证,并且正在尝试通过 Exchange HTML 加载项对其进行调用。我希望允许访问多个域,因为我们可能使用多个不同的应用程序来访问它,但我们不希望允许一般 (*) 访问,因为它是专有的 Web API,所以不需要它可以在已知域之外访问。

为了满足飞行前的需要,我尝试了以下方法:

  • 通过 添加具有多个域的 Access-Control-Allow-Origin 标头 - 这会在包含多个域时返回“标头包含多个值”CORS 错误
  • 通过 PreflightRequestsHandler 添加具有多个域的 Access-Control-Allow-Origin 标头:委托处理程序 - 结果相同

如果我用一个域设置这些,并使用带有 EnableCorsAttribute 的 config.EnableCors 和域,它会将这些添加到标题中,并在冗余域中给出错误。

如何使用 OAuth 和 CORS 设置为多个域设置我的 Web API?

【问题讨论】:

    标签: oauth cors asp.net-web-api2 webapi


    【解决方案1】:

    https://www.ozkary.com/2016/04/web-api-owin-cors-handling-no-access.html 的 Oscar Garcia (@ozkary) 处找到以下内容,实施它并且效果很好!添加到 Microsoft 在创建项目时设置的 AppOAuthProvider:

     /// <summary>
        /// match endpoint is called before Validate Client Authentication. we need
        /// to allow the clients based on domain to enable requests
        /// the header
        /// </summary>
        /// <param name="context"></param>
        /// <returns></returns>
        public override Task MatchEndpoint(OAuthMatchEndpointContext context)
        {
            SetCORSPolicy(context.OwinContext);
            if (context.Request.Method == "OPTIONS")   
            {               
                context.RequestCompleted();
                return Task.FromResult(0);
            }
    
            return base.MatchEndpoint(context);
        }
       
      
        /// <summary>
        /// add the allow-origin header only if the origin domain is found on the     
        /// allowedOrigin list
        /// </summary>
        /// <param name="context"></param>
        private void SetCORSPolicy(IOwinContext context)
        {
            string allowedUrls = ConfigurationManager.AppSettings["allowedOrigins"];
    
            if (!String.IsNullOrWhiteSpace(allowedUrls))
            {
                var list = allowedUrls.Split(',');
                if (list.Length > 0)
                {
    
                    string origin = context.Request.Headers.Get("Origin");
                    var found = list.Where(item => item == origin).Any();
                    if (found){
                        context.Response.Headers.Add("Access-Control-Allow-Origin",
                                                     new string[] { origin });
                    }                   
                }
                
            }
            context.Response.Headers.Add("Access-Control-Allow-Headers", 
                                   new string[] {"Authorization", "Content-Type" });
            context.Response.Headers.Add("Access-Control-Allow-Methods", 
                                   new string[] {"OPTIONS", "POST" });
    
        }            
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-06-09
      • 2019-09-04
      • 2017-07-20
      • 1970-01-01
      • 1970-01-01
      • 2018-06-03
      • 1970-01-01
      • 2019-02-22
      相关资源
      最近更新 更多