IIS7入门之旅:(2)如何实现和加载自定义的Basic Authentication模块

察看原文,请参阅:http://learn.iis.net/page.aspx/170/developing-a-module-using-net/

前言:

相对于IIS6,在IIS7中,最为显著的变化之一就是功能模块的组件化,也就是说可以根据自己的需要,加载或者拆卸组件,从而最大限度减轻server的负担。同时对于一些组件,也提供了相应的接口,用户可以自己重写这些组件,从而提供更加个性化的功能。本文介绍了如何自定义一个进行basic authentication的模块,并进行加载,从而实现第3方的basic authentication验证模式。

正文:

准备工作:

1 将anonymous authentication mode disable 掉(被修改对象:applicationhost.config)。by default, it's enabled.

2 允许相应的authentication mode 能够被overriden(被修改对象:applicationhost.config)。by defaut, it's denied!

< sectionGroup name = " authentication " >
                    
< section name = " anonymousAuthentication "  overrideModeDefault = " Deny "   />
                    
< section name = " basicAuthentication "  overrideModeDefault = " Deny "   />
                    
< section name = " clientCertificateMappingAuthentication "  overrideModeDefault = " Deny "   />
                    
< section name = " digestAuthentication "  overrideModeDefault = " Deny "   />
                    
< section name = " iisClientCertificateMappingAuthentication "  overrideModeDefault = " Deny "   />
                    
< section name = " windowsAuthentication "  overrideModeDefault = " Deny "   />
                
</ sectionGroup >

应将其改为:

< sectionGroup name = " authentication " >
                    
< section name = " anonymousAuthentication "  overrideModeDefault = " Allow "   />
                    
< section name = " basicAuthentication "  overrideModeDefault = " Deny "   />
                    
< section name = " clientCertificateMappingAuthentication "  overrideModeDefault = " Deny "   />
                    
< section name = " digestAuthentication "  overrideModeDefault = " Deny "   />
                    
< section name = " iisClientCertificateMappingAuthentication "  overrideModeDefault = " Deny "   />
                    
< section name = " windowsAuthentication "  overrideModeDefault = " Deny "   />
                
</ sectionGroup >

 

说明:overridenModeDefault的Allow和Deny的区别:

1 overrideModeDefault=Deny (by default)

在此情况下,对于website所做的有关authentication的设置的修改,都被保存至applicationhost.config的<location> section中,如下所示:

< configuration >
..
..
< location path = " CustAuth " >
        
< system.webServer >
            
< security >
                
< authentication >
                    
< digestAuthentication enabled = " true "   />
                    
< windowsAuthentication enabled = " false "   />
                    
< anonymousAuthentication enabled = " true "   />
                
</ authentication >
            
</ security >
        
</ system.webServer >
    
</ location >
</ configuration >

 

2 overrideModeDefault=Allow

在此情况下,对于website所做的有关authentication的设置的修改,都被保存至web.config中,如下所示: 

<? xml version = " 1.0 "  encoding = " UTF-8 " ?>

< configuration >
    
< appSettings  />
    
< connectionStrings  />
    
< system.web >         
        
    
</ system.web >
    
< system.webServer >
        
< security >
            
< authentication >
                
< anonymousAuthentication enabled = " false "   />
            
</ authentication >
        
</ security >
    
</ system.webServer >
</ configuration >

可以看出,因为我们已经将anonymouAuthentication的orverrideModeDefault设置为了Allow,所在在website中的对于anonymouse authentication的修改都被记录在了 web.config中。

 

 正式工作:

1.编写自定义的basic authentication module,将以下保存为 某个cs文件至 App_Code中。

这部分对于IIS验证及.net framework认识有较高要求。源代码如下:

Code

 

2 再添加一个简单的测试页面如default.aspx。然后发布website

3 将anonymous authentication disabled掉。

     因为前面我们已经将其overrideModeDefault设置为Allow.因此此设置将会写入website下的web.config中

4 添加自定义的basic authentication module (修改对象:web.config)

     修改后的web.config如下:

Code

 

5 请求website下的resource,如default.aspx,basic authentication window prompt for username&password,然后自定义的basic authentication module将会被调用验证该credential.
IIS7入门之旅:(2)如何实现和加载自定义的Basic Authentication模块

你可能感兴趣的:(Authentication)