会话启用和 cookieless 与身份验证有什么关系? [英] What do session enabling and cookieless have to do with authentication?

查看:21
本文介绍了会话启用和 cookieless 与身份验证有什么关系?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,我们在这个问题上停留了大约 2 天,但还没有任何线索.

我们有一个 .asmx 网络服务(配置为 cookieless="true"):

 [WebMethod(EnableSession =true)][ScriptMethod(ResponseFormat = ResponseFormat.Json)]公共字符串 SimplestWebService(){return "最简单的网络服务工程";}

我们尝试从两者中调用它:c#表单应用程序,如下所示:
<预><代码>private void button1_Click(object sender, EventArgs e){字符串 url = "

更具体地说,以下调用对我有用,因为我无法获得在页面中看到的第一个示例对我有用.它会在 URL 中使用您的 SessionId 正确生成服务的 URL,因此服务器可以正确查找您的会话...

Sys.Net.WebServiceProxy.invoke(‘WebService.asmx’, ‘HelloWorld’, false,{}, SucceededCallback, null,"User Context",1000000);

有关此调用的信息可在以下位置找到:http://msdn.microsoft.com/en-us/library/bb383814.aspx

很遗憾,我在 MSDN 站点上找不到任何我能找到的场景……上面的页面是我唯一能找到的,它有一个有效的代码示例……

丰富

Ok, we're stuck on this one for like 2 days and don't have a clue yet.

We have a .asmx web service (configured as cookieless="true"):

    [WebMethod(EnableSession =true)]
    [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
    public string SimplestWebService()
    {
        return "Simplest web service works";
    }

And we try to call it from both: c# form application, like so:


 private void button1_Click(object sender, EventArgs e)
        {
            string url = "http://192.168.5.223:8989/MyAPI.asmx/SimplestWebService";
            HttpWebRequest req = (HttpWebRequest) WebRequest.Create(url);
            req.Method = "POST";
            req.ContentType = "application/json; charset=utf-8";
            req.UseDefaultCredentials = true;
            req.PreAuthenticate = false;
            req.ContentLength = 0;
            var result = new StringBuilder();
            using (HttpWebResponse res = (HttpWebResponse) req.GetResponse())
            {
                StreamReader sr = new StreamReader(res.GetResponseStream());
                result.Append(sr.ReadToEnd());
            }
            label1.Text = result.ToString();
        }


And Jquery, like so:


 function simplestMethod() {
var callParams = "{}" $.ajax({ type: "POST", url: "http://192.168.5.223:8989/MyAPI.asmx/SimplestWebService", data: callParams, contentType: "application/json; charset=utf-8", dataType: "json", success: function (data) { alert(data.d); }, error: function (XMLHttpRequest, textStatus, errorThrown) { alert("Error Occured!" + " | " + XMLHttpRequest.responseText + " | " + textStatus + " | " + errorThrown); } }); }

The Forms application yields the following error:
The remote server returned an error: (401) Unauthorized.
But when removing the contentType line - returns valid output- but in XML (instead of json.. which is obvious).

The Jquery (which is on the same machine as the web service) gives:

Both work perfectly when we use them not in cookieless mode or with sessioneabled=false. But that's not what we need.
I should also mention we work with IIS 6.0 & .NET 4.0. One last thing - If we go directly to the web service link (using a browser), it works but returns XML (instead of json).

Any advice as to how to make this work with cookieless=true and EnableSession=true would be much appreciated.

Thank you.

解决方案

I got this to work after finding some code at:

http://ardentdev.com/cookieless-session-with-aspnet-ajax-and-web-services/

More specifically, the following call worked for me as I couldn't get the first example I saw in the page to work for me. It properly generates the URL to the service with your SessionId in the URL, so the server can properly lookup your session...

Sys.Net.WebServiceProxy.invoke(‘WebService.asmx’, ‘HelloWorld’, false,{}, SucceededCallback, null,"User Context",1000000);

Info on this call can be found at: http://msdn.microsoft.com/en-us/library/bb383814.aspx

It's really sad that this scenario is not covered anywhere that I could find, on an MSDN site...The above page was the only thing I could find, that had a working code sample...

Rich

这篇关于会话启用和 cookieless 与身份验证有什么关系?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆