首先先說一下什麼是WIF(Windows Identity Foundation)。因爲各類歷史緣由,身份驗證和標識的管理通常都比較無規律可循。在軟件里加入「身份驗證」功能意味着要在你的代碼裏混進處理底層任務(如驗證用戶名和密碼,與X509證書或相似的證書打交道等)的代碼。這樣一來就得對基礎架構至關依賴,程序很難移植,除非大範圍重寫。要改變這種狀況,使用基於聲明的標識(claims-based identity)能夠很好的解決這個問題。這個「基於聲明的標識」是神馬東西咱們留到之後再講,如今您只要知道有這麼個東西就好了。Windows Identity Foundation(WIF)是微軟的基於聲明標識的協議棧。它是一個新的基礎技術,能夠幫助.NET開發人員利用基於聲明的方法來處理身份驗證,受權,定製化以及任何與標識相關的任務,而無需編寫任何底層代碼。html
下面來講說WIF環境的搭建。在這裏我就走了很多冤枉路。個人悲催經歷就不跟你們分享了,直接介紹各類環境該如何搭建:web
一、若是你已經安裝了VS2012,那麼WIF已經包含在了.Net Framework當中,並且版本爲WIF4.5。可是注意,WIF4.5只能用於.net 4.5的工程,若是你的asp.net或MVC項目是基於.net 4.5如下的話(即便你是在VS2012裏建立的工程),請繼續參考如下方法。windows
二、若是你安裝的是VS2010/VS2008,那麼你首先要先安裝WIF,而後安裝WIF SDK。api
若是你用的是windows 8,那麼系統已經集成了Windows Identity Foundation 3.5了,只要打開控制面板->程序和功能->打開或關閉Windows功能,找到 Windows Identity Foundation 3.5 ,將前面的勾勾上,而後點擊「肯定」,完事兒之後點「關閉」便可,如圖:cookie
這樣WIF 3.5就啓用成功了。架構
若是是其它操做系統,請點擊這裏,找到對應操做系統的exe文件,下載安裝便可。app
三、接下來安裝WIF的SDK,若是你用的是VS2012,那就不用裝任何SDK了,由於它都已經集成在.Net Framework 4.5裏了。可是你可能須要安裝一個VS的插件。打開VS2012,點擊工具->擴展和更新,在左邊列表裏點擊「聯機」,而後在右上角的搜索框裏輸入"identity",點擊搜索結果裏的「Identity and Access Tool」,點擊下載按鈕,等待下載並安裝完成就能夠了。這個工具可讓你很快捷地爲Web Application,MVC程序或者WCF服務增長一個本地開發STS(Local Development STS),來測試WIF的功能。這個工具的使用咱們留到之後再講。asp.net
若是你用的不是VS2012,請點擊這裏進行下載並安裝相應的SDK。less
至此,WIF所需的環境就已經搭建好了,下一步咱們來經過一個小例子來講明WIF的工做原理。ide
這裏我用的環境是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>
-
- <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:#ff0000;"><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:#ff0000;"><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:#ff0000;"><location path="FederationMetadata">
- <system.web>
- <authorization>
- <allow users="*" />
- </authorization>
- </system.web>
- </location></span>
- <system.web>
- <span style="color:#ff0000;"><authorization>
- <deny users="?" />
- </authorization></span>
- <authentication mode="None" />
- <compilation debug="true" targetFramework="4.5" />
- <httpRuntime targetFramework="4.5" requestValidationMode="4.5" />
-
-
- <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:#ff0000;"><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>
其中標紅色的部分即爲工具自動生成的。同時此工具還會在你的程序跟目錄下新建了一個名爲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工具裏的第三個頁籤裏配置的。