夏雷

积极人生,努力加油!
使用Forms实现WebService身份验证
 在安全性要求不是很高的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的任务就交给他了:

使用Forms实现WebService身份验证(图一)System.Net.CookieContainer cookieContainer = new System.Net.CookieContainer();
使用Forms实现WebService身份验证(图一)
使用Forms实现WebService身份验证(图一)MyService.WebService service = new App.MyService.WebService();
使用Forms实现WebService身份验证(图一)service.CookieContainer = cookieContainer;

2.我们不想使用Web表单进行身份验证,幸运的是,ASP.Net表单验证中的表单页(即Web.config文件中 forms 元素内的loginUrl)同样可以指定为WebService文件。
    我们创建一个专门用作身份验证的Web服务,暂且命名为Login.asmx,然后让 loginUrl 等于 “Login.asmx”,当然,还需要在Web.config文件中的 authorization 节中禁止匿名访问(否则我们可就白忙活了),完成配置后的Web.config文件如下:

使用Forms实现WebService身份验证(图一)<?xml version="1.0" encoding="utf-8"?>
使用Forms实现WebService身份验证(图一)<configuration>
使用Forms实现WebService身份验证(图一)    <system.web>
使用Forms实现WebService身份验证(图一)       <compilation debug="false" />
使用Forms实现WebService身份验证(图一)      <authentication mode="Forms">
使用Forms实现WebService身份验证(图一)        <forms name="MyService" loginUrl="Login.asmx"></forms>
使用Forms实现WebService身份验证(图一)      </authentication>
使用Forms实现WebService身份验证(图一)      <authorization >
使用Forms实现WebService身份验证(图一)        <deny users="?"/>
使用Forms实现WebService身份验证(图一)      </authorization>
使用Forms实现WebService身份验证(图一)    </system.web>
使用Forms实现WebService身份验证(图一)</configuration>
使用Forms实现WebService身份验证(图一)

    其实我们并不想在未通过身份验证时让浏览器转向到Login.asmx,对于使用WebService的客户程序来说,真正的实惠在于:可以匿名访问Login.asmx中的方法(当然我们也可以把Login.asmx放在单独的目录中,然后允许对该目录的匿名访问来达个这个目的,但我觉得还是用loginUrl更优雅一些)。
    接下来,我们为Login.asmx添加用于身份验证的WebMethod:

使用Forms实现WebService身份验证(图一)[WebMethod]
使用Forms实现WebService身份验证(图一)public bool Check(string userName,string password)
使用Forms实现WebService身份验证(图二)使用Forms实现WebService身份验证(图三)使用Forms实现WebService身份验证(图四){
使用Forms实现WebService身份验证(图五)    if (userName == "aaaaaa" && password == "123456")//添加验证逻辑
使用Forms实现WebService身份验证(图六)使用Forms实现WebService身份验证(图七)    使用Forms实现WebService身份验证(图四){
使用Forms实现WebService身份验证(图五)        System.Web.Security.FormsAuthentication.SetAuthCookie(userName, false);
使用Forms实现WebService身份验证(图五)        return true;
使用Forms实现WebService身份验证(图八)    }
使用Forms实现WebService身份验证(图五)    else
使用Forms实现WebService身份验证(图六)使用Forms实现WebService身份验证(图七)    使用Forms实现WebService身份验证(图四){
使用Forms实现WebService身份验证(图五)        return false;
使用Forms实现WebService身份验证(图八)    }
使用Forms实现WebService身份验证(图九)}

    最后一步工作就是:让客户程序中的WebService实例与Login实例共享CookieContainer。

使用Forms实现WebService身份验证(图一)class Sample
使用Forms实现WebService身份验证(图二)使用Forms实现WebService身份验证(图三)使用Forms实现WebService身份验证(图四){
使用Forms实现WebService身份验证(图五)    System.Net.CookieContainer cookieContainer = new System.Net.CookieContainer();
使用Forms实现WebService身份验证(图五)
使用Forms实现WebService身份验证(图五)    public void Login()
使用Forms实现WebService身份验证(图六)使用Forms实现WebService身份验证(图七)    使用Forms实现WebService身份验证(图四){
使用Forms实现WebService身份验证(图五)        MyServiceLogin.Login login = new App.MyServiceLogin.Login();
使用Forms实现WebService身份验证(图五)        login.CookieContainer = cookieContainer;
使用Forms实现WebService身份验证(图五)        login.Check("aaaaaa", "123456");                       
使用Forms实现WebService身份验证(图八)    }
使用Forms实现WebService身份验证(图五)
使用Forms实现WebService身份验证(图五)    public void ShowHelloWorld()
使用Forms实现WebService身份验证(图六)使用Forms实现WebService身份验证(图七)    使用Forms实现WebService身份验证(图四){
使用Forms实现WebService身份验证(图五)        MyService.WebService service = new App.MyService.WebService();
使用Forms实现WebService身份验证(图五)        service.CookieContainer = cookieContainer;
使用Forms实现WebService身份验证(图五)
使用Forms实现WebService身份验证(图五)        Console.WriteLine(service.HelloWorld());
使用Forms实现WebService身份验证(图八)    }
使用Forms实现WebService身份验证(图九)}

    Login()以后再ShowHelloWorld(),你是否看到了我们熟悉的“Hello World”?Ok,就这么简单!

posted on 2008-10-27 15:01  夏雷  阅读(310)  评论(0编辑  收藏  举报