前面发布了一篇ASP.NET提供哪些身份验证方式?,那么如何在ASP.NET中使用Windows身份验证呢?
(1)设置Web.config文件
Windows身份验证只适用于放在受控环境里的网站。在应用程序中必须正确的设置web.config文件才能够在ASP.NET应用程序里使用Windows身份验证。
在Visual Studio.NET中打开一个项目,在“解决方案资源管理器”中,双击web.config,打开web.config文件,在里面加入以下设置:

如上图修改了web.config之后,再次访问web.config浏览器就会弹出下图对话框:

(2)全局验证函数
当用户访问页面通过了Windows身份验证,ASP.NET就会处罚Global.asax文件中的WindowsAuthentication_OnAuthenticate事件,在这个事件,可以实现System.Security.Principal.Iprincipal接口的类附加到请求上,也可以用已有类的对象附在请求上,比如将一个WindowsPrincipal类的对象附在请求上。这样,在各个页面都可以通过访问User类来访问用户相关信息。
例如:在Visual Studio.NET中打开Global.asax文件,在代码中增加如下图函数,就将一个WindowsPrincipal类的对象附在了User对象上。

(3)在页面中编写代码
.NET FrameWork提供了使用Windows验证方式实现安全所需要的类。这些类被包括在System.Web.Security和System.Security.Principal这两个命名空间中。
引入这两个命名空间后,就可以使用适当的安全类来访问当前的用户设置。当使用Windows身份验证时,ASP.NET将WindowsPrincipal类的对象附加到当前请求。该对象有URL身份验证使用。应用程序也可以以编程方式来确定请求标识是否在给定角色中。在页面代码中,可以用User对象来查看当前通过验证的用户名字和验证类型等。下面的代码实现了通过Windows身份验证之后,显示出用户名及验证方式。


哈,这个是学.NET的最基本的知识啦
The grandstand end was provided any time Ameobis towering header setup Nolan, who could scarcely miss from practically underneath the crossbar for their hattrick.
Darren Bents injurytime strike was welcomed by almost complete silence as the the greater part of Sunderland supporters remaining before the whistle.
我来踩踩!
下载不了 奶奶的
路过看看呢
我来踩踩!
不错,顶一下
Thanks for you sharing.That is good article.I like it.
I read with great interest.Thanks for you sharing.
This information is useful to us.That is very kind of you to write this share for us, thanks a lot.
It’s really a nice and helpful piece of information. I’m glad that you shared this helpful info with us. Please keep us informed like this. Thanks for sharing.
I was suggested this website by my cousin. I’m not sure whether this post is written by him as no one else know such detailed about my difficulty. You’re incredible! Thanks!
Thanks for sharing. That is a nice article.
Best free SWF Converter Mac is a powerful Mac SWF converter tool, that can help you convert SWF on Mac OS X.
Spyker saves Saab from certain death
Do you like Michael jordan shoes? http://www.jordanshoesebuy.com
SWF Converter Mac is a powerful Mac SWF converter tool,
Really smart.Anyway i love to use twitter in my site.
我的站:http://www.bkhack.com
http://www.8color.net
找友情链接哦