上个月有一个星期的时间都在研究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看看:

[html] view plaincopyprint?
  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.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: 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.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原理及单点登录的实现,我们将留到下面的文章进行讲解。

转载于:https://www.cnblogs.com/haoliansheng/p/3360931.html

使用WIF实现单点登录Part I——Windows Identity Foundation介绍及环境搭建 -摘自网络...相关推荐

  1. 使用WIF实现单点登录Part II —— Windows Identity Foundation基本原理

    在上一篇文章中,我们已经使用WIF构建了一个基于MVC4的简单的身份验证程序,在这篇文章里,我们将探讨一下到底什么是WIF,以及它的工作原理.然后在下一篇文章开始,我们将实际操作,实现单点登录功能. ...

  2. 如何使用Windows Identity Foundation(WTF)实现单点登录

    资料参考来源 : 我姓区不姓区 有关于WIF的介绍以及环境配置在此不多说,可以去网上搜索,或者点击上方链接前往查看,以下所述都基于WIF配置完成的条件上: 以下很多东西都是从 我姓区不姓区 的博客直接 ...

  3. Windows Identity Foundation 说明

    适用于: Windows 7 EnterpriseWindows 7 Enterprise NWindows 7 Home Basic 详细 重要提示:本文章是 Microsoft 软件自动翻译的结果 ...

  4. 【1】windows下IOS开发基础环境搭建

    一.目的 本文的目的是windows下IOS开发基础环境搭建做了对应的介绍,大家可根据文档步骤进行mac环境部署: 二.安装虚拟机 下载虚拟机安装文件绿色版,点击如下文件安装 获取安装包:       ...

  5. Windows 10下MASM汇编语言环境搭建

    Windows 10下MASM汇编语言环境搭建 另有MASM汇编语言教程: https://download.csdn.net/download/xjzdr/12692694 1.下载插件:DOSBo ...

  6. windows下 QT 的 Android 环境搭建(附软件测试)

    windows下 QT for Android环境搭建 1. QT环境搭建 2. Android 环境搭建 2.1 JDK 2.2. NDK 2.3 SDK 3. 软件测试 3.1 QT自带 Scro ...

  7. MonkeyRunner在Windows下的Eclipse开发环境搭建步骤(兼解决网上Jython配置出错的问题)...

    网上有一篇shangdong_chu网友写的文章介绍如何在Eclipse上配置MonkeyRunner,做了挺好的一个描述,但经过我的试验在我的环境上碰到了Jython解析器出错的问题,且该文章缺少P ...

  8. Windows下的PHP开发环境搭建——PHP线程安全与非线程安全、Apache版本选择,及详解五种...

    2019独角兽企业重金招聘Python工程师标准>>> 今天为在Windows下建立PHP开发环境,在考虑下载何种PHP版本时,遭遇一些让我困惑的情况,为了解决这些困惑,不出意料地牵 ...

  9. 【学习OpenCV4】案例1:Windows OpenCV C++语言开发环境搭建

    本文的案例内容源自于图书**<学习OpenCV 4:基于Python的算法实战>**,该书是国内第一本完整介绍OpenCV4的图书,第一章有在各个平台搭建环境的介绍,后面每个章节都有对模块 ...

最新文章

  1. 一图看清全球270家典型区块链服务商
  2. 研究人员通过小鼠控制光标来研究意图控制,探索更好的脑机接口使用方式
  3. java bean spring_Java+Spring+Bean+注入方式
  4. 5.sql2008分组与嵌套
  5. C++虚继承(十) --- 谈谈陈皓遗留的问题
  6. 把今天接到的任务都给搞定了
  7. Python 基础 - Day 2 Learning Note - 字符转编码操作
  8. getgrgid()函数
  9. Window2003 IIS+MySQL+PHP+Zend环境配置
  10. matlab 解相位_光测力学栅线投影技术-相位求解方法
  11. HTTP协议的基本格式
  12. 三种坐标系经纬度转化小工具
  13. android系统目录哪些不能删除,手机系统文件中的以下文件是不能删除
  14. JavaScript模块化编程实例——天气预报
  15. 2022读书感第一篇《小王子》
  16. 【教程】如何使用ArcGIS绘制荧光图
  17. 短视频剪辑怎么自学?短视频剪辑的教程分享
  18. 华为cpe虚拟服务器,你真的看懂它了吗?华为5G CPE Pro深度体验
  19. Javase 笔记知识点概要
  20. 敲黑板,定积分也有换元和分部积分法!

热门文章

  1. js修改背景图片路径_前端面试题————关键渲染路径(Critical Rendering Path)...
  2. 不相干进程之间传递文件描述符
  3. leetcode —— 938. 二叉搜索树的范围和
  4. Vue+elementUI时间选择器的应用
  5. 使用正则表达式实现将浮点数点左边的数每三位添加一个逗号
  6. 去哪儿-12-city-search-logic
  7. Opencv--学习Opencv比较好的网址
  8. MyBatis框架 动态SQL
  9. 《汇编语言》王爽—第六章实验四详解
  10. java jdbc 批处理_Java JDBC批处理插入数据操作