惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

罗磊的独立博客
Cisco Talos Blog
Cisco Talos Blog
C
Check Point Blog
博客园_首页
Recent Commits to openclaw:main
Recent Commits to openclaw:main
Martin Fowler
Martin Fowler
Recorded Future
Recorded Future
S
Security @ Cisco Blogs
L
LINUX DO - 最新话题
博客园 - 司徒正美
P
Privacy International News Feed
G
Google Developers Blog
I
Intezer
Hacker News - Newest:
Hacker News - Newest: "LLM"
博客园 - 聂微东
The GitHub Blog
The GitHub Blog
C
Cybersecurity and Infrastructure Security Agency CISA
www.infosecurity-magazine.com
www.infosecurity-magazine.com
Scott Helme
Scott Helme
K
Kaspersky official blog
I
InfoQ
Y
Y Combinator Blog
T
The Blog of Author Tim Ferriss
Webroot Blog
Webroot Blog
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
大猫的无限游戏
大猫的无限游戏
D
Docker
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
W
WeLiveSecurity
Microsoft Azure Blog
Microsoft Azure Blog
Spread Privacy
Spread Privacy
量子位
H
Hacker News: Front Page
Simon Willison's Weblog
Simon Willison's Weblog
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
SecWiki News
SecWiki News
S
Security Affairs
Latest news
Latest news
人人都是产品经理
人人都是产品经理
C
CERT Recently Published Vulnerability Notes
S
Security Archives - TechRepublic
V
Visual Studio Blog
T
Troy Hunt's Blog
S
Secure Thoughts
F
Fortinet All Blogs
V
V2EX
The Register - Security
The Register - Security
J
Java Code Geeks
MongoDB | Blog
MongoDB | Blog
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO

博客园 - 杨林

IIs一些配置 扩大传输限制 exchange file 关于VS2005 无法使用切换到设计视图的解决方法 SendEmail - 杨林 - 博客园 web导出excel格式问题 Form身份验证 - 杨林 - 博客园 在ASP.NET 2.0中使用Membership sql T 设计模式之状态模式 sqlserver2005学习笔记 - 杨林 共享内存进程之间 - 杨林 ComparaCarAdapter sql 利用索引优化性能 - 杨林 SQL 2005新增的几个函数之学习 - 杨林 Thread pool - 杨林 web service web service - 杨林 多线程分析 js Function.call
使用ASP.Net Forms模式实现WebService身份验证
杨林 · 2008-06-02 · via 博客园 - 杨林

    在安全性要求不是很高的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,就这么简单!