zoukankan      html  css  js  c++  java
  • 服务端跨域四种方式

    1、自定义方式

    ····/// <summary>
        /// 支持WebAPI服务器端跨域
        /// 有很多种支持服务端跨域的方式,但是不能够同时使用
        /// </summary>
        public class ServerCrossDomainAttribute : ActionFilterAttribute
        {
            private const string ORIGIN = "Origin";
            private const string ACCESS_CONTROL_ALLOW_ORIGIN = "Access-Control-Allow-Origin";
    
            public override void OnActionExecuted(HttpActionExecutedContext actionExecutedContext)
            {
                #region 跨域
                if (actionExecutedContext.Request.Headers.Contains(ORIGIN))
                {
                    string originHeader = actionExecutedContext.Request.Headers.GetValues(ORIGIN).FirstOrDefault();
                    if (!string.IsNullOrEmpty(originHeader))
                    {
                        #region 第一种支持跨域的方式
                        //actionExecutedContext.Response.Headers.Add(ACCESS_CONTROL_ALLOW_ORIGIN, originHeader);
                        //actionExecutedContext.Response.Headers.Add("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS");
                        //actionExecutedContext.Response.Headers.Add("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept, Authorization");
                        #endregion
    
                        #region 第二种支持跨域方式
                        //actionExecutedContext.Response.Headers.Add("Access-Control-Allow-Origin", "*");
                        //actionExecutedContext.Response.Headers.Add("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS");
                        //actionExecutedContext.Response.Headers.Add("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept, Authorization");
                        #endregion
    
                        #region 第三种支持跨域方式  在webconfig中配置
                        //< !--< add name = "Access-Control-Allow-Origin" value = "*" />    
                        //< add name = "Access-Control-Allow-Headers" value = "Content-Type, Authorization" />       
                        //< add name = "Access-Control-Allow-Methods" value = "GET, POST, PUT, DELETE, OPTIONS" /> -->
                        #endregion
                    }
                }
                #endregion
    
                
            }
        }

    2、使用System.Web.Http.Cors

  • 相关阅读:
    IDEA 使用 Gradle 创建 Java 项目
    Java HttpsUnits 工具类实现 Https
    Android Thread 常用方法
    Android Handler、Message 常用方法
    Android 透明导航键遮挡布局
    Android 通过 JDBC 连接远程数据库
    python 连接数据库
    备份及更新 Portainer
    secureCRT免密码登陆Linux
    Linux vi 编辑器常见命令的使用
  • 原文地址:https://www.cnblogs.com/qtiger/p/13427047.html
Copyright © 2011-2022 走看看