2013-02-18 89 views
3

我正在使用Windows Azure訪問控制服務與自定義STS。我可以通過ACS登錄到我的應用程序,但我有註銷功能的麻煩。我在我的應用程序中試過這段代碼。從自定義STS登錄訪問控制服務

 WSFederationAuthenticationModule fam = FederatedAuthentication.WSFederationAuthenticationModule; 

     try 
     { 
      FormsAuthentication.SignOut(); 
     } 
     finally 
     { 
      fam.SignOut(true); 
     } 
     Page.Response.Redirect("default.aspx"); 

但它似乎從ACS註銷用戶,而不是從自定義STS註銷。我應該怎麼做才能從STS註銷。應用程序(RP),ACS或STS中可能存在哪些問題?

我認爲ACS應該要求自定義STS註銷用戶,但它似乎並沒有這樣做。我錯過了什麼?

回答

2

ACS的2012年12月更新包括聯合單點登錄了支持:

使用WS聯盟的協議。使用ACS到 的Web應用程序啓用使用 WS聯合身份驗證協議的身份提供程序的單點登錄(SSO)現在可以利用單一登出 功能。當用戶退出Web應用程序時,ACS可以自動將用戶簽出身份提供商,並自動將其簽署給使用相同身份提供商的其他依賴方應用程序。

此功能爲WS聯合身份驗證提供程序啓用,包括 Active Directory聯合身份驗證服務2.0和Windows Live ID (Microsoft帳戶)。要啓用單點登錄時,ACS執行對WS聯盟協議端點 以下任務:

  • ACS從身份提供 識別wsignoutcleanup1.0消息,並通過發送wsignoutcleanup1.0消息給依賴方 應用程序響應。

  • ACS識別從依賴方 應用wsignout1.0和wreply消息,並通過發送wsignout1.0消息身份 提供商和wsignoutcleanup1.0消息依賴方 應用程序響應。

Code Sample: ASP.NET MVC 4 with Federated Sign-out,實施這樣的行動,從ACS登出:

(注:Windows標識基金會現已併入.NET 4.5框架,這就是爲什麼下面的新的命名空間)

using System.IdentityModel.Services; 
using System.IdentityModel.Services.Configuration; 

public ActionResult Logout() 
{ 
    // Load Identity Configuration 
    FederationConfiguration config = FederatedAuthentication.FederationConfiguration; 

    // Get wtrealm from WsFederationConfiguation Section 
    string wtrealm = config.WsFederationConfiguration.Realm; 
    string wreply; 

    // Construct wreply value from wtrealm (This will be the return URL to your app) 
    if (wtrealm.Last().Equals('/')) 
    { 
     wreply = wtrealm + "Logout"; 
    } 
    else 
    { 
     wreply = wtrealm + "/Logout"; 
    } 

    // Read the ACS Ws-Federation endpoint from web.Config 
    // something like "https://<your-namespace>.accesscontrol.windows.net/v2/wsfederation" 
    string wsFederationEndpoint = ConfigurationManager.AppSettings["ida:Issuer"]; 

    SignOutRequestMessage signoutRequestMessage = new SignOutRequestMessage(new Uri(wsFederationEndpoint)); 

    signoutRequestMessage.Parameters.Add("wreply", wreply); 
    signoutRequestMessage.Parameters.Add("wtrealm", wtrealm); 

    FederatedAuthentication.SessionAuthenticationModule.SignOut(); 

    string signoutUrl = signoutRequestMessage.WriteQueryString(); 

    return this.Redirect(signoutUrl); 
} 
+0

謝謝!我一直在尋找這樣的解決方案。像魅力一樣工作。 – 2013-09-16 16:42:29

5

我已經創建了一個輔助方法做FederatedSignout,有什麼我一路上發現(HTH)

012代碼中的註釋
public static void FederatedSignOut(string reply = null) 
{ 
    WSFederationAuthenticationModule fam = FederatedAuthentication.WSFederationAuthenticationModule; 

    // Native FederatedSignOut doesn't seem to have a way for finding/registering realm for singout, get it from the FAM 
    string wrealm = string.Format("wtrealm={0}", fam.Realm); 

    // Create basic url for signout (wreply is set by native FederatedSignOut) 
    string signOutUrl = WSFederationAuthenticationModule.GetFederationPassiveSignOutUrl(fam.Issuer, null, wrealm); 

    // Check where to return, if not set ACS will use Reply address configured for the RP 
    string wreply = !string.IsNullOrEmpty(reply) ? reply : (!string.IsNullOrEmpty(fam.Reply) ? fam.Reply : null); 

    WSFederationAuthenticationModule.FederatedSignOut(new Uri(signOutUrl), !string.IsNullOrEmpty(wreply) ? new Uri(wreply) : null); 

    // Remarks! Native FederatedSignout has an option for setting signOutUrl to null, even if the documentation tells otherwise. 
    // If set to null the method will search for signoutUrl in Session token, but I couldn't find any information about how to set this. Found some Sharepoint code that use this 
    // Michele Leroux Bustamante had a code example (from 2010) that also uses this form. 
    // Other examples creates the signout url manually and calls redirect. 

    // FAM has support for wsignoutcleanup1.0 right out of the box, there is no need for code to handle this. 
    // That makes it even harder to understand why there are no complete FederatedSignOut method in FAM 

    // When using native FederatedSignOut() no events for signout will be called, if you need this use the FAM SignOut methods instead. 
} 

此代碼用於我們爲具有ACS的Web SSO創建的標準RP庫。

相關問題