使用WIF實現單點登陸Part I——Windows Identity Foundation介紹及環境搭建

首先先說一下什麼是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

 Quick tip: Enable Windows Identity Foundation (Windows 8)

這樣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看看:

 

[html]   view plain copy print ?
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <!--  
  3.   有關如何配置 ASP.NET 應用程序的詳細信息,請訪問  
  4.   http://go.microsoft.com/fwlink/?LinkId=169433  
  5.   -->  
  6. <configuration>  
  7.   <configSections>  
  8.     <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->  
  9.     <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=5.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />  
  10.     <span style="color:#ff0000;"><section name="system.identityModel" type="System.IdentityModel.Configuration.SystemIdentityModelSection, System.IdentityModel, Version=4.0.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089" />  
  11.     <section name="system.identityModel.services" type="System.IdentityModel.Services.Configuration.SystemIdentityModelServicesSection, System.IdentityModel.Services, Version=4.0.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089" /></span>  
  12.   </configSections>  
  13.   <connectionStrings>  
  14.     <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" />  
  15.   </connectionStrings>  
  16.   <appSettings>  
  17.     <add key="webpages:Version" value="2.0.0.0" />  
  18.     <add key="webpages:Enabled" value="false" />  
  19.     <add key="PreserveLoginUrl" value="true" />  
  20.     <add key="ClientValidationEnabled" value="true" />  
  21.     <add key="UnobtrusiveJavaScriptEnabled" value="true" />  
  22.     <span style="color:#ff0000;"><add key="ida:FederationMetadataLocation" value="http://localhost:14419/wsFederationSTS/FederationMetadata/2007-06/FederationMetadata.xml" />  
  23.     <add key="ida:Issuer" value="http://localhost:14419/wsFederationSTS/Issue" />  
  24.     <add key="ida:ProviderSelection" value="localSTS" /></span>  
  25.   </appSettings>  
  26.   <span style="color:#ff0000;"><location path="FederationMetadata">  
  27.     <system.web>  
  28.       <authorization>  
  29.         <allow users="*" />  
  30.       </authorization>  
  31.     </system.web>  
  32.   </location></span>  
  33.   <system.web>  
  34.     <span style="color:#ff0000;"><authorization>  
  35.       <deny users="?" />  
  36.     </authorization></span>  
  37.     <authentication mode="None" />  
  38.     <compilation debug="true" targetFramework="4.5" />  
  39.     <httpRuntime targetFramework="4.5" requestValidationMode="4.5" />  
  40.     <!--Commented by Identity and Access VS Package-->  
  41.     <!--<authentication mode="Forms"><forms loginUrl="~/Account/Login" timeout="2880" /></authentication>-->  
  42.     <pages>  
  43.       <namespaces>  
  44.         <add namespace="System.Web.Helpers" />  
  45.         <add namespace="System.Web.Mvc" />  
  46.         <add namespace="System.Web.Mvc.Ajax" />  
  47.         <add namespace="System.Web.Mvc.Html" />  
  48.         <add namespace="System.Web.Optimization" />  
  49.         <add namespace="System.Web.Routing" />  
  50.         <add namespace="System.Web.WebPages" />  
  51.       </namespaces>  
  52.     </pages>  
  53.   </system.web>  
  54.   <system.webServer>  
  55.     <validation validateIntegratedModeConfiguration="false" />  
  56.     <handlers>  
  57.       <remove name="ExtensionlessUrlHandler-ISAPI-4.0_32bit" />  
  58.       <remove name="ExtensionlessUrlHandler-ISAPI-4.0_64bit" />  
  59.       <remove name="ExtensionlessUrlHandler-Integrated-4.0" />  
  60.       <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" />  
  61.       <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" />  
  62.       <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" />  
  63.     </handlers>  
  64.     <modules>  
  65.       <remove name="FormsAuthentication" />  
  66.       <add name="WSFederationAuthenticationModule" type="System.IdentityModel.Services.WSFederationAuthenticationModule, System.IdentityModel.Services, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" preCondition="managedHandler" />  
  67.       <add name="SessionAuthenticationModule" type="System.IdentityModel.Services.SessionAuthenticationModule, System.IdentityModel.Services, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" preCondition="managedHandler" />  
  68.     </modules>  
  69.   </system.webServer>  
  70.   <runtime>  
  71.     <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">  
  72.       <dependentAssembly>  
  73.         <assemblyIdentity name="System.Web.Helpers" publicKeyToken="31bf3856ad364e35" />  
  74.         <bindingRedirect oldVersion="1.0.0.0-2.0.0.0" newVersion="2.0.0.0" />  
  75.       </dependentAssembly>  
  76.       <dependentAssembly>  
  77.         <assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35" />  
  78.         <bindingRedirect oldVersion="1.0.0.0-4.0.0.0" newVersion="4.0.0.0" />  
  79.       </dependentAssembly>  
  80.       <dependentAssembly>  
  81.         <assemblyIdentity name="System.Web.WebPages" publicKeyToken="31bf3856ad364e35" />  
  82.         <bindingRedirect oldVersion="1.0.0.0-2.0.0.0" newVersion="2.0.0.0" />  
  83.       </dependentAssembly>  
  84.     </assemblyBinding>  
  85.   </runtime>  
  86.   <entityFramework>  
  87.     <defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">  
  88.       <parameters>  
  89.         <parameter value="v11.0" />  
  90.       </parameters>  
  91.     </defaultConnectionFactory>  
  92.   </entityFramework>  
  93.   <system.identityModel>  
  94.     <identityConfiguration>  
  95.       <audienceUris>  
  96.         <add value="http://localhost:8007/" />  
  97.       </audienceUris>  
  98.       <issuerNameRegistry type="System.IdentityModel.Tokens.ConfigurationBasedIssuerNameRegistry, System.IdentityModel, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">  
  99.         <trustedIssuers>  
  100.           <add thumbprint="9B74CB2F320F7AAFC156E1252270B1DC01EF40D0" name="LocalSTS" />  
  101.         </trustedIssuers>  
  102.       </issuerNameRegistry>  
  103.       <certificateValidation certificateValidationMode="None" />  
  104.     </identityConfiguration>  
  105.   </system.identityModel>  
  106.   <span style="color:#ff0000;"><system.identityModel.services>  
  107.     <federationConfiguration>  
  108.       <cookieHandler requireSsl="false" />  
  109.       <wsFederation passiveRedirectEnabled="true" issuer="http://localhost:14419/wsFederationSTS/Issue" realm="http://localhost:8007/" requireHttps="false" />  
  110.     </federationConfiguration>  
  111.   </system.identityModel.services></span>  
  112. </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工具裏的第三個頁籤裏配置的。

相關文章
相關標籤/搜索