使用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 ,将前面的勾勾上,然后点击“确定”,完事儿以后点“关闭”即可,如图:

 Quick tip: Enable Windows Identity Foundation (Windows 8)

这样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看看:

  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: rgb(255, 0, 0);"><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: rgb(255, 0, 0);"><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: rgb(255, 0, 0);"><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: rgb(255, 0, 0);"><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.NETFrameworkv4.0.30319aspnet_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.NETFramework64v4.0.30319aspnet_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: rgb(255, 0, 0);"><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>  
<?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.NETFrameworkv4.0.30319aspnet_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.NETFramework64v4.0.30319aspnet_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原理及单点登录的实现,我们将留到下面的文章进行讲解。

原文地址:https://www.cnblogs.com/haoliansheng/p/3360931.html