会话不能在ASP.MVC中的IE和Mobile Safari中使用JSONP调用

时间:2015-09-06 12:41:47

标签: c# json asp.net-mvc jsonp

我有MVC应用程序,我们的客户使用我们提供的代码使用JSONP ajax调用从他们的网站发送请求。

当我们在Chrome和Firefox上浏览他们的网站时,MVC应用程序可以保留会话信息,但是当您从IE浏览网站(任何版本)和移动Safari MVC应用程序将每个请求视为新请求时...我不确定这是前端或后端问题(因为在Chrome和FF中一切正常,我认为它与前端相关)

以下是代码:

在我们的web.config文件中,我们允许从任何地方使用CORS:

<httpProtocol>
  <customHeaders>
    <add name="Access-Control-Allow-Origin" value="*" />
    <add name="Access-Control-Allow-Headers" value="Content-Type, Accept" />
  </customHeaders>
</httpProtocol>

这是我们提供给客户的JSONP呼叫功能

 $.ajax({
    type: "GET",
    url: "<OUR API>?callback=?",
    data: {
        <OUR VARIABLES>
    },
    beforeSend: setHeader,
    xhrFields: {
        withCredentials: true
    },
    dataType: "jsonp",
    success: function (data) {}
})

function setHeader(xhr) {
  xhr.setRequestHeader('Access-Control-Allow-Origin', '*');
}

这是处理后端的C#代码

public class ClientController : Controller, IRequiresSessionState
{
    //
    // GET: /
    [AcceptVerbs(HttpVerbs.Get | HttpVerbs.Post)]
    public string Index()
    {
         if (System.Web.HttpContext.Current.Session["test-var"] != null)
            return "you are here before " + DateTime.Now.ToString("MM/dd/yyyy HH:mm");
        System.Web.HttpContext.Current.Session["test-var"] = "hi";
        return "you are new";
    }

1 个答案:

答案 0 :(得分:0)

您是否尝试添加属性[WebMethod(EnableSession = true)]

public class ClientController : Controller, IRequiresSessionState
{
//
// GET: /
[AcceptVerbs(HttpVerbs.Get | HttpVerbs.Post)]
[WebMethod(EnableSession = true)]
public string Index()
{
     if (System.Web.HttpContext.Current.Session["test-var"] != null)
        return "you are here before " + DateTime.Now.ToString("MM/dd/yyyy HH:mm");
    System.Web.HttpContext.Current.Session["test-var"] = "hi";
    return "you are new";
}