使用ASP.Net Forms模式實現WebService身分識別驗證

來源:互聯網
上載者:User
在安全性要求不是很高的ASP.Net程式中,基於Forms的身分識別驗證是經常使用的一種方式,而如果需要對WebService進行身分識別驗證,最常用的可能是基於Soap 標題的自訂身分識別驗證方式。如果對兩者做一下比較的話,顯然,基於Forms的驗證方式更加方便易用,能否將Forms驗證方式應用到WebService中去呢? 
    從理論上講,使用基於Forms的方式對WebService進行身分識別驗證是可行的,但是使用過程中會存在以下兩個問題:
1.基於Forms的驗證方式同時也是基於Cookie的驗證方式,在使用瀏覽器時,這個問題是不需要我們考慮的。但對於使用WebService的應用程式來說,預設是不能儲存Cookie的,需要我們自己去做這個工作。
2.WebService既然是一個A2A(Application To Application)應用程式,使用Web表單進行身分識別驗證顯然不太合適,而且,這將不可避免的造成人機互動,使WebService的應用大打折扣。
  接下來,我們就分步解決這兩個問題:
1.Cookie的儲存問題
    WebService的用戶端代理類有一個屬性CookieContainer可用於設定或擷取Cookie集合,儲存Cookie的任務就交給他了:

System.Net.CookieContainer cookieContainer = new System.Net.CookieContainer();

MyService.WebService service = new App.MyService.WebService();
service.CookieContainer = cookieContainer;


2.我們不想使用Web表單進行身分識別驗證,幸運的是,ASP.Net表單驗證中的表單頁(即Web.config檔案中 forms 元素內的loginUrl)同樣可以指定為WebService檔案。
    我們建立一個專門用作身分識別驗證的Web服務,暫且命名為Login.asmx,然後讓 loginUrl 等於 “Login.asmx”,當然,還需要在Web.config檔案中的 authorization 節中禁止匿名訪問(否則我們可就白忙活了),完成配置後的Web.config檔案如下:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
    <system.web>
       <compilation debug="false" />
      <authentication mode="Forms">
        <forms name="MyService" loginUrl="Login.asmx"></forms>
      </authentication>
      <authorization >
        <deny users="?"/>
      </authorization>
    </system.web>
</configuration>

    其實我們並不想在未通過身分識別驗證時讓瀏覽器轉向到Login.asmx,對於使用WebService的客戶程式來說,真正的實惠在於:可以匿名訪問Login.asmx中的方法(當然我們也可以把Login.asmx放在單獨的目錄中,然後允許對該目錄的匿名訪問來達個這個目的,但我覺得還是用loginUrl更優雅一些)。
    接下來,我們為Login.asmx添加用於身分識別驗證的WebMethod:

[WebMethod]
public bool Check(string userName,string password)
{
    if (userName == "aaaaaa" && password == "123456")//添加驗證邏輯
    {
        System.Web.Security.FormsAuthentication.SetAuthCookie(userName, false);
        return true;
    }
    else
    {
        return false;
    }
}

    最後一步工作就是:讓客戶程式中的WebService執行個體與Login執行個體共用CookieContainer。

class Sample
{
    System.Net.CookieContainer cookieContainer = new System.Net.CookieContainer();

    public void Login()
    {
        MyServiceLogin.Login login = new App.MyServiceLogin.Login();
        login.CookieContainer = cookieContainer;
        login.Check("aaaaaa", "123456");                       
    }

    public void ShowHelloWorld()
    {
        MyService.WebService service = new App.MyService.WebService();
        service.CookieContainer = cookieContainer;

        Console.WriteLine(service.HelloWorld());
    }
}

    Login()以後再ShowHelloWorld(),你是否看到了我們熟悉的“Hello World”?Ok,就這麼簡單!

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.