使用WIF实现单点登录Part I——Windows Identity Foundation介绍及环境搭建 -摘自网络
上个月有一个星期的时间都在研究asp.net mvc统一身份验证及单点登录的实现。经过了一番的探索,最终决定使用微软的Windows Identity Foundation。但是这东西用的人貌似不多,而且中文资料甚少,所以在测试的过程中走了不少弯路,所以写下这一系列文章,希望能对以后要使用的朋友带来一点帮助。
首先先说一下什么是WIF(Windows Identity Foundation)。由于各种历史原因,身份验证和标识的管理一般都比较无规律可循。在软件里加入“身份验证”功能意味着要在你的代码里混进处理底层任务(如验证用户名和密码,与X509证书或类似的证书打交道等)的代码。这样一来就得对基础架构相当依赖,程序很难移植,除非大范围重写。要改变这种情况,使用基于声明的标识(claims-based identity)可以很好的解决这个问题。这个“基于声明的标识”是神马东西我们留到以后再讲,现在您只要知道有这么个东西就行了。Windows Identity Foundation(WIF)是微软的基于声明标识的协议栈。它是一个新的基础技术,可以帮助.NET开发人员利用基于声明的方法来处理身份验证,授权,定制化以及任何与标识相关的任务,而无需编写任何底层代码。
下面来说说WIF环境的搭建。在这里我就走了不少冤枉路。我的悲催经历就不跟大家分享了,直接介绍各种环境该如何搭建:
1、如果你已经安装了VS2012,那么WIF已经包含在了.Net Framework当中,而且版本为WIF4.5。但是注意,WIF4.5只能用于.net 4.5的工程,如果你的asp.net或MVC项目是基于.net 4.5以下的话(即使你是在VS2012里创建的工程),请继续参考以下方法。
2、如果你安装的是VS2010/VS2008,那么你首先要先安装WIF,然后安装WIF SDK。
如果你用的是windows 8,那么系统已经集成了Windows Identity Foundation 3.5了,只要打开控制面板->程序和功能->打开或关闭Windows功能,找到 Windows Identity Foundation 3.5 ,将前面的勾勾上,然后点击“确定”,完事儿以后点“关闭”即可,如图:
这样WIF 3.5就启用成功了。
如果是其它操作系统,请点击这里,找到对应操作系统的exe文件,下载安装即可。
3、接下来安装WIF的SDK,如果你用的是VS2012,那就不用装任何SDK了,因为它都已经集成在.Net Framework 4.5里了。但是你可能需要安装一个VS的插件。打开VS2012,点击工具->扩展和更新,在左边列表里点击“联机”,然后在右上角的搜索框里输入"identity",点击搜索结果里的“Identity and Access Tool”,点击下载按钮,等待下载并安装完成就可以了。这个工具可以让你很快捷地为Web Application,MVC程序或者WCF服务增加一个本地开发STS(Local Development STS),来测试WIF的功能。这个工具的使用我们留到以后再讲。
如果你用的不是VS2012,请点击这里进行下载并安装相应的SDK。
至此,WIF所需的环境就已经搭建好了,下一步我们来通过一个小例子来说明WIF的工作原理。
这里我用的环境是win7+vs2012,vs2010+WIF3.5/4.0的情况请参见这篇文章。
打开VS2012,新建一个基于.net framework4.5的MVC4的工程,取名为WIFTutorial。如下图:
在弹出的“新ASP.NET MVC 4项目”对话框中直接点“确定”。
项目新建完毕后,在“解决方案资源管理器”中,项目名称上点右键,然后选择“Identity and Accee...”,如图:
在“Providers”页签里选择“Use the Local Development STS to test your application”。此时”Local Development STS"页签由不可用变为可用。切换到该页签,将Test claims to issue表格里的第一行的Value列改为“ojlovecd”,如图:
点击“OK”,这个工具将会更改你的web.config文件,我们在解决方案资源管理器里打开web.config看看:
- <?xml version="1.0" encoding="utf-8"?>
- <!--
- 有关如何配置 ASP.NET 应用程序的详细信息,请访问
- http://go.microsoft.com/fwlink/?LinkId=169433
- -->
- <configuration>
- <configSections>
- <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
- <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=5.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
- <span style="color: rgb(255, 0, 0);"><section name="system.identityModel" type="System.IdentityModel.Configuration.SystemIdentityModelSection, System.IdentityModel, Version=4.0.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089" />
- <section name="system.identityModel.services" type="System.IdentityModel.Services.Configuration.SystemIdentityModelServicesSection, System.IdentityModel.Services, Version=4.0.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089" /></span>
- </configSections>
- <connectionStrings>
- <add name="DefaultConnection" connectionString="Data Source=(LocalDb)\v11.0;Initial Catalog=aspnet-WIFTutorial-20130220231745;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|\aspnet-WIFTutorial-20130220231745.mdf" providerName="System.Data.SqlClient" />
- </connectionStrings>
- <appSettings>
- <add key="webpages:Version" value="2.0.0.0" />
- <add key="webpages:Enabled" value="false" />
- <add key="PreserveLoginUrl" value="true" />
- <add key="ClientValidationEnabled" value="true" />
- <add key="UnobtrusiveJavaScriptEnabled" value="true" />
- <span style="color: rgb(255, 0, 0);"><add key="ida:FederationMetadataLocation" value="http://localhost:14419/wsFederationSTS/FederationMetadata/2007-06/FederationMetadata.xml" />
- <add key="ida:Issuer" value="http://localhost:14419/wsFederationSTS/Issue" />
- <add key="ida:ProviderSelection" value="localSTS" /></span>
- </appSettings>
- <span style="color: rgb(255, 0, 0);"><location path="FederationMetadata">
- <system.web>
- <authorization>
- <allow users="*" />
- </authorization>
- </system.web>
- </location></span>
- <system.web>
- <span style="color: rgb(255, 0, 0);"><authorization>
- <deny users="?" />
- </authorization></span>
- <authentication mode="None" />
- <compilation debug="true" targetFramework="4.5" />
- <httpRuntime targetFramework="4.5" requestValidationMode="4.5" />
- <!--Commented by Identity and Access VS Package-->
- <!--<authentication mode="Forms"><forms loginUrl="~/Account/Login" timeout="2880" /></authentication>-->
- <pages>
- <namespaces>
- <add namespace="System.Web.Helpers" />
- <add namespace="System.Web.Mvc" />
- <add namespace="System.Web.Mvc.Ajax" />
- <add namespace="System.Web.Mvc.Html" />
- <add namespace="System.Web.Optimization" />
- <add namespace="System.Web.Routing" />
- <add namespace="System.Web.WebPages" />
- </namespaces>
- </pages>
- </system.web>
- <system.webServer>
- <validation validateIntegratedModeConfiguration="false" />
- <handlers>
- <remove name="ExtensionlessUrlHandler-ISAPI-4.0_32bit" />
- <remove name="ExtensionlessUrlHandler-ISAPI-4.0_64bit" />
- <remove name="ExtensionlessUrlHandler-Integrated-4.0" />
- <add name="ExtensionlessUrlHandler-ISAPI-4.0_32bit" path="*." verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS" modules="IsapiModule" scriptProcessor="%windir%\Microsoft.NET\Framework\v4.0.30319\aspnet_isapi.dll" preCondition="classicMode,runtimeVersionv4.0,bitness32" responseBufferLimit="0" />
- <add name="ExtensionlessUrlHandler-ISAPI-4.0_64bit" path="*." verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS" modules="IsapiModule" scriptProcessor="%windir%\Microsoft.NET\Framework64\v4.0.30319\aspnet_isapi.dll" preCondition="classicMode,runtimeVersionv4.0,bitness64" responseBufferLimit="0" />
- <add name="ExtensionlessUrlHandler-Integrated-4.0" path="*." verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" />
- </handlers>
- <modules>
- <remove name="FormsAuthentication" />
- <add name="WSFederationAuthenticationModule" type="System.IdentityModel.Services.WSFederationAuthenticationModule, System.IdentityModel.Services, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" preCondition="managedHandler" />
- <add name="SessionAuthenticationModule" type="System.IdentityModel.Services.SessionAuthenticationModule, System.IdentityModel.Services, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" preCondition="managedHandler" />
- </modules>
- </system.webServer>
- <runtime>
- <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
- <dependentAssembly>
- <assemblyIdentity name="System.Web.Helpers" publicKeyToken="31bf3856ad364e35" />
- <bindingRedirect oldVersion="1.0.0.0-2.0.0.0" newVersion="2.0.0.0" />
- </dependentAssembly>
- <dependentAssembly>
- <assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35" />
- <bindingRedirect oldVersion="1.0.0.0-4.0.0.0" newVersion="4.0.0.0" />
- </dependentAssembly>
- <dependentAssembly>
- <assemblyIdentity name="System.Web.WebPages" publicKeyToken="31bf3856ad364e35" />
- <bindingRedirect oldVersion="1.0.0.0-2.0.0.0" newVersion="2.0.0.0" />
- </dependentAssembly>
- </assemblyBinding>
- </runtime>
- <entityFramework>
- <defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
- <parameters>
- <parameter value="v11.0" />
- </parameters>
- </defaultConnectionFactory>
- </entityFramework>
- <system.identityModel>
- <identityConfiguration>
- <audienceUris>
- <add value="http://localhost:8007/" />
- </audienceUris>
- <issuerNameRegistry type="System.IdentityModel.Tokens.ConfigurationBasedIssuerNameRegistry, System.IdentityModel, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
- <trustedIssuers>
- <add thumbprint="9B74CB2F320F7AAFC156E1252270B1DC01EF40D0" name="LocalSTS" />
- </trustedIssuers>
- </issuerNameRegistry>
- <certificateValidation certificateValidationMode="None" />
- </identityConfiguration>
- </system.identityModel>
- <span style="color: rgb(255, 0, 0);"><system.identityModel.services>
- <federationConfiguration>
- <cookieHandler requireSsl="false" />
- <wsFederation passiveRedirectEnabled="true" issuer="http://localhost:14419/wsFederationSTS/Issue" realm="http://localhost:8007/" requireHttps="false" />
- </federationConfiguration>
- </system.identityModel.services></span>
- </configuration>
<?xml version="1.0" encoding="utf-8"?> <!-- 有关如何配置 ASP.NET 应用程序的详细信息,请访问 http://go.microsoft.com/fwlink/?LinkId=169433 --> <configuration> <configSections> <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 --> <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=5.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" /> <section name="system.identityModel" type="System.IdentityModel.Configuration.SystemIdentityModelSection, System.IdentityModel, Version=4.0.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089" /> <section name="system.identityModel.services" type="System.IdentityModel.Services.Configuration.SystemIdentityModelServicesSection, System.IdentityModel.Services, Version=4.0.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089" /> </configSections> <connectionStrings> <add name="DefaultConnection" connectionString="Data Source=(LocalDb)\v11.0;Initial Catalog=aspnet-WIFTutorial-20130220231745;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|\aspnet-WIFTutorial-20130220231745.mdf" providerName="System.Data.SqlClient" /> </connectionStrings> <appSettings> <add key="webpages:Version" value="2.0.0.0" /> <add key="webpages:Enabled" value="false" /> <add key="PreserveLoginUrl" value="true" /> <add key="ClientValidationEnabled" value="true" /> <add key="UnobtrusiveJavaScriptEnabled" value="true" /> <add key="ida:FederationMetadataLocation" value="http://localhost:14419/wsFederationSTS/FederationMetadata/2007-06/FederationMetadata.xml" /> <add key="ida:Issuer" value="http://localhost:14419/wsFederationSTS/Issue" /> <add key="ida:ProviderSelection" value="localSTS" /> </appSettings> <location path="FederationMetadata"> <system.web> <authorization> <allow users="*" /> </authorization> </system.web> </location> <system.web> <authorization> <deny users="?" /> </authorization> <authentication mode="None" /> <compilation debug="true" targetFramework="4.5" /> <httpRuntime targetFramework="4.5" requestValidationMode="4.5" /> <!--Commented by Identity and Access VS Package--> <!--<authentication mode="Forms"><forms loginUrl="~/Account/Login" timeout="2880" /></authentication>--> <pages> <namespaces> <add namespace="System.Web.Helpers" /> <add namespace="System.Web.Mvc" /> <add namespace="System.Web.Mvc.Ajax" /> <add namespace="System.Web.Mvc.Html" /> <add namespace="System.Web.Optimization" /> <add namespace="System.Web.Routing" /> <add namespace="System.Web.WebPages" /> </namespaces> </pages> </system.web> <system.webServer> <validation validateIntegratedModeConfiguration="false" /> <handlers> <remove name="ExtensionlessUrlHandler-ISAPI-4.0_32bit" /> <remove name="ExtensionlessUrlHandler-ISAPI-4.0_64bit" /> <remove name="ExtensionlessUrlHandler-Integrated-4.0" /> <add name="ExtensionlessUrlHandler-ISAPI-4.0_32bit" path="*." verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS" modules="IsapiModule" scriptProcessor="%windir%\Microsoft.NET\Framework\v4.0.30319\aspnet_isapi.dll" preCondition="classicMode,runtimeVersionv4.0,bitness32" responseBufferLimit="0" /> <add name="ExtensionlessUrlHandler-ISAPI-4.0_64bit" path="*." verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS" modules="IsapiModule" scriptProcessor="%windir%\Microsoft.NET\Framework64\v4.0.30319\aspnet_isapi.dll" preCondition="classicMode,runtimeVersionv4.0,bitness64" responseBufferLimit="0" /> <add name="ExtensionlessUrlHandler-Integrated-4.0" path="*." verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" /> </handlers> <modules> <remove name="FormsAuthentication" /> <add name="WSFederationAuthenticationModule" type="System.IdentityModel.Services.WSFederationAuthenticationModule, System.IdentityModel.Services, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" preCondition="managedHandler" /> <add name="SessionAuthenticationModule" type="System.IdentityModel.Services.SessionAuthenticationModule, System.IdentityModel.Services, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" preCondition="managedHandler" /> </modules> </system.webServer> <runtime> <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1"> <dependentAssembly> <assemblyIdentity name="System.Web.Helpers" publicKeyToken="31bf3856ad364e35" /> <bindingRedirect oldVersion="1.0.0.0-2.0.0.0" newVersion="2.0.0.0" /> </dependentAssembly> <dependentAssembly> <assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35" /> <bindingRedirect oldVersion="1.0.0.0-4.0.0.0" newVersion="4.0.0.0" /> </dependentAssembly> <dependentAssembly> <assemblyIdentity name="System.Web.WebPages" publicKeyToken="31bf3856ad364e35" /> <bindingRedirect oldVersion="1.0.0.0-2.0.0.0" newVersion="2.0.0.0" /> </dependentAssembly> </assemblyBinding> </runtime> <entityFramework> <defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework"> <parameters> <parameter value="v11.0" /> </parameters> </defaultConnectionFactory> </entityFramework> <system.identityModel> <identityConfiguration> <audienceUris> <add value="http://localhost:8007/" /> </audienceUris> <issuerNameRegistry type="System.IdentityModel.Tokens.ConfigurationBasedIssuerNameRegistry, System.IdentityModel, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"> <trustedIssuers> <add thumbprint="9B74CB2F320F7AAFC156E1252270B1DC01EF40D0" name="LocalSTS" /> </trustedIssuers> </issuerNameRegistry> <certificateValidation certificateValidationMode="None" /> </identityConfiguration> </system.identityModel> <system.identityModel.services> <federationConfiguration> <cookieHandler requireSsl="false" /> <wsFederation passiveRedirectEnabled="true" issuer="http://localhost:14419/wsFederationSTS/Issue" realm="http://localhost:8007/" requireHttps="false" /> </federationConfiguration> </system.identityModel.services> </configuration>
其中标红色的部分即为工具自动生成的。同时此工具还会在你的程序跟目录下新建了一个名为FederationMetadata的目录,通过在解决方案资源管理器中点击“显示所有文件”按钮就可以看到了。
然后我们按F5,结果报错了,如图:
看提示,是因为要启动LocalSTS必须要用管理员身份来运行。好吧,关掉VS,以管理员身份再次打开,载入项目,再次按F5运行程序,此时看到右下角托盘处LocalSTS已经运行了,而打开的主页中,右上角显示用户ojlovecd已经处于登录状态。
这一连串操作到底都干了什么?
首先,刚才已经说到,Identity and Access Tool先修改了web.config,增加了WIF要用到的一些配置信息,在程序启动的时候,首先一并启动LocalSTS,这个LocalSTS起什么作用?由于web.config中配置了authorization节点,拒绝匿名用户访问,因此在你的程序运行起来以后,对程序的请求信息被重定向到LocalSTS,向LocalSTS请求身份信息,LocalSTS收到请求后,返回身份信息。那么返回的身份信息怎么来的?没错,就是在Identity and Access Tool工具里的第三个页签里配置的。
至此,我们没有编写任何代码,就已经实现了一个很简单的身份验证功能,关于此例子的详细讲解,以及我们要重点介绍的WIF原理及单点登录的实现,我们将留到下面的文章进行讲解。