PHP群:95885625 Hbuilder+MUI群:81989597 站长QQ:634381967
    您现在的位置: 首页 > 开发编程 > ASP.NET教程 > 正文

    C#后台如何调用跨域MVC服务并实现Cookie验证??

    作者:admin来源:网络浏览:时间:2020-09-30 00:07:50我要评论
    导读:B5教程网今天分享一个C#后台如何调用跨域MVC服务并实现Cookie验证??是不是大家都遇到过这样的问题呢?背景随着富客户端框架的盛行,以及...
    B5教程网今天分享一个C#后台如何调用跨域MVC服务并实现Cookie验证??是不是大家都遇到过这样的问题呢?
    背景

    随着富客户端框架的盛行,以及众多优秀的前端js框架,很多情况我们会遇到跨域的问题,而js的ajax请求是不允许直接跨域访问的,当然你会说可以用JSONP等,但是由于代码洁癖,不想在前端和后台添加callback,而且很多情况你是无法控制的,需要牵连考虑太多的情况。

    所以我直接绕过了,每个前端应用,自带一个通用后端服务代理,该服务解决跨域问题,自动代理帮前台获取跨域的数据。 

    如何算跨域

    虽然是个老问题,但是还是要提醒注意下两点:同IP,不同端口,数据访问是跨域的,但是Cookie访问是可以的(这个让我很难理解)
    解决,源码


     
    复制代码代码如下:
    1. CookieContainer cookieContainer = new CookieContainer(); 
    2.  
    3.      [HttpPost] 
    4.         public string CommonPost(string url) 
    5.         { 
    6.             log.Info(CookieHelper.GetCookie("ITDC_UserName") + "进入方法CommonPost Url=" + url); 
    7.             Uri address = new Uri(System.Configuration.ConfigurationManager.AppSettings["RESTfulAPI"].ToString() + url); 
    8.             HttpWebRequest request = WebRequest.Create(address) as HttpWebRequest; 
    9.             request.Method = "POST"; 
    10.             request.ContentType = "application/x-www-form-urlencoded"; 
    11.        //远程服务,需要加入cookie验证
    12.             cookieContainer.Add(address, GetCookie("ITDC_UserName")); 
    13.             cookieContainer.Add(address, GetCookie("ITDC_UserRole")); 
    14.             request.CookieContainer = cookieContainer; 
    15.             StringBuilder data = new StringBuilder(); 
    16.             for (int i = 0; i < Request.QueryString.Count; i++) 
    17.             { 
    18.                 if (Request.QueryString.Keys[i].ToString() == "url") continue; 
    19.                 data.Append("&" + Request.QueryString.Keys[i].ToString() + "=" + Request.QueryString[i].ToString()); 
    20.             } 
    21.             // Create a byte array of the data we want to send
    22.             byte[] byteData = UTF8Encoding.UTF8.GetBytes(data.ToString().TrimStart('&')); 
    23.             // Set the content length in the request headers
    24.             request.ContentLength = byteData.Length; 
    25.             // Write data
    26.             using (Stream postStream = request.GetRequestStream()) 
    27.             { 
    28.                 postStream.Write(byteData, 0, byteData.Length); 
    29.             }   
    30.             string result = ""; 
    31.             using (HttpWebResponse response = request.GetResponse() as HttpWebResponse) 
    32.             { 
    33.                 StreamReader reader = new StreamReader(response.GetResponseStream()); 
    34.                 result = reader.ReadToEnd(); 
    35.             } 
    36.             log.Info(CookieHelper.GetCookie("ITDC_UserName") + " 执行完成 CommonPost Url=" + url); 
    37.             return (result); 
    38.         }


    前台调用

     
    复制代码代码如下:
    1. Ext.Ajax.request({url: APIUrl + '/Nebula/CommonPost?url=/Nebula/PostComment/&KlId=1&Msg=ok&Author=admin&Title=文章标题', 
    2.                   method: "POST", 
    3.                   success: function (response) { 
    4.                               Ext.Viewport.unmask(); 
    5.                               var obj = Ext.decode(response.responseText); 
    6.                               Ext.Msg.alert("提示", obj.Msg, Ext.emptyFn); 
    7.                            }, 
    8.                   failure: function (response) { 
    9.                               Ext.Viewport.unmask(); 
    10.                               Ext.Msg.alert("提示", "操作失败,请检查网络!", Ext.emptyFn); 
    11.                            } 
    12. });

    转载请注明(B5教程网)原文链接:https://b5.mxunkeji.com/content-11-565-1.html
    相关热词搜索: MVC Cookie 跨域 C#