asp.net編寫webservice時,默認情況下是不支持session的

在使用asp.net編寫webservice時,默認情況下是不支持session的,但我們可以把WebMethod的EnableSession選項設爲true來顯式的打開它,請看以下例子:

1 新建網站WebSite

2 新建web服務WebService.asmx,它具有以下兩個方法:

C#-Code:

[WebMethod(EnableSession = true)]

public string Login(string name)

{

    Context.Session["name"] = name;

    return name;

}

[WebMethod(EnableSession = true)]

public string GetName()

{

    if (Context.Session["name"] != null)

        return Context.Session["name"].ToString();

    else

        return "";

}

3 添加asp.net頁面SessionInWebservice.aspx

ASP.NET-Code:

<form id="form1" runat="server">

    <div>

        <asp:TextBox ID="txtName" runat="server"></asp:TextBox>

        <asp:Button ID="btnLogin" runat="server"

            Text="Login" OnClick="btnLogin_Click" />

    </div>

    <div>

        <asp:Button ID="btnGetName" runat="server"

            Text="GetName" OnClick="btnGetName_Click" />

        <asp:Label ID="lblName" runat="server" Text="Label"></asp:Label>

    </div>

</form>

SessionInWebservice.aspx.cs

C#-Code:

protected void btnLogin_Click(object sender, EventArgs e)

{

    WebService ws = new WebService();   

    ws.Login(txtName.Text);

}

protected void btnGetName_Click(object sender, EventArgs e)

{

    WebService ws = new WebService();

    lblName.Text = ws.GetName();

}

問題似乎到此結束了,按Login按鈕記錄用戶名以後,再按GetName就可以獲取到剛纔輸入的名字。

但如果我們另外新建一個website,並添加web引用來調用剛纔編寫的webservice,問題就出來了,GeName方法並沒有獲取到我們剛纔登錄的用戶名(如果是在winform中調用該方法,也會出現同樣的問題)。莫非這個方法行不通了?

其實不然,我們給該WebService的CookieContainer賦值就可以了,修改SessionInWebservice.aspx.cs 的代碼:

C#-Code:

private static System.Net.CookieContainer cookieContainer

    = new System.Net.CookieContainer();

protected void btnLogin_Click(object sender, EventArgs e)

{

    localhost.WebService ws = new localhost.WebService();

    ws.CookieContainer = cookieContainer;

    ws.Login(txtName.Text);

}

protected void btnGetName_Click(object sender, EventArgs e)

{

    localhost.WebService ws = new localhost.WebService();

    ws.CookieContainer = cookieContainer;

    lblName.Text = ws.GetName();

}

請注意:Login方法和GetName方法必須指定同一個CookieContainer,因此在這裏我們使用了靜態變量。

但如果是在不同的頁面中調用該webservice,問題依舊存在,因此我們需要重新修改代碼,通過編寫新類繼承上面的webservice,並給CookieContainer賦值就可以解決該問題了:

C#-Code:

public class WebService1:localhost.WebService

{

    private static System.Net.CookieContainer cookieContainer;

    static WebService1()

    {

        cookieContainer = new System.Net.CookieContainer();

    }

    public WebService1()

    {

        this.CookieContainer = cookieContainer;

    }

}

調用的時候也不需要重新給CookieContainer賦值了:

C#-Code:

protected void btnLogin_Click(object sender, EventArgs e)

{

    WebService1 ws = new WebService1();

    ws.Login(txtName.Text);

}

protected void btnGetName_Click(object sender, EventArgs e)

{

    WebService1 ws = new WebService1();

    lblName.Text = ws.GetName();

}

I’ve come across a number of developers who have run into this issue when trying to call a web service in Visual Studio 2005 Beta 2. The symptom of the problem is that when trying to call a web service that is running on the "ASP.NET Development Server" from another application, the web service throws a WebException with the message: “The request failed with HTTP status 401: Unauthorized.”

First and foremost, this issue is not related to debugging. If you run the application outside of Visual Studio, you’ll see the same result. Also noteworthy to mention is that when you run the web service by itself, everything works fine. It’s only when you call the web service from another application that it fails.

Why do I get this error?

The reason for this error is that by default, a web service project in Visual Studio 2005 Beta 2 required NTLM authentication. When you call the web service from another application, the application tries to call into the web service with anonymous access and fails. But, when you call the web service from the web service test page in Internet Explorer, your credentials are passed from IE to the web service which means that the call succeeds.

 How do I fix this error?

There are two ways to fix this error.

1. Disable NTLM Authentication in Project Properties
If your web service is not intended to require authentication, the easiest way to fix this error is to turn off NTML authentication in a web service project. To do this, right click on the web service project and select “Property Pages”. On the “Start Options” page, uncheck “NTLM Authentication”. NOTE: disabling this option has security implications if you are running the web service in a Terminal Server environment. If the web service is on your local machine and it’s not being used in a Terminal Server environment, then you should be fine.

2. Pass User's Credentials from Calling Application to Web Service
If your web service is intended to require authentication and not anonymous access, then pass the credentials of the user from the application to the web service. Here is an example:

Samples.TimeWebService ws = new Samples.TimeWebService();
ws.Credentials = System.Net.CredentialCache.DefaultCredentials;
string currentTime = ws.GetCurrentTime();

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章