實戰:ADFS3.0單點登錄系列-集成MVC


本文將講解如何讓MVC應用程序與ADFS集成,完成認證的過程。

 

目錄:

實戰:ADFS3.0單點登錄系列-總覽

實戰:ADFS3.0單點登錄系列-前置准備

實戰:ADFS3.0單點登錄系列-ADFS3.0安裝配置

實戰:ADFS3.0單點登錄系列-集成SharePoint

實戰:ADFS3.0單點登錄系列-集成MVC

實戰:ADFS3.0單點登錄系列-集成Exchange

實戰:ADFS3.0單點登錄系列-自定義ADFS樣式

實戰:ADFS3.0單點登錄系列-問題匯總

 

一.配置ADFS

步驟基本與上一篇相同,只是在添加轉換聲明規則向導的時候有細微差別,根據實際情況,聲明映射有差別,我這里使用upn映射到名稱。

在MVC應用程序中,使用httpContext.User.Identity.Name可以獲取登錄人的信息,當然是xxx@domain.com的形式。關於如何添加其他信息的映射,例如真實姓名等,有需要在后續章節會介紹。

 

二 MVC應用程序中的配置

這里會介紹兩種方式(我這里使用的VS2013),兩種方法分別適用於不同的場景:

方案一適用於新建的應用程序(前提是已經有了ADFS環境)

方案二適用於已經存在的項目。

 

方案一:自動感知方式

1.打開VS,點擊新建項目,並創建MVC應用程序

2.依次點擊更改身份驗證->組織賬戶—>本地

在本地頒發機構輸入ADFS元數據地址,格式為:

https://{ FQDN}/ FederationMetadata/2007-06/FederationMetadata.xml

例如:https://sts.tt.com/FederationMetadata/2007-06/FederationMetadata.xml

,應用ID Uri可空,待正式部署時進行修改。

 

 點擊保存之后,VS會自動將需要的配置(web.config)和DLL進行修改和加載。

 

方案二:手動配置方案

1.<configSections>節點下增加配置項:

<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" /> 

2.<appSettings>節點下增加配置項:

<add key="ida:FederationMetadataLocation" value="https://sts(ADFS地址)/FederationMetadata/2007-06/FederationMetadata.xml" />

<add key="ida:Realm" value="https://xxxxx/(應用程序地址)" />

<add key="ida:AudienceUri" value="https://xxxxx/(應用程序地址)" /> 

3.<system.web>節點下增加和修改配置項:

<authentication mode="None" />

 <authorization>

    <deny users="?" />

  </authorization> 

4.<system.webServer>節點下增加和修改配置項:

<modules>

      <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> 

5.</configuration>之前增加配置節

<system.identityModel>

    <identityConfiguration>

      <audienceUris>

        <add value="https://localhost:44300/" />

      </audienceUris>

      <securityTokenHandlers>

        <add type="System.IdentityModel.Services.Tokens.MachineKeySessionSecurityTokenHandler, System.IdentityModel.Services, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />

        <remove type="System.IdentityModel.Tokens.SessionSecurityTokenHandler, System.IdentityModel, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />

      </securityTokenHandlers>

      <certificateValidation certificateValidationMode="None" />

      <issuerNameRegistry type="System.IdentityModel.Tokens.ValidatingIssuerNameRegistry, System.IdentityModel.Tokens.ValidatingIssuerNameRegistry">

        <authority name="http://ADFS/adfs/services/trust">

          <keys>

            <add thumbprint="6DBC99C8BFB07435495849EC538E8A54A5587963" />(這里的證書指紋為ADFS令牌簽名證書的指紋)

          </keys>

          <validIssuers>

            <add name="http://ADFS/adfs/services/trust" />

          </validIssuers>

        </authority>

      </issuerNameRegistry>

    </identityConfiguration>

</system.identityModel> 

<system.identityModel.services>

    <federationConfiguration>

      <cookieHandler requireSsl="true" />

      <wsFederation passiveRedirectEnabled="true" issuer="https://ADFS/adfs/ls/" realm="https://xxx/" requireHttps="true" />

    </federationConfiguration>

  </system.identityModel.services>

6.dll引用

1)Web項目右鍵 添加引用 找到system.identityModel和system.identityMoel.services並選擇確認

 

2)Nugget方式添加System.IdentityModel.Tokens.ValidatingIssuerNameRegistry

 

這樣就手動完成了整個的配置過程。主要是對web.config進行修改的對dll的引用。

 

注意事項:

如果遇到這個錯誤:

解決方案:

在Global.asax中的 Application_Start()方法中增加如下代碼:

AntiForgeryConfig.UniqueClaimTypeIdentifier = ClaimTypes.Name;

意思是告訴MVC,使用的聲明類型是 Name。

 

如遇到如下錯誤:

 Cookie加解密錯誤,嘗試使用ProtectedData API解密Cookie出現錯誤

解決方案:

這個錯誤是因為多個應用程序都使用了默認的cookieName,因此只需要將不同應用程序的cookiehandler配置節配置為不同的名稱即可。

 

如果遇到如下錯誤:

解決方案:

這個錯誤是由於未正確配置指紋導致的,需要使用的是ADFS令牌簽名證書的指紋,按如下步驟找到正確的證書指紋即可。

 

原文地址:http://www.cnblogs.com/hudun/p/5919630.html


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM